Home > Blockchain >  How to forbid a key from object
How to forbid a key from object

Time:11-25

Seems like a no brainer but I can't figure how to add a constraint to forbid the existence of a key in Joi, please how do I do this.

    const data = {foo: 'xyz', bar: '123'};
    const schema = {
        foo: Joi.string(),
        // how do i forbid bar
        bar: Joi.forbid()
    };
    
    const { error } = Joi.object(schema).validate(data)

CodePudding user response:

you can simple delete the key from the object :

1st Method : delete data.bar

2nd method : {bar,...schema}=data

CodePudding user response:

Use forbidden: https://joi.dev/api/?v=17.4.2#anyforbidden

const schema = {
    a: Joi.any().forbidden()
};
  • Related