Complete the code to import the schema validation library.
const Joi = require('[1]');
We use joi to define and validate schemas in Express apps.
Complete the code to define a schema for a user with a required string name.
const schema = Joi.object({ name: Joi.[1]().required() });The string() method defines a string type in Joi schemas.
Fix the error in the validation call to check the request body.
const { error } = schema.[1](req.body);The correct method to validate data with Joi is validate().
Fill both blanks to create middleware that validates the request body and sends an error if invalid.
function validateUser(req, res, next) {
const { error } = schema.[1](req.body);
if (error) return res.status([2]).send(error.details[0].message);
next();
}Use validate to check the data and send status 400 for bad requests.
Fill all three blanks to define a schema with a required string 'username', an optional number 'age', and validate an object.
const schema = Joi.object({
username: Joi.[1]().required(),
age: Joi.[2](),
});
const { error } = schema.[3]({ username: 'Alice', age: 30 });Use string() for username, number() for age, and validate() to check the object.
