Farhan Yaseen Farhan Yaseen - 2 months ago 9
Node.js Question

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

Answer

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