如何在 fastest-validator 中使用字符串属性
How to use String properties in fastest-validator
Link: https://www.npmjs.com/package/fastest-validator
我在我的 NodeJS 应用程序中使用 fastest-validation
。我在这方面取得了巨大的成功。不幸的是,我 运行 遇到了一个我似乎无法弄清楚的问题。
如果您在 String
下查看(位于此处:https://www.npmjs.com/package/fastest-validator#user-content-string)
我正在尝试使用 属性 numeric
,因为我有一个字符串,它是我想要验证的数字。我找不到任何例子,所以我只能假设我必须将 属性 设置为 true
。这似乎不起作用,因为我试图通过设置另一个数字字符串字段并将 alpha
的 属性 设置为 true 来验证该理论。我完全预计我的 'label' 会通过,而我的 'value' 字段会失败。但是都通过了
您应该如何使用这些属性?
请参阅下面的代码:
buildSchema.catalogPages = {
type: "array", items: {
type: "object", props: {
label: { type: "string", empty: false, numeric: true },
value: { type: "string", empty: false, alpha: true }
}
}
}
const v = new Validator()
const check = v.compile(buildSchema)
check(valuesToCheck)
这是我的数据:
const valuesToCheck = [
{
label: "9"
value: "9"
},
{
label: "12"
value: "12"
},
编辑
我刚刚解决了我的问题。我有一个处理函数来检查我的模式和 valuesToCheck。我所做的是返回 check(valuesToCheck)
并假设它只是 returns 真或假。但实际上,如果检查失败,它会 returns 失败的数组(这很棒)。我将接受 tam.teixeira 的回答,因为它帮助我意识到我需要更新我的处理程序以检查它是布尔值 true 还是数组(也就是失败)。
我认为这应该与模式本身无关,在以下示例中:
const Validator = require('fastest-validator');
const schema = {
myItems: {
type: 'array',
items: {
type: 'object',
props: {
label: { type: 'string', empty: false, numeric: true },
value: { type: 'string', empty: false, alpha: true },
},
},
},
};
const valuesToCheck = {
myItems: [
{
label: '9',
value: '9',
},
{
label: '12',
value: '12',
},
],
};
const v = new Validator();
const check = v.compile(schema);
console.log(JSON.stringify(check(valuesToCheck), null, 4));
确实验证失败并出现错误:
$> node fastestValidatorTest.js
[
{
"type": "stringAlpha",
"message": "The 'myItems[0].value' field must be an alphabetic string.",
"field": "myItems[0].value",
"actual": "9"
},
{
"type": "stringAlpha",
"message": "The 'myItems[1].value' field must be an alphabetic string.",
"field": "myItems[1].value",
"actual": "12"
}
]
所以我得到了您预期的行为:“'label' 通过,我的 'value' 字段失败”
所以我的结论是 buildSchema.catalogPages
部分可能有些地方不对,对我来说感觉有点可疑而且我也得到了你的示例的预期行为,但我已经更改了模式对象更简单。
PS:谢谢你的问题,我不知道图书馆,所以学到了一些新东西,这很酷
Link: https://www.npmjs.com/package/fastest-validator
我在我的 NodeJS 应用程序中使用 fastest-validation
。我在这方面取得了巨大的成功。不幸的是,我 运行 遇到了一个我似乎无法弄清楚的问题。
如果您在 String
下查看(位于此处:https://www.npmjs.com/package/fastest-validator#user-content-string)
我正在尝试使用 属性 numeric
,因为我有一个字符串,它是我想要验证的数字。我找不到任何例子,所以我只能假设我必须将 属性 设置为 true
。这似乎不起作用,因为我试图通过设置另一个数字字符串字段并将 alpha
的 属性 设置为 true 来验证该理论。我完全预计我的 'label' 会通过,而我的 'value' 字段会失败。但是都通过了
您应该如何使用这些属性?
请参阅下面的代码:
buildSchema.catalogPages = {
type: "array", items: {
type: "object", props: {
label: { type: "string", empty: false, numeric: true },
value: { type: "string", empty: false, alpha: true }
}
}
}
const v = new Validator()
const check = v.compile(buildSchema)
check(valuesToCheck)
这是我的数据:
const valuesToCheck = [
{
label: "9"
value: "9"
},
{
label: "12"
value: "12"
},
编辑
我刚刚解决了我的问题。我有一个处理函数来检查我的模式和 valuesToCheck。我所做的是返回 check(valuesToCheck)
并假设它只是 returns 真或假。但实际上,如果检查失败,它会 returns 失败的数组(这很棒)。我将接受 tam.teixeira 的回答,因为它帮助我意识到我需要更新我的处理程序以检查它是布尔值 true 还是数组(也就是失败)。
我认为这应该与模式本身无关,在以下示例中:
const Validator = require('fastest-validator');
const schema = {
myItems: {
type: 'array',
items: {
type: 'object',
props: {
label: { type: 'string', empty: false, numeric: true },
value: { type: 'string', empty: false, alpha: true },
},
},
},
};
const valuesToCheck = {
myItems: [
{
label: '9',
value: '9',
},
{
label: '12',
value: '12',
},
],
};
const v = new Validator();
const check = v.compile(schema);
console.log(JSON.stringify(check(valuesToCheck), null, 4));
确实验证失败并出现错误:
$> node fastestValidatorTest.js
[
{
"type": "stringAlpha",
"message": "The 'myItems[0].value' field must be an alphabetic string.",
"field": "myItems[0].value",
"actual": "9"
},
{
"type": "stringAlpha",
"message": "The 'myItems[1].value' field must be an alphabetic string.",
"field": "myItems[1].value",
"actual": "12"
}
]
所以我得到了您预期的行为:“'label' 通过,我的 'value' 字段失败”
所以我的结论是 buildSchema.catalogPages
部分可能有些地方不对,对我来说感觉有点可疑而且我也得到了你的示例的预期行为,但我已经更改了模式对象更简单。
PS:谢谢你的问题,我不知道图书馆,所以学到了一些新东西,这很酷