Skip to content

Commit a07ba1d

Browse files
authored
fix: use spec to find required state closes #4598 (#4599)
1 parent 8f6523a commit a07ba1d

File tree

3 files changed

+19
-27
lines changed

3 files changed

+19
-27
lines changed

.changeset/angry-steaks-itch.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@vee-validate/yup": patch
3+
---
4+
5+
fix: use spec to find required state closes #4598

packages/yup/src/index.ts

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,4 @@
1-
import {
2-
AnyObjectSchema,
3-
ArraySchema,
4-
InferType,
5-
Schema,
6-
SchemaFieldDescription,
7-
ValidateOptions,
8-
ValidationError,
9-
} from 'yup';
1+
import { AnyObjectSchema, ArraySchema, InferType, Schema, ValidateOptions, ValidationError } from 'yup';
102
import {
113
TypedSchema,
124
TypedSchemaError,
@@ -76,49 +68,40 @@ export function toTypedSchema<TSchema extends Schema, TOutput = InferType<TSchem
7668
},
7769
describe(path) {
7870
if (!path) {
79-
return getDescriptionFromYupDescription(yupSchema.describe());
71+
return getDescriptionFromYupSpec(yupSchema.spec);
8072
}
8173

82-
const description = getDescriptionForPath(path, yupSchema);
74+
const description = getSpecForPath(path, yupSchema);
8375
if (!description) {
8476
return {
8577
required: false,
8678
exists: false,
8779
};
8880
}
8981

90-
return getDescriptionFromYupDescription(description);
82+
return getDescriptionFromYupSpec(description);
9183
},
9284
};
9385

9486
return schema;
9587
}
9688

97-
function getDescriptionFromYupDescription(desc: SchemaFieldDescription): TypedSchemaPathDescription {
98-
if ('tests' in desc) {
99-
const required = desc?.tests?.some(t => t.name === 'required') || false;
100-
101-
return {
102-
required,
103-
exists: true,
104-
};
105-
}
106-
89+
function getDescriptionFromYupSpec(spec: AnyObjectSchema['spec']): TypedSchemaPathDescription {
10790
return {
108-
required: false,
109-
exists: false,
91+
required: !spec.optional,
92+
exists: true,
11093
};
11194
}
11295

113-
function getDescriptionForPath(path: string, schema: Schema): SchemaFieldDescription | null {
96+
function getSpecForPath(path: string, schema: Schema): AnyObjectSchema['spec'] | null {
11497
if (!isObjectSchema(schema)) {
11598
return null;
11699
}
117100

118101
if (isNotNestedPath(path)) {
119102
const field = schema.fields[cleanupNonNestedPath(path)];
120103

121-
return field?.describe() || null;
104+
return (field as AnyObjectSchema)?.spec || null;
122105
}
123106

124107
const paths = (path || '').split(/\.|\[(\d+)\]/).filter(Boolean);
@@ -133,7 +116,7 @@ function getDescriptionForPath(path: string, schema: Schema): SchemaFieldDescrip
133116
}
134117

135118
if (i === paths.length - 1) {
136-
return currentSchema.describe();
119+
return currentSchema.spec;
137120
}
138121
}
139122

packages/yup/tests/yup.spec.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,7 @@ test('reports required state on fields', async () => {
331331
yup.object({
332332
'not.nested.req': yup.string().required(),
333333
name: yup.string(),
334+
num: yup.number().required(),
334335
email: yup.string().required(),
335336
nested: yup.object({
336337
arr: yup.array().of(yup.object({ req: yup.string().required(), nreq: yup.string() })),
@@ -347,6 +348,7 @@ test('reports required state on fields', async () => {
347348
});
348349

349350
const { meta: name } = useField('name');
351+
const { meta: num } = useField('num');
350352
const { meta: email } = useField('email');
351353
const { meta: req } = useField('nested.obj.req');
352354
const { meta: nreq } = useField('nested.obj.nreq');
@@ -356,6 +358,7 @@ test('reports required state on fields', async () => {
356358

357359
metaSpy({
358360
name: name.required,
361+
num: num.required,
359362
email: email.required,
360363
objReq: req.required,
361364
objNreq: nreq.required,
@@ -376,6 +379,7 @@ test('reports required state on fields', async () => {
376379
expect.objectContaining({
377380
name: false,
378381
email: true,
382+
num: true,
379383
objReq: true,
380384
objNreq: false,
381385
arrReq: true,

0 commit comments

Comments
 (0)