I want to add some expressions before throwing error message.
Below is the example code that I want to implement.
const schema = Yup.object().shape({
name: yup.string().required(() => "error message")
});
Instead of just passing a string like this
const schema = Yup.object().shape({
name: yup.string().required("error message")
});
Should be able to pass in a function using Yup test method. You just have to be sure to createError and return that to yup once created.
const schema = Yup.object().shape({
name: Yup.string()
.string()
.required()
.test("name validation fn", "Name Validation Error", function (value) { // Use function
const customError = myCustomErrorFn(value);
return this.createError({message: customError });
})
});
see Yup validation docs