Reputation: 75
Hi I am wondering if I can make a validation that is not based on a user input but based on an object derived from somewhere else. For example I have an object like this:
userDetails{
"id":1,
"surname":"Wood",
"firstName":"Victor"
}
and I have a Yup validation like this
export const userDetailValidation= Yup.object().shape({
surname: Yup.string().
.required("This field is required.")
.max(50, "Maximum character is 50.")
firstName: Yup.string().
.required("This field is required.")
.max(50, "Maximum character is 50.")
});
What can I do so that I can validate the object that I have based on that Yup Validation?
Upvotes: 0
Views: 45
Reputation: 243
Yes, you can use the Yup validation to validate your object. You can use the .validate()
method to check if the object meets the validation requirements. For example:
const isValid = userDetailValidation.validate(userDetails, {abortEarly: false});
If the validation passes, isValid will be true. Otherwise, it will be false.
Upvotes: 2