如何在Joi String验证中使用枚举值 [英] How to use enum values with Joi String validation

查看:357
本文介绍了如何在Joi String验证中使用枚举值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在对我的HTTP请求使用Joi验证程序.那里有一个名为type的参数.我需要确保该参数的可能值为"ios"或"android".

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".

我该怎么办?

body : {
  device_key : joi.string().required(),
  type : joi.string().required()
}

推荐答案

您可以使用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);

这会导致错误ValidationError: child "type" fails because ["type" must be one of [ios, android]]

这篇关于如何在Joi String验证中使用枚举值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆