我正在为我的api构建一个hapi-swagger接口。其中一个查询参数type具有另一个依赖于前者的查询param子类型。我已经知道了如何实现Joi validation for it successfully,但在这个接口上并不那么成功。我的验证代码是
{
    type: Joi.string()
         .valid('image', 'publication', 'dataset')
         .optional(),
    subtype: Joi.string()
         .optional()
         .when('type', {is: 'image',       then: Joi.valid('png', 'jpg')})
         .when('type', {is: 'publication', then: Joi.valid('newspaper', 'book')})
         .description('subtype based on the file_type')
}但是接口只显示了子类型的、png、和jpg。关于如何实现这一点的建议,以便正确的子类型显示何时选择相应的类型?
发布于 2017-10-10 09:51:21
我也尝试过类似的方法,这对我来说很好。请检查我的代码如下:
Joi.object().keys({
  billFormat: Joi.string().valid('sms', 'email').required(),
  email: Joi.string()
    .when('ebillFormat', { is: 'sms', then: Joi.valid('a', 'b') })
    .when('ebillFormat', { is: 'email', then: Joi.valid('c', 'd') }),
});我的有效载荷如下:
{
    "ebillFormat": "email",
    "email": "hello"
}我得到的错误如下:
{
    "statusCode": 400,
    "error": "Bad Request",
    "message": "child \"email\" fails because [\"email\" must be one of [c, d]]",
    "validation": {
        "source": "payload",
        "keys": [
            "email"
        ]
    }
}请让我知道你到底在努力实现什么,你面临什么问题。
https://stackoverflow.com/questions/46593497
复制相似问题