joi custom error

Joi Custom Error

If you have ever worked with Node.js and Joi library, you might have come across situations where you want to customize the error message for a specific validation rule. This is where Joi custom error comes into play.

What is Joi?

Joi is a library for object schema validation in JavaScript. It allows you to create blueprints or schemas for JavaScript objects to ensure that they have required properties and are of the expected data type. It also provides validation rules for strings, numbers, dates, and more.

What is a custom error?

A custom error is an error message that you can customize to provide more meaningful feedback to the user. When a validation fails, Joi returns an error object that contains a message property. By default, the message property is set to a generic error message that may not be very helpful to the user.

How to create a custom error in Joi?

Creating a custom error in Joi is quite simple. You just need to pass an options object with a messages property to the validate method. The messages property should be an object that maps validation rule names to custom error messages.


const Joi = require('joi');

const schema = Joi.object({
  username: Joi.string().alphanum().min(3).max(30).required(),
  password: Joi.string().pattern(/^[a-zA-Z0-9]{3,30}$/),
});

const options = {
  messages: {
    'string.alphanum': 'Username must only contain alpha-numeric characters',
    'string.min': 'Username must be at least {{limit}} characters long',
    'string.max': 'Username cannot be longer than {{limit}} characters',
    'any.required': 'Username is required',
    'string.pattern.base': 'Password must be at least 3 characters long and contain only letters and numbers',
  },
};

const { error, value } = schema.validate({ username: 'abc123', password: 'password' }, options);

if (error) {
  console.log(error.details);
} else {
  console.log(value);
}
  • In the above code, we first define a Joi schema for an object that has a username and password field.
  • We then define an options object that contains custom error messages for specific validation rules.
  • We pass the options object to the validate method along with the object to validate.
  • If there is an error, we log the error details. Otherwise, we log the validated object.

As you can see, we have customized the error messages for the alphanum, min, max, required, and pattern.base rules. You can customize error messages for any validation rule supported by Joi.

Conclusion

Joi custom error allows you to provide more meaningful error messages to the user when a validation fails. By creating a custom error object, you can customize the error messages for specific validation rules. This makes it easier for users to understand what went wrong and how to fix it.

Subscribe to The Poor Coder | Algorithm Solutions

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]
Subscribe