I'm using yup to create validations for my data , how can i handle enums?
this is a sample of my validations - I'm using object.shape method of the yup:
export const deleteCityValidation = yup.object().shape({
id: yup.string()
});
looking for a way to validate an input field that should only have a value from a set of enums any help is appreciated .
is it possible to use yup.arrays to validate enums ?
Yup is a JavaScript schema builder for value parsing and validation. Define a schema, transform a value to match, validate the shape of an existing value, or both. Yup schema are extremely expressive and allow modeling complex, interdependent validations, or value transformations.
you can use example like below:
let schema = yup.mixed().oneOf(['jimmy', 42]);
await schema.isValid(42); // => true
await schema.isValid('jimmy'); // => true
await schema.isValid(new Date()); // => false
more info
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With