Skip to content
the poor coder

joi custom error

Poor Man
Published
Reading time
2 min read
On this page

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.

Text

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.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC