diff --git a/.changeset/angry-steaks-itch.md b/.changeset/angry-steaks-itch.md new file mode 100644 index 000000000..b327078ce --- /dev/null +++ b/.changeset/angry-steaks-itch.md @@ -0,0 +1,5 @@ +--- +"@vee-validate/yup": patch +--- + +fix: use spec to find required state closes #4598 diff --git a/packages/yup/src/index.ts b/packages/yup/src/index.ts index 5e4410601..3ece1876d 100644 --- a/packages/yup/src/index.ts +++ b/packages/yup/src/index.ts @@ -1,12 +1,4 @@ -import { - AnyObjectSchema, - ArraySchema, - InferType, - Schema, - SchemaFieldDescription, - ValidateOptions, - ValidationError, -} from 'yup'; +import { AnyObjectSchema, ArraySchema, InferType, Schema, ValidateOptions, ValidationError } from 'yup'; import { TypedSchema, TypedSchemaError, @@ -76,10 +68,10 @@ export function toTypedSchema t.name === 'required') || false; - - return { - required, - exists: true, - }; - } - +function getDescriptionFromYupSpec(spec: AnyObjectSchema['spec']): TypedSchemaPathDescription { return { - required: false, - exists: false, + required: !spec.optional, + exists: true, }; } -function getDescriptionForPath(path: string, schema: Schema): SchemaFieldDescription | null { +function getSpecForPath(path: string, schema: Schema): AnyObjectSchema['spec'] | null { if (!isObjectSchema(schema)) { return null; } @@ -118,7 +101,7 @@ function getDescriptionForPath(path: string, schema: Schema): SchemaFieldDescrip if (isNotNestedPath(path)) { const field = schema.fields[cleanupNonNestedPath(path)]; - return field?.describe() || null; + return (field as AnyObjectSchema)?.spec || null; } const paths = (path || '').split(/\.|\[(\d+)\]/).filter(Boolean); @@ -133,7 +116,7 @@ function getDescriptionForPath(path: string, schema: Schema): SchemaFieldDescrip } if (i === paths.length - 1) { - return currentSchema.describe(); + return currentSchema.spec; } } diff --git a/packages/yup/tests/yup.spec.ts b/packages/yup/tests/yup.spec.ts index 54f5834f5..bad35b41f 100644 --- a/packages/yup/tests/yup.spec.ts +++ b/packages/yup/tests/yup.spec.ts @@ -331,6 +331,7 @@ test('reports required state on fields', async () => { yup.object({ 'not.nested.req': yup.string().required(), name: yup.string(), + num: yup.number().required(), email: yup.string().required(), nested: yup.object({ arr: yup.array().of(yup.object({ req: yup.string().required(), nreq: yup.string() })), @@ -347,6 +348,7 @@ test('reports required state on fields', async () => { }); const { meta: name } = useField('name'); + const { meta: num } = useField('num'); const { meta: email } = useField('email'); const { meta: req } = useField('nested.obj.req'); const { meta: nreq } = useField('nested.obj.nreq'); @@ -356,6 +358,7 @@ test('reports required state on fields', async () => { metaSpy({ name: name.required, + num: num.required, email: email.required, objReq: req.required, objNreq: nreq.required, @@ -376,6 +379,7 @@ test('reports required state on fields', async () => { expect.objectContaining({ name: false, email: true, + num: true, objReq: true, objNreq: false, arrReq: true,