Skip to content

Commit f290933

Browse files
feat: provide form values as context for yup schema extending #4753 (#4837)
1 parent 3d2ec93 commit f290933

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

packages/vee-validate/src/validate.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ export async function validateTypedSchema<TValues extends GenericObject, TOutput
310310
values: TValues,
311311
): Promise<FormValidationResult<TValues, TOutput>> {
312312
const typedSchema = isTypedSchema(schema) ? schema : yupToTypedSchema(schema);
313-
const validationResult = await typedSchema.parse(deepCopy(values));
313+
const validationResult = await typedSchema.parse(deepCopy(values), { formData: deepCopy(values) });
314314

315315
const results: Partial<FlattenAndMapPathsValidationResult<TValues, TOutput>> = {};
316316
const errors: Partial<Record<Path<TValues>, string>> = {};

packages/vee-validate/tests/Form.spec.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3222,3 +3222,48 @@ test('provides form values as yup context refs', async () => {
32223222
expect(pwError?.textContent).toBeFalsy();
32233223
expect(cpwError?.textContent).toBeFalsy();
32243224
});
3225+
3226+
test('provides form values as yup context refs for schema validation', async () => {
3227+
mountWithHoc({
3228+
setup() {
3229+
const validationSchema = yup.object({
3230+
password: yup.string().required().min(3).label('Password'),
3231+
confirmPassword: yup
3232+
.string()
3233+
.required()
3234+
.oneOf([yup.ref('$password')])
3235+
.label('Confirm Password'),
3236+
});
3237+
3238+
return {
3239+
validationSchema,
3240+
};
3241+
},
3242+
template: `
3243+
<VForm v-slot="{ errors }" :validation-schema="validationSchema">
3244+
<Field id="password" name="password" type="password" />
3245+
<span id="passwordErr">{{ errors.password }}</span>
3246+
3247+
<Field id="confirmPassword" name="confirmPassword" type="password" />
3248+
<span id="confirmPasswordErr">{{ errors.confirmPassword }}</span>
3249+
3250+
<button>Validate</button>
3251+
</VForm>
3252+
`,
3253+
});
3254+
3255+
const pwError = document.querySelector('#passwordErr');
3256+
const cpwError = document.querySelector('#confirmPasswordErr');
3257+
3258+
await flushPromises();
3259+
setValue(document.querySelector('#password') as HTMLInputElement, '123');
3260+
setValue(document.querySelector('#confirmPassword') as HTMLInputElement, '12');
3261+
await flushPromises();
3262+
3263+
expect(pwError?.textContent).toBeFalsy();
3264+
expect(cpwError?.textContent).toBeTruthy();
3265+
setValue(document.querySelector('#confirmPassword') as HTMLInputElement, '123');
3266+
await flushPromises();
3267+
expect(pwError?.textContent).toBeFalsy();
3268+
expect(cpwError?.textContent).toBeFalsy();
3269+
});

0 commit comments

Comments
 (0)