Joi is a library that lets us validate an object’s structure with ease.
In this article, we’ll look at how to validate objects with Joi.
Getting Started
We can install Joi by running:
npm i joi
Then we can use it by writing:
const Joi = require("joi");
const schema = Joi.object({
username: Joi.string().alphanum().min(3).max(30).required(),
password: Joi.string().pattern(new RegExp("^[a-zA-Z0-9]+$"))
});
const value = schema.validate({ username: "abc", password: "abc123" });
console.log(value);
We create a schema with Joi by calling its built-in methods.
The keys are the property names,
string
makes sure it’s a string.
alphanum
checks for alphanumeric characters.
min
checks for a minimum amount of characters.
max
checks for a max amount of characters.
required
makes the property value required.
pattern
checks for a given regex pattern.
Once we created a schema, we called schema.validate
to validate the property values.
It returns an object with the value
and error
properties.
value
has the argument we passed in.
error
has any error that are found.
If we call:
const value = schema.validate({});
then error
would be an object with the message:
'ValidationError: "username" is required'
The details
property has more details.
We can do the validation asynchronously with the validateAsync
method.
For example, we can write:
const Joi = require("joi");
const schema = Joi.object({
username: Joi.string().alphanum().min(3).max(30).required(),
password: Joi.string().pattern(new RegExp("^[a-zA-Z0-9]+$"))
});
(async () => {
try {
const value = await schema.validateAsync({
username: "abc",
password: "abc123"
});
console.log(value);
} catch (err) {
console.log(err);
}
})();
to do the validation.
value
has the value we passed into validateAsync
.
And err
has validation errors if there are any.
Assertions
The assert
mthod lets us validate against a schema and throws an error if validation fails.
For example, we can write:
Joi.assert("x", Joi.number());
Then we would get:
"value" must be a number
error displayed.
The first argument is the value to validate.
The 2nd argument is the data type to validate against.
The attempt
method validates against a schema and returns a valid object.
It’ll throw an error if validation fails.
For example, we can write:
const result = Joi.attempt("10", Joi.number());
console.log(result);
and result
is 10.
Defaults
The defaults
method returns a new joi
instance that applies the provided modifier function to every new schema.
For example, if we have:
const Joi = require("joi");
const custom = Joi.defaults((schema) => {
switch (schema.type) {
case "string":
return schema.allow("");
case "object":
return schema.min(1);
default:
return schema;
}
});
const schema = custom.object();
Then schema
is schema.min(1)
since 'object'
is the value of schema.type
.
in
The in
method returns a reference to another schema.
For example, we can write:
const Joi = require("joi");
const schema = Joi.object({
a: Joi.array().items(Joi.number()),
b: Joi.number().valid(Joi.in("a"))
});
Then we check against the schema for property a
, which is an array that have numbers.
So we check that it’s a number that’s in the a
array with the in
method.
Conclusion
We can check for object properties and values with Joi.