How to validate string with Joi?

Farhan Yaseen picture Farhan Yaseen · Sep 21, 2016 · Viewed 10.3k times · Source

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

Answer

Vsevolod Goloviznin picture Vsevolod Goloviznin · Sep 21, 2016

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