如何使用 JOI 验证来验证嵌套 json 对象的某些字段
How to validate certain fields of a nested json object using JOI validation
我需要有关如何使用 JOI 验证来验证嵌套 json 对象的某些字段的帮助。在我的示例中,我有一个包含两个子对象的对象,即 clientObj
和 agentObj
。我只对验证必需的 username
字段感兴趣,但我不想验证其余字段。如果我只提到那个字段,通过删除所有其他字段,在我的模式和 joi.validate()
函数中我得到 422 错误。代码如下:
exports.callAuthentication = function (req, res, next) {
let connectSchema = {
clientObj: joi.object().keys({
name: joi.string().min(3).max(38),
email: joi.string().min(3).max(38),
language: joi.string().min(3).max(38),
username: joi.string().min(3).max(38).required(),
mobile_no: joi.string().min(3).max(38),
time_zone: joi.string().min(3).max(38),
system_phone: joi.string().optional().allow('').min(3).max(38),
phone_no_info: joi.any().optional().allow(''),
voicemail_pin: joi.string().min(3).max(38),
display_picture: joi.string().min(3).max(38),
external_extension: joi.string().min(3).max(38)
}),
agentObj: joi.object().keys({
userId: joi.number(),
username: joi.string().min(3).max(38).required(),
name: joi.string().min(3).max(38),
email: joi.string().min(3).max(38),
status: joi.string().min(3).max(38),
role: joi.string().min(3).max(38)
})
};
const data = req.body;
joi.validate(data, connectSchema, (err) => {
if (err) {
// send a 422 error response if validation fails
res.status(422).json({
status: 'error',
message: err.details[0].message
});
} else {
req.body = data;
next();
}
});
}
您可以使用 { allowUnknown: true }
允许未知密钥
const data = {
clientObj: {
username: 'username',
otherProp: 'otherProp'
},
agentObj: {
otherProp2: 'otherProp2'
}
};
const schema = Joi.object().keys({
clientObj: Joi.object().keys({
username: Joi.string().required()
})
});
Joi.validate(data, schema, { allowUnknown: true }, (err) => {
console.log(`err with allowUnknown: ${err}`);
});
Joi.validate(data, schema, { allowUnknown: false }, (err) => {
console.log(`err without allowUnknown: ${err}`);
});
<script src="https://cdn.jsdelivr.net/npm/joi-browser@13.4.0/dist/joi-browser.min.js"></script>
我需要有关如何使用 JOI 验证来验证嵌套 json 对象的某些字段的帮助。在我的示例中,我有一个包含两个子对象的对象,即 clientObj
和 agentObj
。我只对验证必需的 username
字段感兴趣,但我不想验证其余字段。如果我只提到那个字段,通过删除所有其他字段,在我的模式和 joi.validate()
函数中我得到 422 错误。代码如下:
exports.callAuthentication = function (req, res, next) {
let connectSchema = {
clientObj: joi.object().keys({
name: joi.string().min(3).max(38),
email: joi.string().min(3).max(38),
language: joi.string().min(3).max(38),
username: joi.string().min(3).max(38).required(),
mobile_no: joi.string().min(3).max(38),
time_zone: joi.string().min(3).max(38),
system_phone: joi.string().optional().allow('').min(3).max(38),
phone_no_info: joi.any().optional().allow(''),
voicemail_pin: joi.string().min(3).max(38),
display_picture: joi.string().min(3).max(38),
external_extension: joi.string().min(3).max(38)
}),
agentObj: joi.object().keys({
userId: joi.number(),
username: joi.string().min(3).max(38).required(),
name: joi.string().min(3).max(38),
email: joi.string().min(3).max(38),
status: joi.string().min(3).max(38),
role: joi.string().min(3).max(38)
})
};
const data = req.body;
joi.validate(data, connectSchema, (err) => {
if (err) {
// send a 422 error response if validation fails
res.status(422).json({
status: 'error',
message: err.details[0].message
});
} else {
req.body = data;
next();
}
});
}
您可以使用 { allowUnknown: true }
const data = {
clientObj: {
username: 'username',
otherProp: 'otherProp'
},
agentObj: {
otherProp2: 'otherProp2'
}
};
const schema = Joi.object().keys({
clientObj: Joi.object().keys({
username: Joi.string().required()
})
});
Joi.validate(data, schema, { allowUnknown: true }, (err) => {
console.log(`err with allowUnknown: ${err}`);
});
Joi.validate(data, schema, { allowUnknown: false }, (err) => {
console.log(`err without allowUnknown: ${err}`);
});
<script src="https://cdn.jsdelivr.net/npm/joi-browser@13.4.0/dist/joi-browser.min.js"></script>