Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use enum values with Joi String validation

I am using Joi validator for my HTTP requests. There I have a parameter called type. I need to make sure that the possible values for the parameter are either "ios" or "android".

How can I do that?

body : {   device_key : joi.string().required(),   type : joi.string().required() } 
like image 522
Eranga Kapukotuwa Avatar asked May 03 '18 13:05

Eranga Kapukotuwa


1 Answers

You can use valid.

const schema = Joi.object().keys({   type: Joi.string().valid('ios', 'android'), });  const myObj = { type: 'none' }; const result = Joi.validate(myObj, schema); console.log(result); 

This gives an error ValidationError: child "type" fails because ["type" must be one of [ios, android]]

like image 170
AbhinavD Avatar answered Sep 28 '22 23:09

AbhinavD