Farhan Yaseen
Farhan Yaseen

Reputation: 2677

How to validate string with Joi?

I am using Node Joi for validation. I am new in node I want to validate env to accept only 2 words "Yes" or "No" What changes I have to make in the following code

schema = Joi.object().keys({
    app_id: Joi.string().required(),
    env: Joi.string().required()
});

Upvotes: 14

Views: 24330

Answers (1)

Vsevolod Goloviznin
Vsevolod Goloviznin

Reputation: 12324

You can use valid function to define valid values for the field:

schema = Joi.object().keys({
    app_id: Joi.string().required(),
    env: Joi.string().valid("Yes", "No").required()
});

Upvotes: 30

Related Questions