joi
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|---|---|---|---|---|
@hapi/hoek | 9.3.0 | 13.31 kB | BSD-3-Clause | prod | |
@hapi/topo | 5.1.0 | 3.72 kB | BSD-3-Clause | prod | |
@sideway/address | 4.1.5 | 15.69 kB | BSD-3-Clause | prod | |
@sideway/formula | 3.0.1 | 4.63 kB | BSD-3-Clause | prod | |
@sideway/pinpoint | 2.0.0 | 1.85 kB | BSD-3-Clause | prod |
Joi is a powerful schema description language and data validator for JavaScript. It is highly robust and aids in ensuring data integrity by validating JavaScript objects against predefined schemas. Joi can enforce a complex set of rules on an object and ensure it matches the expected schema. It is perfect for ensuring data consistency, especially when dealing with input or output data, such as form submission data or API response structures.
To use Joi, you need to first install the package using npm by executing npm install joi
in your command line interface. Once installed, you can require it in your JavaScript files and utilize it to create schemas and validate objects. Here is a basic example of creating a schema and validating an object:
const Joi = require('joi');
// Define a schema
const schema = Joi.object({
name: Joi.string().required(),
email: Joi.string().email().required(),
age: Joi.number().integer().min(18)
});
// Object to be validated
const user = {
name: 'John Doe',
email: 'john.doe@example.com',
age: 25
};
// Validate the object against the schema
const validation = schema.validate(user);
if (validation.error) {
console.log(validation.error.details);
} else {
console.log('Validation passed!');
}
In this example, we're validating a user
object. If the user
data doesn't adhere to the schema rules, you'll get a console log of errors, otherwise, it displays 'Validation passed!'.
The detailed documentation for Joi can be easily accessed through the official joi.dev Developer Portal. For specifics regarding functions, you can visit the documentation and API page. Updates about the various version statuses can be found on the versions status page and any potential changes to the library can be tracked through their changelog. The project's policies, ethos, and how to contribute are outlined on the project policies page.