For take package Benefit you need to install this beautiful package
Installing Joi is as easy as running:
npm i joi
After importing Joi at the top of your file with,
const Joi = require(“joi”);
Joi can be used by first constructing a schema, then validating a value against the constructed schema.
As we have established connection with our database, we are good to proceed with inserting data to our table:
app.post(‘/’, (req,res) => {
let insertQuery = “INSERT INTO students (name,age,hobby,email) VALUES (‘“ + req.body.name + “‘, ‘“ + req.body.age + “‘, ‘“ + req.body.hobby + “‘, ‘“ + req.body.email + “‘);”;
connection.query(insertQuery, (err, result, field) => {
res.send(result); })
})
Joi will validate all the object keys that will pass down the values to the database. And then that validated schema will be sent as the response. Our complete POST request will now look like this:
app.post(‘/’, (req,res) => {
let insertQuery = “INSERT INTO students (name,age,hobby,email) VALUES (‘“ + req.body.name + “‘, ‘“ + req.body.age + “‘, ‘“ + req.body.hobby + “‘, ‘“ + req.body.email + “‘);”;
const schema = Joi.object().keys({
name: Joi.string().required(),
age: Joi.number().required(),
hobby: Joi.string().required(),
email: Joi.string().email().required()
})
console.log(schema.validate(req.body));
if (schema.validate(req.body).error) {
res.send(schema.validate(req.body).error.details);
}
else {
connection.query(insertQuery, (err, result, field) => {
res.send(schema.validate(req.body));
})
}
})
What we are looking at above is us doing the following:
constructing a schema, our call to Joi.object(),
validating our data, our call to Joi.validate() with dataToValidate and schema as input parameters
Ok, now we understand the basic motions. What else can we do?
Well, Joi supports all sorts of primitives as well as Regex and can be nested to any depth. Let’s list some different constructs it supports:
string, this says it needs to be of type string, and we use it like so Joi.string()
number, Joi.number() and also supporting helper operations such as min() and max(), like so Joi.number().min(1).max(10)
required, we can say whether a property is required with the help of the method required, like so Joi.string().required()
any, this means it could be any type, usually, we tend to use it with the helper allow() that specifies what it can contain, like so, Joi.any().allow('a')
optional, this is strictly speaking not a type but has an interesting effect. If you specify for example prop : Joi.string().optional. If we don't provide prop then everybody's happy. However, if we do provide it and make it an integer the validation will fail
array, we can check whether the property is an array of say strings, then it would look like this Joi.array().items(Joi.string().valid('a', 'b')
regex, it supports pattern matching with RegEx as well like so Joi.string().regex(/^[a-zA-Z0-9]{3,30}$/)
The whole API for Joi is enormous. I suggest to have a look and see if there is a helper function that can solve whatever case you have that I’m not showing above
Joi API
Top comments (0)