Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to validate enums in yup

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 ?

like image 583
PayamB. Avatar asked Jan 21 '20 07:01

PayamB.


People also ask

What is Yup validation?

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.


1 Answers

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

like image 183
hbinduni Avatar answered Sep 18 '22 11:09

hbinduni