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()
});
Hapi Joi is an object schema description language and validator for JavaScript objects. With Hapi Joi, we create blueprints or schemas for JavaScript objects (an object that stores information) to ensure validation of key information.
Joi can be used for creating schemas (just like we use mongoose for creating NoSQL schemas) and you can use it with plain Javascript objects. It's like a plug n play library and is easy to use. On the other hand, express-validator uses validator. js to validate expressjs routes, and it's mainly built for express.
Joi module is a popular module for data validation. This module validates the data based on schemas. There are various functions like optional(), required(), min(), max(), etc which make it easy to use and a user-friendly module for validating the data.
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()
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With