【问题标题】:query params dependent on other query params in hapi-swagger查询参数依赖于 hapi-swagger 中的其他查询参数
【发布时间】:2017-10-10 18:50:51
【问题描述】:

我正在为我的 api 构建一个 hapi-swagger 接口。其中一个查询参数 type 有另一个查询参数 subtype 依赖于前者。我已经想出了如何实现Joivalidation 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')
}

但界面只显示 subtypepngjpg。关于如何实现这一点的建议,以便在选择相应的 type 时显示正确的 subtype

【问题讨论】:

    标签: swagger hapijs hapi-swagger


    【解决方案1】:

    我尝试了类似的方法,对我来说效果很好。请在下面查看我的代码:

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

    请让我知道您到底想达到什么目标以及您面临什么问题。

    【讨论】:

    • 正如我在问题中提到的,我能够成功地进行验证。我失败的地方(或者更确切地说,hapi-swagger 模块失败)是正确构建接口。所以,我想这更像是一个hapi-swagger 问题而不是Joi 问题。具体来说,就我而言,subtype 的下拉菜单仅显示 pngjpg 作为选项。
    • 哦,现在我知道你的问题了。据我说,这与here 提到的问题有些相关
    猜你喜欢
    • 2015-02-09
    • 2020-03-03
    • 1970-01-01
    • 1970-01-01
    • 2020-05-20
    • 1970-01-01
    • 1970-01-01
    • 2018-03-27
    • 1970-01-01
    相关资源
    最近更新 更多