简体   繁体   English

如何使用JOI验证来验证嵌套json对象的某些字段

[英]How to validate certain fields of a nested json object using JOI validation

I need help regarding how to validate certain fields of a nested json object using JOI validation. 我需要有关如何使用JOI验证来验证嵌套json对象的某些字段的帮助。 In my example I have an object which contains two sub objects ie clientObj and agentObj . 在我的示例中,我有一个包含两个子对象的对象,即clientObjagentObj I'm only interested in validating the username field which is required but I don't want to validate the remaining fields. 我只对验证必填的username段感兴趣,但我不想验证其余字段。 If I only mention that field, by deleting all other fields, in my schema and joi.validate() function I get 422 error. 如果我仅提及该字段,则通过删除所有其他字段,在我的架构和joi.validate()函数中,我将收到422错误。 Code is given below: 代码如下:

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();
        }
    });
}

You can allow unknown keys with { allowUnknown: true } 您可以使用{ 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> 

doc doc

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM