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.
isError
We can check if a property is an Error
instance with the isError
method:
Joi.isError(new Error());
isExpression
The isExpressiuon
method lets us check if the argument is an expression:
const expression = Joi.x('{a}');
Joi.isExpression(expression);
isRef
isRef
lets us check if the argument is a reference to another schema:
const ref = Joi.ref('a');
Joi.isRef(ref);
isSchema
isSchema
lets us check if the argument is a schema:
const schema = Joi.any();
Joi.isSchema(schema);
ref
ref
generates a reference to another schema.
For example, we can write:
const Joi = require("joi");
const schema = Joi.object({
a: Joi.ref("b.c"),
b: {
c: Joi.any()
}
});
to apply the Joi.any
schema by referencing the schema of the b.c
property.
The refercnes are relative, so we can write something like:
const Joi = require("joi");
const schema = Joi.object({
x: {
b: {
c: Joi.any(),
d: Joi.ref("c")
}
}
});
to reference the c
property within b
.
types
We can use the types
method to get validators that we can use to create schemas.
For example, we can write:
const Joi = require("joi");
const { object, string } = Joi.types();
const schema = object.keys({
property: string.min(10)
});
to get the string
schema from the types
method.
any
any
creates a schema that matches any data type:
const any = Joi.any();
await any.validateAsync('a');
type
We can use the type
property to get the type of the schema:
const schema = Joi.string();
schema.type === 'string';
allow
The allow
method lets us set the values we allow in a schema:
const schema = {
a: Joi.any().allow("a")
};
only 'a'
is allows as the value of property a
.
We can check for multiple values by passing in more arguments.
artifact
artifact
returns the validation result:
const schema = {
a: [
Joi.number().max(10).artifact("under"),
Joi.number().min(11).artifact("over")
]
};
custom
We can create our own schema with the custom
method:
const Joi = require("joi");
const method = (value, helpers) => {
if (value === "1") {
throw new Error("nope");
}
if (value === "2") {
return "3";
}
return value;
};
const schema = Joi.string().custom(method, "custom validation");
We check the value
and return the value we want.
We can also throw errors if the value isn’t valid.
Also, we can throw existing errors:
const Joi = require("joi");
const method = (value, helpers) => {
if (value === "1") {
return helpers.error("any.invalid");
}
if (value === "2") {
return "3";
}
return value;
};
const schema = Joi.string().custom(method, "custom validation");
We get the error from the helpers
object.
empty
empty
matches an empty schema:
let schema = Joi.string().empty('');
This will consider an empty string to be valid.
And if we have:
let schema = Joi.string().empty();
then we match undefined
.
error
error
lets us throw an error if validation fails:
const schema = Joi.string().error(new Error('string required'));
We throw the error we pass into the error
method if validation fails.
extract
extract
extracts a schema from another schema:
const schema = Joi.object({ foo: Joi.object({ bar: Joi.number() }) });
const number = schema.extract('foo.bar');
This will take the schema from foo.bar
.
Conclusion
We can validate values with various Joi methods.