How to use enum values with Joi String validation

风格不统一 提交于 2019-12-21 03:51:04

问题


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()
}

回答1:


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]]



来源:https://stackoverflow.com/questions/50156176/how-to-use-enum-values-with-joi-string-validation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!