如何在快速验证器中验证对象数组
How to validate array of objects in express validator
text_settings_descriptions: {
'en': { language_id: '5a3a13238481824b077b23ca', value: '' },
'ar': { language_id: '600eca59ced1c8317d473b54', value: '' }
}
这是我的地址数组
app.post(
'/addresses',
check('text_settings_descriptions.*.value'),
(req, res) => {
// Handle the request
},
);
我正在使用此验证来验证两种语言的值。但我希望它只验证 'en' 语言值
如果要确保en
里面的值不为空,可以使用如下:
const {check, validationResult} = require('express-validator');
app.post(
'/addresses',
check('text_settings_descriptions.en.value').notEmpty(),
(req, res) => {
const errors = validationResult(req);
console.log(errors);
// ... handle errors and request
},
);
使用上面的代码,如果 value
是一个空值,则以下内容将被记录到控制台:
{
formatter: [Function: formatter],
errors: [
{
value: '',
msg: 'Invalid value',
param: 'text_settings_descriptions.en.value',
location: 'body'
}
]
}
text_settings_descriptions: {
'en': { language_id: '5a3a13238481824b077b23ca', value: '' },
'ar': { language_id: '600eca59ced1c8317d473b54', value: '' }
}
这是我的地址数组
app.post(
'/addresses',
check('text_settings_descriptions.*.value'),
(req, res) => {
// Handle the request
},
);
我正在使用此验证来验证两种语言的值。但我希望它只验证 'en' 语言值
如果要确保en
里面的值不为空,可以使用如下:
const {check, validationResult} = require('express-validator');
app.post(
'/addresses',
check('text_settings_descriptions.en.value').notEmpty(),
(req, res) => {
const errors = validationResult(req);
console.log(errors);
// ... handle errors and request
},
);
使用上面的代码,如果 value
是一个空值,则以下内容将被记录到控制台:
{
formatter: [Function: formatter],
errors: [
{
value: '',
msg: 'Invalid value',
param: 'text_settings_descriptions.en.value',
location: 'body'
}
]
}