簡體   English   中英

依賴於hapi-swagger中其他查詢參數的查詢參數

[英]query params dependent on other query params in hapi-swagger

我正在為我的api構建一個hapi-swagger接口。 其中一個查詢參數type ,具有另一個查詢參數子類型 ,該子類型取決於前者。 我已經找到了如何成功實現Joi 驗證的方法,但是使用該界面卻不太成功。 我的驗證碼是

{
    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')
}

但是界面僅顯示子類型的 pngjpg 有關如何實現此建議的建議,以便在選擇相應的類型時顯示正確的 類型

我嘗試了類似的方法,對我來說效果很好。 請在下面簽出我的代碼:

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

請讓我知道您到底要實現什么目標,以及面臨什么問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM