joi
Joi is the most powerful schema description language and data validator for JavaScript, originally created as part of the Hapi.js framework ecosystem. Joi provi…
Installation
npm install joi
yarn add joi
pnpm add joi
Import
import Joi from 'joi';
Quick Example
import Joi from 'joi';
const schema = Joi.object({
username: Joi.string().alphanum().min(3).max(30).required(),
email: Joi.string().email().required(),
password: Joi.string().pattern(/^[a-zA-Z0-9]{8,}$/),
role: Joi.string().valid('admin', 'user').default('user'),
});
const { error, value } = schema.validate({
username: 'alice', email: '[email protected]', password: 'Pass1234',
});About joi
Joi is the most powerful schema description language and data validator for JavaScript, originally created as part of the Hapi.js framework ecosystem. Joi provides an extremely comprehensive API for describing data schemas with over 100 validation methods covering strings, numbers, dates, arrays, objects, alternatives, binary data, and symbols. The library supports complex validation scenarios including conditional requirements (.when()), forbidden fields, field references (validating one field relative to another), recursive schemas, custom validation functions, and detailed error messages with full path information. Joi's string validators include email, URI, IP address, GUID, credit card, ISO date, and regex patterns. Object schemas support .and() (all or none), .or() (at least one), .xor() (exactly one), .with() (if A then B), and .without() (if A then not B) for complex field relationships. Joi handles type coercion, default values, stripping unknown keys, and renaming keys during validation. While Joi was designed for Node.js server-side validation and is heavier than browser-focused alternatives, its unmatched validation power makes it the go-to choice for complex API input validation, configuration file validation, and data pipeline verification.
Quick Facts
| Package | joi |
| Category | Validation |
| Weekly Downloads | 5M+ |
| License | BSD-3-Clause |
| Install | npm install joi |
Related Packages
Zod is a TypeScript-first schema validation library that provides a concise, chainable API for defin…
Yup is a schema validation library for JavaScript that provides a declarative, chainable API for def…
Ajv (Another JSON Schema Validator) is the fastest JSON Schema validator for JavaScript and TypeScri…
Hapi (originally Happy.js) is a rich framework for building applications and services on Node.js, cr…
class-validator is a decorator-based validation library for TypeScript and JavaScript that uses clas…
Browse npm Packages by Category
Explore our reference of 200 popular npm packages with install commands, examples, and quick-start guides.