How to use the ValidationError function from joi
Find comprehensive JavaScript joi.ValidationError code examples handpicked from public code repositorys.
joi.ValidationError is an error class used by the joi validation library to indicate when a validation error occurs.
48 49 50 51 52 53 54 55 56 57
codePostal: '', test: 'error' }); expect(schemaValidation).toEqual({ error: new ValidationError('"test" is not allowed'), value: { dateDebut: new Date('2021-11-01T00:00:00.000Z'), dateFin: new Date('2021-12-31T11:00:00.000Z'), type: 'user',
0
1
2
+ 4 other calls in file
How does joi.ValidationError work?
joi.ValidationError
is a class that represents the errors encountered during validation of a Joi schema, which provides information about the type of error, where it occurred in the schema, and a message describing the error. When validation fails, Joi throws an instance of this class containing all the relevant information about the error.
7 8 9 10 11 12 13 14 15 16 17 18
router = express.Router(); const passwordValidator = (value, helpers) => { if (value.length < 8) { throw new Joi.ValidationError('Password must contain at least 8 characters') } if (!(value.match(/[a-z]/) && value.match(/[A-Z]/) && value.match(/[0-9]/))) { throw new Joi.ValidationError('Password must be harder') }
0
0
1
+ 11 other calls in file
71 72 73 74 75 76 77 78 79 80
// const passwordValidator = (value, helpers) => { // if (value.length < 8) { // throw new Joi.ValidationError('Password must contain at least 8 characters') // } // if (!(value.match(/[a-z]/) && value.match(/[A-Z]/) && value.match(/[0-9]/))) { // throw new Joi.ValidationError('Password must be harder') // } // return value // }
0
0
1
+ 21 other calls in file
Ai Example
1 2 3 4 5 6
const Joi = require("joi"); const schema = Joi.object({ username: Joi.string().alphanum().min(3).max(30).required(), email: Joi.string().email().required(), });
If we validate an object that doesn't meet these criteria: javascript Copy code
82 83 84 85 86 87 88 89 90 91 92 93
// POST request handling // with endpoints app.post('/api/courses', (req,res) => { // const result = Joi.ValidationError(req.body, schema); // const result = validateData(req.body); // by obj destructure const { error } = validateData(req.body);
0
0
1
+ 3 other calls in file
joi.string is the most popular function in joi (40578 examples)