Eddie Dane
Eddie Dane

Reputation: 1589

How to forbid a key from object

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)

Upvotes: 0

Views: 541

Answers (2)

Simon Epskamp
Simon Epskamp

Reputation: 9976

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

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

Upvotes: 2

chiheb bel haj ali
chiheb bel haj ali

Reputation: 102

you can simple delete the key from the object :

1st Method : delete data.bar

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

Upvotes: 0

Related Questions