-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Description
https://stackoverflow.com/questions/78830806/i-have-a-problem-in-yup-validation-when-adding-required-and-default-value
jquense/yup#502
When I add default(value) to my schema, the validation stops working. The user clears the value, but it looks like formik substitutes it (without displaying it)
To solve this problem I had to override the method required:
`
yup.addMethod(yup.number, 'required', function (message?: string) {
return this.test(
'required',
message ?? 'common.validation.required',
function (value, testContext) {
const options = testContext.options as Record<string, string>;
const defaultValue = testContext.schema.spec.default;
const originalValue = !isEmpty(options) ? options['originalValue'] : value;
const finalValue = typeof defaultValue === 'number' ? originalValue : value;
return finalValue !== '' && finalValue !== null && finalValue !== undefined;
},
);
});
`