joi.label,这个joi schema验证的方法是做什么的
joi.label , what does this method of joi schema validation do
我刚刚遇到了我必须处理的这行代码:
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 Name
、Emails
是可以传递的特定参数吗?它压倒了什么?我们还可以传递哪些其他参数等等。这个方法具体是做什么的?
如果您有此架构:
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
将覆盖 message
和 context.label
。
根据 documentation 你不能传递任何其他参数。
我刚刚遇到了我必须处理的这行代码:
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 Name
、Emails
是可以传递的特定参数吗?它压倒了什么?我们还可以传递哪些其他参数等等。这个方法具体是做什么的?
如果您有此架构:
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
将覆盖 message
和 context.label
。
根据 documentation 你不能传递任何其他参数。