diff --git a/packages/vue-components/src/components/OmegaForm/OmegaFormStuff.ts b/packages/vue-components/src/components/OmegaForm/OmegaFormStuff.ts index 1d4df881a..38568a1f8 100644 --- a/packages/vue-components/src/components/OmegaForm/OmegaFormStuff.ts +++ b/packages/vue-components/src/components/OmegaForm/OmegaFormStuff.ts @@ -346,6 +346,11 @@ export const createMeta = ( const nullableOrUndefined = isNullableOrUndefined(p.type) const isRequired = !nullableOrUndefined + // check if parent is nullOr and set meta as not required + const parentNullableOrFalse = meta.nullableOrUndefined ?? false + // return a true or false + const finalIsRequired = parentNullableOrFalse ? false : isRequired + const typeToProcess = p.type if (S.AST.isUnion(p.type)) { // First unwrap any nested unions, then filter out null/undefined @@ -367,7 +372,7 @@ export const createMeta = ( const parentMeta = createMeta({ parent: key, property: p.type, - meta: { required: isRequired, nullableOrUndefined } + meta: { required: finalIsRequired, nullableOrUndefined } }) acc[key as NestedKeyOf] = parentMeta as FieldMeta } @@ -380,7 +385,7 @@ export const createMeta = ( createMeta({ parent: key, propertySignatures: nonNullType.propertySignatures, - meta: { required: isRequired, nullableOrUndefined } + meta: { required: finalIsRequired, nullableOrUndefined } }) ) } @@ -395,7 +400,7 @@ export const createMeta = ( type: "multiple", members: arrayType.elements, rest: arrayType.rest, - required: isRequired, + required: finalIsRequired, nullableOrUndefined } as FieldMeta @@ -456,7 +461,7 @@ export const createMeta = ( const newMeta = createMeta({ parent: key, property: p.type, - meta: { required: isRequired, nullableOrUndefined } + meta: { required: finalIsRequired, nullableOrUndefined } }) acc[key as NestedKeyOf] = newMeta as FieldMeta } @@ -467,7 +472,7 @@ export const createMeta = ( createMeta({ parent: key, propertySignatures: typeToProcess.propertySignatures, - meta: { required: isRequired, nullableOrUndefined } + meta: { required: finalIsRequired, nullableOrUndefined } }) ) } else { @@ -532,7 +537,7 @@ export const createMeta = ( type: "multiple", members: p.type.elements, rest: p.type.rest, - required: isRequired, + required: finalIsRequired, nullableOrUndefined } as FieldMeta } @@ -543,7 +548,7 @@ export const createMeta = ( meta: { // an empty string is valid for a S.String field, so we should not mark it as required // TODO: handle this better via the createMeta minLength parsing - required: isRequired && (p.type._tag !== "StringKeyword" || getMetadataFromSchema(p.type).minLength), + required: finalIsRequired && (p.type._tag !== "StringKeyword" || getMetadataFromSchema(p.type).minLength), nullableOrUndefined } }) diff --git a/packages/vue-components/stories/OmegaForm/Union.vue b/packages/vue-components/stories/OmegaForm/Union.vue index 927381947..1061b4251 100644 --- a/packages/vue-components/stories/OmegaForm/Union.vue +++ b/packages/vue-components/stories/OmegaForm/Union.vue @@ -46,7 +46,8 @@ class B extends S.TaggedClass()("B", { const schema = S .Struct({ title: S.String, - union: S.Union(A, B) + // testing it works! union is nullable no asterisk showed + union: S.NullOr(S.Union(A, B)) }) const defaultValues: typeof schema.Encoded = {