[英]How create a custom message for method valid () in JOI library?
我正在像這樣驗證屬性訂閱:
Joi.object({
subscription: Joi.string()
.valid('starter', 'pro', 'business')
.required()
.messages({
'string.base': `{{#label}} should be a type of string`,
'string.empty': `{{#label}} must contain value`,
'any.required': `missing field {{#label}}`,
}),
})
如果訂閱不是這些“初學者”、“專業”、“商業”之一,我有一條消息
““訂閱”必須是 [starter, pro, business] 之一”
那么,如何為 JOI 庫中的方法 valid() 創建自定義消息呢?
您可以使用any.only :
只允許一些值,輸入與其中任何一個都不匹配。
像這樣:
'any.only': `missing field {{#label}}`
這將 output 以下自定義錯誤消息:
Validation Error: missing field "subscription"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.