gpt4 book ai didi

joi.label ,这种 joi 模式验证方法有什么作用

转载 作者:行者123 更新时间:2023-12-05 02:01:04 25 4
gpt4 key购买 nike

我刚刚遇到了我必须处理的这行代码:

Joi.array().label('Emails').items(Joi.string()).required()

我特别不明白 .label('Emails') 在做什么,所以,我打开了文档:

Overrides the key name in error messages.

name - the name of the key.

const schema = {first_name: Joi.string().label('First Name') };

这对我来说尤其没有任何意义。因为,First NameEmails 是可以传递的特定参数吗?它压倒了什么?我们还可以传递哪些其他参数等。这个方法具体做什么?

最佳答案

如果您有此架构:

const schema = Joi.object({
first_name: Joi.string().label('First Name')
});

然后您验证一个无效对象(将 first_name 作为 number 类型传递):

const { error, value } = schema.validate({ first_name: 123 })

这是 error.details 对象的样子:

[
{
message: 'first_name must be a string',
path: [ 'first_name' ],
type: 'string.base',
context: {
label: 'First Name',
valids: 123,
key: 'first_name'
}
}
]

但是,如果您使用 .label('First Name'),这就是您从错误对象中得到的:

[
{
message: 'First Name must be a string', <-- OVERRIDES
path: [ 'first_name' ],
type: 'string.base',
context: {
label: 'First Name', <-- OVERRIDES
valids: 123,
key: 'first_name'
}
}
]

因此,.label 将覆盖messagecontext.label

根据documentation你不能传递任何其他参数。

关于joi.label ,这种 joi 模式验证方法有什么作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66821398/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com