javascript - JOI - Validating complex object - Stack Overflow

I tried, and tried but can't figure it :(This is the object I need to validate:let body = {greetin

I tried, and tried but can't figure it :(

This is the object I need to validate:

let body = {
    greeting:
        {
            stringValue: 'Hello !',
            stringListValues: [],
            binaryListValues: [],
            dataType: 'String'
        },
    newsletterId:
        {
            stringValue: '123456789',
            stringListValues: [],
            binaryListValues: [],
            dataType: 'String'
        }
};

I need to validate that there is a greeting, and that is has key stringValue and that is not empty. Other values I don't care.

Also, for the second object newsletterId, and that also has key stringValue and that is not empty. Other values I don't care.

I have e up with checking only root object, with this schema:

const schema = {
    greeting: Joi.required(),
    newsletterId: Joi.required()
};

I read many examples, but I was unable to find none that has this type of structure.

I tried, and tried but can't figure it :(

This is the object I need to validate:

let body = {
    greeting:
        {
            stringValue: 'Hello !',
            stringListValues: [],
            binaryListValues: [],
            dataType: 'String'
        },
    newsletterId:
        {
            stringValue: '123456789',
            stringListValues: [],
            binaryListValues: [],
            dataType: 'String'
        }
};

I need to validate that there is a greeting, and that is has key stringValue and that is not empty. Other values I don't care.

Also, for the second object newsletterId, and that also has key stringValue and that is not empty. Other values I don't care.

I have e up with checking only root object, with this schema:

const schema = {
    greeting: Joi.required(),
    newsletterId: Joi.required()
};

I read many examples, but I was unable to find none that has this type of structure.

Share Improve this question asked Feb 7, 2019 at 16:40 Amiga500Amiga500 6,14111 gold badges71 silver badges119 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

lets define a schema :

const schema = Joi.object().keys({
    greeting: Joi.object({
       stringValue: Joi.string().required().empty(['', null]),
       stringListValues: Joi.array().items(Joi.string()),
       binaryListValues: Joi.array().items(Joi.binary())
    }).required(),
    newsletterId: // same as above
});

and test it like this :

Joi.validate(myObjectToTest, schema, function(error, cleanObject){
    console.log(error, cleanObject);
})

Full reference can be found here https://github./hapijs/joi/blob/master/API.md

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745187503a4615706.html

相关推荐

  • javascript - JOI - Validating complex object - Stack Overflow

    I tried, and tried but can't figure it :(This is the object I need to validate:let body = {greetin

    8小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信