Joi configuration and error messages

node v14.20.1
version: 2.0.0
endpointsharetweet
const Joi = require("joi") const joiOptions = { errors: { wrap: { label: "'" } } }; // We want a single quote instead of double quote // Using Joi - configuration and custom error messages const petSchema = Joi.object({ name: Joi.string().required(), food: Joi.object({ name: Joi.string().required().messages({ 'any.required': 'Food name is required' }), type: Joi.string().required().messages({ 'any.required': 'Food type is required' }), }), }); // Checking for name const result = petSchema.validate({}, joiOptions); if (result.error) { console.log(result.error.message); } // Checking for food -> type console.log(petSchema.validate({ name: 'Popo', food: { name: 'Milk' } }, joiOptions).error.message);
Loading…

no comments

    sign in to comment