joi / 8.0.5 last updated a month ago created on Sep 16th 2012

Install

npm install --save joi

Clone

git clone git@github.com:hapijs/joi.git

dependencies

main hoek 3.0.4 BSD 3-Clause
topo 2.0.0 BSD 3-Clause
isemail 2.1.0 BSD 3-Clause
moment 2.13.0 MIT

maintainers

hueniverse hueniverse
marsup marsup

versions 110 total

8.0.5 a month ago marsup
8.0.4 2 months ago marsup
8.0.3 2 months ago marsup
8.0.2 2 months ago marsup
8.0.1 3 months ago marsup
8.0.0 3 months ago marsup
7.3.0 3 months ago marsup
7.2.3 3 months ago marsup
7.2.2 3 months ago marsup
7.2.1 4 months ago marsup
7.2.0 4 months ago marsup
7.1.0 4 months ago marsup
7.0.1 5 months ago marsup
7.0.0 6 months ago marsup
6.10.1 5 months ago marsup
6.10.0 6 months ago marsup
6.9.1 7 months ago marsup
6.9.0 7 months ago marsup
6.8.1 7 months ago marsup
6.7.1 8 months ago marsup
6.7.0 8 months ago marsup
6.6.1 9 months ago marsup
6.6.0 9 months ago marsup
6.5.0 10 months ago marsup
6.4.3 a year ago marsup
6.4.2 a year ago marsup
6.4.1 a year ago marsup
6.4.0 a year ago marsup
6.3.0 a year ago marsup
6.2.0 a year ago marsup
6.1.2 a year ago marsup
6.1.1 a year ago marsup
6.1.0 a year ago marsup
6.0.8 a year ago marsup
6.0.7 a year ago marsup
6.0.6 a year ago marsup
6.0.5 a year ago marsup
6.0.4 a year ago marsup
6.0.3 a year ago marsup
6.0.2 a year ago marsup
6.0.1 a year ago marsup
6.0.0 a year ago marsup
5.1.0 a year ago marsup
5.0.2 a year ago marsup
5.0.1 a year ago marsup
5.0.0 a year ago marsup
4.9.0 a year ago marsup
4.8.1 a year ago marsup
4.8.0 a year ago marsup
4.7.0 2 years ago hueniverse
4.6.2 2 years ago hueniverse
4.6.1 2 years ago hueniverse
4.6.0 2 years ago hueniverse
4.5.0 2 years ago hueniverse
4.4.0 2 years ago hueniverse
4.3.1 2 years ago hueniverse
4.3.0 2 years ago hueniverse
4.2.1 2 years ago hueniverse
4.0.0 2 years ago hueniverse
3.1.0 2 years ago hueniverse
3.0.0 2 years ago hueniverse
2.9.0 2 years ago hueniverse
2.8.0 2 years ago hueniverse
2.7.0 2 years ago hueniverse
2.6.0 2 years ago hueniverse
2.5.0 2 years ago hueniverse
2.4.3 2 years ago hueniverse
2.4.2 2 years ago hueniverse
2.4.1 2 years ago hueniverse
2.3.1 2 years ago nvcexploder
2.3.0 2 years ago hueniverse
2.2.0 2 years ago hueniverse
2.1.0 2 years ago wyatt
2.0.3 2 years ago hueniverse
2.0.2 2 years ago hueniverse
2.0.1 2 years ago hueniverse
2.0.0 2 years ago hueniverse
1.2.3 3 years ago wyatt
1.2.2 3 years ago hueniverse
1.2.1 3 years ago hueniverse
1.2.0 3 years ago wyatt
1.1.0 3 years ago wyatt
1.0.0 3 years ago wyatt
0.4.0 3 years ago wyatt
0.3.6 3 years ago wyatt
0.3.5 3 years ago hueniverse
0.3.4 3 years ago hueniverse
0.3.3 3 years ago thegoleffect
0.3.2 3 years ago wyatt
0.3.1 3 years ago hueniverse
0.3.0 3 years ago hueniverse
0.2.6 3 years ago thegoleffect
0.2.5 3 years ago hueniverse
0.2.4 3 years ago hueniverse
0.2.3 3 years ago hueniverse
0.2.2 3 years ago hueniverse
0.2.1 3 years ago thegoleffect
0.2.0 3 years ago thegoleffect
0.1.3 3 years ago hueniverse
0.1.2 3 years ago hueniverse
0.1.1 3 years ago thegoleffect
0.1.0 3 years ago hueniverse
0.0.10 3 years ago thegoleffect
0.0.9 3 years ago thegoleffect
0.0.8 3 years ago thegoleffect
0.0.7 3 years ago hueniverse
0.0.5 3 years ago thegoleffect
0.0.4 4 years ago thegoleffect
0.0.3 4 years ago thegoleffect
0.0.2 4 years ago hueniverse
35082 Downloads yesterday.

readme

joi Logo

Object schema description language and validator for JavaScript objects.

npm version Build Status Dependencies Status DevDependencies Status Known Vulnerabilities

Join the chat at https://gitter.im/hapijs/joi

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.

Example

var Joi = require('joi');

var 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()
}).with('username', 'birthyear').without('password', 'access_token');

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

Usage

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

1
2
3
var 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.

Then the value is validated against the schema:

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

If the value is valid, null is returned, otherwise an Error object.

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

var 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:

1
2
3
var schema = Joi.object().keys({
    a: Joi.string()
});

When validating a schema:

  • Keys are optional by default.
  • Strings are utf-8 encoded by default.
  • Rules are defined in an additive fashion and evaluated in order after whitelist and blacklist checks.

API

See the API Reference.

Resolving all dependencies.. This could take a while as our cache is not yet fully warmed up.