joi

Object schema validation

This module has moved and is now available at @hapi/joi. Please update your dependencies as this version is no longer maintained an may contain bugs and security issues.
Last updated a year ago by marsup .
BSD-3-Clause · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install joi 
SYNC missed versions from official npm registry.

joi Logo

Object schema description language and validator for JavaScript objects.

npm version Build Status NSP Status Known Vulnerabilities

Lead Maintainer: Nicolas Morel

Introduction

Imagine you run facebook and you want visitors to sign up on the website with real names and not something like l337_p@nda in the first name field. How would you define the limitations of what can be inputted and validate it against the set rules?

This is joi, joi allows you to create blueprints or schemas for JavaScript objects (an object that stores information) to ensure validation of key information.

API

See the detailed API Reference.

Example

const Joi = require('joi');

const schema = Joi.object().keys({
    username: Joi.string().alphanum().min(3).max(30).required(),
    password: Joi.string().regex(/^[a-zA-Z0-9]{3,30}$/),
    access_token: [Joi.string(), Joi.number()],
    birthyear: Joi.number().integer().min(1900).max(2013),
    email: Joi.string().email({ minDomainAtoms: 2 })
}).with('username', 'birthyear').without('password', 'access_token');

// Return result.
const result = Joi.validate({ username: 'abc', birthyear: 1994 }, schema);
// result.error === null -> valid

// You can also pass a callback which will be called synchronously with the validation result.
Joi.validate({ username: 'abc', birthyear: 1994 }, schema, function (err, value) { });  // err === null -> valid

The above schema defines the following constraints:

  • username
    • a required string
    • must contain only alphanumeric characters
    • at least 3 characters long but no more than 30
    • must be accompanied by birthyear
  • password
    • an optional string
    • must satisfy the custom regex
    • cannot appear together with access_token
  • access_token
    • an optional, unconstrained string or number
  • birthyear
    • an integer between 1900 and 2013
  • email
    • a valid email address string
    • must have two domain parts e.g. example.com

Usage

Usage is a two steps process. First, a schema is constructed using the provided types and constraints:

const schema = {
    a: Joi.string()
};

Note that joi schema objects are immutable which means every additional rule added (e.g. .min(5)) will return a new schema object.

Second, the value is validated against the defined schema:

const {error, value} = Joi.validate({ a: 'a string' }, schema);

// or

Joi.validate({ a: 'a string' }, schema, function (error, value) { });

If the input is valid, then the error will be null, otherwise it will be an Error object providing more information.

The schema can be a plain JavaScript object where every key is assigned a joi type, or it can be a joi type directly:

const schema = Joi.string().min(10);

If the schema is a joi type, the schema.validate(value, callback) can be called directly on the type. When passing a non-type schema object, the module converts it internally to an object() type equivalent to:

const schema = Joi.object().keys({
    a: Joi.string()
});

When validating a schema:

  • Values (or keys in case of objects) are optional by default.

    Joi.validate(undefined, Joi.string()); // validates fine
    

    To disallow this behavior, you can either set the schema as required(), or set presence to "required" when passing options:

    Joi.validate(undefined, Joi.string().required());
    // or
    Joi.validate(undefined, Joi.string(), /* options */ { presence: "required" });
    
  • Strings are utf-8 encoded by default.

  • Rules are defined in an additive fashion and evaluated in order, first the inclusive rules, then the exclusive rules.

Browsers

Joi doesn't directly support browsers, but you could use joi-browser for an ES5 build of Joi that works in browsers, or as a source of inspiration for your own builds.

Current Tags

  • 9.0.0-9                                ...           9-beta (3 years ago)
  • 14.3.1                                ...           latest (a year ago)
  • 6.10.1                                ...           node-0.x (4 years ago)

200 Versions

  • 14.3.1 [deprecated]           ...           a year ago
  • 14.3.0 [deprecated]           ...           a year ago
  • 14.2.0 [deprecated]           ...           a year ago
  • 14.1.1 [deprecated]           ...           a year ago
  • 14.1.0 [deprecated]           ...           a year ago
  • 14.0.6 [deprecated]           ...           a year ago
  • 14.0.5 [deprecated]           ...           a year ago
  • 14.0.4 [deprecated]           ...           a year ago
  • 14.0.3 [deprecated]           ...           a year ago
  • 14.0.2 [deprecated]           ...           a year ago
  • 14.0.1 [deprecated]           ...           a year ago
  • 14.0.0 [deprecated]           ...           a year ago
  • 13.7.0 [deprecated]           ...           a year ago
  • 13.6.0 [deprecated]           ...           a year ago
  • 13.5.2 [deprecated]           ...           a year ago
  • 13.5.1 [deprecated]           ...           a year ago
  • 13.5.0 [deprecated]           ...           a year ago
  • 13.4.0 [deprecated]           ...           2 years ago
  • 13.3.0 [deprecated]           ...           2 years ago
  • 13.2.0 [deprecated]           ...           2 years ago
  • 13.1.2 [deprecated]           ...           2 years ago
  • 13.1.1 [deprecated]           ...           2 years ago
  • 13.1.0 [deprecated]           ...           2 years ago
  • 13.0.2 [deprecated]           ...           2 years ago
  • 13.0.1 [deprecated]           ...           2 years ago
  • 13.0.0 [deprecated]           ...           2 years ago
  • 12.0.0 [deprecated]           ...           2 years ago
  • 11.4.0 [deprecated]           ...           2 years ago
  • 11.3.4 [deprecated]           ...           2 years ago
  • 11.3.3 [deprecated]           ...           2 years ago
  • 11.3.2 [deprecated]           ...           2 years ago
  • 11.3.0 [deprecated]           ...           2 years ago
  • 11.2.0 [deprecated]           ...           2 years ago
  • 11.1.1 [deprecated]           ...           2 years ago
  • 11.1.0 [deprecated]           ...           2 years ago
  • 11.0.3 [deprecated]           ...           2 years ago
  • 11.0.2 [deprecated]           ...           2 years ago
  • 11.0.1 [deprecated]           ...           2 years ago
  • 11.0.0 [deprecated]           ...           2 years ago
  • 10.6.0 [deprecated]           ...           2 years ago
  • 10.5.2 [deprecated]           ...           3 years ago
  • 10.5.1 [deprecated]           ...           3 years ago
  • 10.5.0 [deprecated]           ...           3 years ago
  • 10.4.2 [deprecated]           ...           3 years ago
  • 10.4.1 [deprecated]           ...           3 years ago
  • 10.4.0 [deprecated]           ...           3 years ago
  • 10.3.4 [deprecated]           ...           3 years ago
  • 10.3.3 [deprecated]           ...           3 years ago
  • 10.3.2 [deprecated]           ...           3 years ago
  • 10.3.1 [deprecated]           ...           3 years ago
  • 10.3.0 [deprecated]           ...           3 years ago
  • 10.2.2 [deprecated]           ...           3 years ago
  • 10.2.1 [deprecated]           ...           3 years ago
  • 10.2.0 [deprecated]           ...           3 years ago
  • 10.1.0 [deprecated]           ...           3 years ago
  • 10.0.6 [deprecated]           ...           3 years ago
  • 10.0.5 [deprecated]           ...           3 years ago
  • 10.0.4 [deprecated]           ...           3 years ago
  • 10.0.2 [deprecated]           ...           3 years ago
  • 10.0.1 [deprecated]           ...           3 years ago
  • 10.0.0 [deprecated]           ...           3 years ago
  • 9.2.0 [deprecated]           ...           3 years ago
  • 9.1.1 [deprecated]           ...           3 years ago
  • 9.1.0 [deprecated]           ...           3 years ago
  • 9.0.4 [deprecated]           ...           3 years ago
  • 9.0.3 [deprecated]           ...           3 years ago
  • 9.0.2 [deprecated]           ...           3 years ago
  • 9.0.1 [deprecated]           ...           3 years ago
  • 9.0.0 [deprecated]           ...           3 years ago
  • 9.0.0-9 [deprecated]           ...           3 years ago
  • 9.0.0-8 [deprecated]           ...           3 years ago
  • 9.0.0-7 [deprecated]           ...           3 years ago
  • 9.0.0-6 [deprecated]           ...           3 years ago
  • 8.4.2 [deprecated]           ...           4 years ago
  • 9.0.0-5 [deprecated]           ...           4 years ago
  • 9.0.0-4 [deprecated]           ...           4 years ago
  • 9.0.0-3 [deprecated]           ...           4 years ago
  • 8.4.1 [deprecated]           ...           4 years ago
  • 8.4.0 [deprecated]           ...           4 years ago
  • 8.3.0 [deprecated]           ...           4 years ago
  • 8.2.1 [deprecated]           ...           4 years ago
  • 8.2.0 [deprecated]           ...           4 years ago
  • 9.0.0-2 [deprecated]           ...           4 years ago
  • 8.1.1 [deprecated]           ...           4 years ago
  • 9.0.0-1 [deprecated]           ...           4 years ago
  • 8.1.0 [deprecated]           ...           4 years ago
  • 9.0.0-0 [deprecated]           ...           4 years ago
  • 8.0.5 [deprecated]           ...           4 years ago
  • 8.0.4 [deprecated]           ...           4 years ago
  • 8.0.3 [deprecated]           ...           4 years ago
  • 8.0.2 [deprecated]           ...           4 years ago
  • 8.0.1 [deprecated]           ...           4 years ago
  • 8.0.0 [deprecated]           ...           4 years ago
  • 7.3.0 [deprecated]           ...           4 years ago
  • 7.2.3 [deprecated]           ...           4 years ago
  • 7.2.2 [deprecated]           ...           4 years ago
  • 7.2.1 [deprecated]           ...           4 years ago
  • 7.2.0 [deprecated]           ...           4 years ago
  • 7.1.0 [deprecated]           ...           4 years ago
  • 7.0.1 [deprecated]           ...           4 years ago
  • 6.10.1 [deprecated]           ...           4 years ago
  • 7.0.0 [deprecated]           ...           4 years ago
  • 6.10.0 [deprecated]           ...           4 years ago
  • 6.9.1 [deprecated]           ...           4 years ago
  • 6.9.0 [deprecated]           ...           4 years ago
  • 6.8.1 [deprecated]           ...           4 years ago
  • 6.7.1 [deprecated]           ...           4 years ago
  • 6.7.0 [deprecated]           ...           4 years ago
  • 6.6.1 [deprecated]           ...           4 years ago
  • 6.6.0 [deprecated]           ...           4 years ago
  • 6.5.0 [deprecated]           ...           4 years ago
  • 6.4.3 [deprecated]           ...           5 years ago
  • 6.4.2 [deprecated]           ...           5 years ago
  • 6.4.1 [deprecated]           ...           5 years ago
  • 6.4.0 [deprecated]           ...           5 years ago
  • 6.3.0 [deprecated]           ...           5 years ago
  • 6.2.0 [deprecated]           ...           5 years ago
  • 6.1.2 [deprecated]           ...           5 years ago
  • 6.1.1 [deprecated]           ...           5 years ago
  • 6.1.0 [deprecated]           ...           5 years ago
  • 6.0.8 [deprecated]           ...           5 years ago
  • 6.0.7 [deprecated]           ...           5 years ago
  • 6.0.6 [deprecated]           ...           5 years ago
  • 6.0.5 [deprecated]           ...           5 years ago
  • 6.0.4 [deprecated]           ...           5 years ago
  • 6.0.3 [deprecated]           ...           5 years ago
  • 6.0.2 [deprecated]           ...           5 years ago
  • 6.0.1 [deprecated]           ...           5 years ago
  • 6.0.0 [deprecated]           ...           5 years ago
  • 5.1.0 [deprecated]           ...           5 years ago
  • 5.0.2 [deprecated]           ...           5 years ago
  • 5.0.1 [deprecated]           ...           5 years ago
  • 5.0.0 [deprecated]           ...           5 years ago
  • 4.9.0 [deprecated]           ...           5 years ago
  • 4.8.1 [deprecated]           ...           5 years ago
  • 4.8.0 [deprecated]           ...           5 years ago
  • 4.7.0 [deprecated]           ...           5 years ago
  • 4.6.2 [deprecated]           ...           5 years ago
  • 4.6.1 [deprecated]           ...           5 years ago
  • 4.6.0 [deprecated]           ...           6 years ago
  • 4.5.0 [deprecated]           ...           6 years ago
  • 4.4.0 [deprecated]           ...           6 years ago
  • 4.3.1 [deprecated]           ...           6 years ago
  • 4.3.0 [deprecated]           ...           6 years ago
  • 4.2.1 [deprecated]           ...           6 years ago
  • 4.0.0 [deprecated]           ...           6 years ago
  • 3.1.0 [deprecated]           ...           6 years ago
  • 3.0.0 [deprecated]           ...           6 years ago
  • 2.9.0 [deprecated]           ...           6 years ago
  • 2.8.0 [deprecated]           ...           6 years ago
  • 2.7.0 [deprecated]           ...           6 years ago
  • 2.6.0 [deprecated]           ...           6 years ago
  • 2.5.0 [deprecated]           ...           6 years ago
  • 2.4.3 [deprecated]           ...           6 years ago
  • 2.4.2 [deprecated]           ...           6 years ago
  • 2.4.1 [deprecated]           ...           6 years ago
  • 2.3.1 [deprecated]           ...           6 years ago
  • 2.3.0 [deprecated]           ...           6 years ago
  • 2.2.0 [deprecated]           ...           6 years ago
  • 2.1.0 [deprecated]           ...           6 years ago
  • 2.0.3 [deprecated]           ...           6 years ago
  • 2.0.2 [deprecated]           ...           6 years ago
  • 2.0.1 [deprecated]           ...           6 years ago
  • 2.0.0 [deprecated]           ...           6 years ago
  • 1.2.3 [deprecated]           ...           6 years ago
  • 1.2.2 [deprecated]           ...           6 years ago
  • 1.2.1 [deprecated]           ...           6 years ago
  • 1.2.0 [deprecated]           ...           6 years ago
  • 1.1.0 [deprecated]           ...           6 years ago
  • 1.0.0 [deprecated]           ...           6 years ago
  • 0.4.0 [deprecated]           ...           6 years ago
  • 0.3.6 [deprecated]           ...           6 years ago
  • 0.3.5 [deprecated]           ...           7 years ago
  • 0.3.4 [deprecated]           ...           7 years ago
  • 0.3.3 [deprecated]           ...           7 years ago
  • 0.3.2 [deprecated]           ...           7 years ago
  • 0.3.1 [deprecated]           ...           7 years ago
  • 0.3.0 [deprecated]           ...           7 years ago
  • 0.2.6 [deprecated]           ...           7 years ago
  • 0.2.5 [deprecated]           ...           7 years ago
  • 0.2.4 [deprecated]           ...           7 years ago
  • 0.2.3 [deprecated]           ...           7 years ago
  • 0.2.2 [deprecated]           ...           7 years ago
  • 0.2.1 [deprecated]           ...           7 years ago
  • 0.2.0 [deprecated]           ...           7 years ago
  • 0.1.3 [deprecated]           ...           7 years ago
  • 0.1.2 [deprecated]           ...           7 years ago
  • 0.1.1 [deprecated]           ...           7 years ago
  • 0.1.0 [deprecated]           ...           7 years ago
  • 0.0.10 [deprecated]           ...           7 years ago
  • 0.0.9 [deprecated]           ...           7 years ago
  • 0.0.8 [deprecated]           ...           7 years ago
  • 0.0.7 [deprecated]           ...           7 years ago
  • 0.0.6-1 [deprecated]           ...           7 years ago
  • 0.0.6 [deprecated]           ...           7 years ago
  • 0.0.5 [deprecated]           ...           7 years ago
  • 0.0.4 [deprecated]           ...           7 years ago
  • 0.0.3 [deprecated]           ...           7 years ago
  • 0.0.2 [deprecated]           ...           7 years ago
  • 0.0.1 [deprecated]           ...           7 years ago
Maintainers (1)
Downloads
Today 3,714
This Week 23,457
This Month 53,204
Last Day 4,591
Last Week 28,446
Last Month 121,327
Dependencies (3)
Dev Dependencies (3)
Dependents (4561)

Copyright 2014 - 2016 © taobao.org |