![joi Logo](https://raw.github.com/hapijs/joi/master/images/joi.png)
Object schema description language and validator for JavaScript objects.
[![npm version](https://badge.fury.io/js/joi.svg)](http://badge.fury.io/js/joi)
[![Build Status](https://secure.travis-ci.org/hapijs/joi.svg)](http://travis-ci.org/hapijs/joi)
[![Dependencies Status](https://david-dm.org/hapijs/joi.svg)](https://david-dm.org/hapijs/joi)
[![DevDependencies Status](https://david-dm.org/hapijs/joi/dev-status.svg)](https://david-dm.org/hapijs/joi#info=devDependencies)
Lead Maintainer: [Nicolas Morel](https://github.com/marsup)
## Table of Contents
<img src="https://raw.github.com/hapijs/joi/master/images/validation.png" align="right" />
<!-- toc -->
- [Example](#example)
- [Usage](#usage)
- [`validate(value, schema, [options], [callback])`](#validatevalue-schema-options-callback)
- [`compile(schema)`](#compileschema)
- [`assert(value, schema, [message])`](#assertvalue-schema-message)
- [`any`](#any)
- [`any.allow(value)`](#anyallowvalue)
- [`any.valid(value)` - aliases: `only`, `equal`](#anyvalidvalue---aliases-only-equal)
- [`any.invalid(value)` - aliases: `disallow`, `not`](#anyinvalidvalue---aliases-disallow-not)
- [`any.required()`](#anyrequired)
- [`any.optional()`](#anyoptional)
- [`any.forbidden()`](#anyforbidden)
- [`any.strip()`](#anystrip)
- [`any.description(desc)`](#anydescriptiondesc)
- [`any.notes(notes)`](#anynotesnotes)
- [`any.tags(tags)`](#anytagstags)
- [`any.meta(meta)`](#anymetameta)
- [`any.example(value)`](#anyexamplevalue)
- [`any.unit(name)`](#anyunitname)
- [`any.options(options)`](#anyoptionsoptions)
- [`any.strict(isStrict)`](#anystrictisstrict)
- [`any.default([value, [description]])`](#anydefaultvalue-description)
- [`any.concat(schema)`](#anyconcatschema)
- [`any.when(ref, options)`](#anywhenref-options)
- [`any.label(name)`](#anylabelname)
- [`any.raw(isRaw)`](#anyrawisraw)
- [`any.empty(schema)`](#anyemptyschema)
- [`array`](#array)
- [`array.sparse(enabled)`](#arraysparseenabled)
- [`array.single(enabled)`](#arraysingleenabled)
- [`array.items(type)`](#arrayitemstype)
- [`array.min(limit)`](#arrayminlimit)
- [`array.max(limit)`](#arraymaxlimit)
- [`array.length(limit)`](#arraylengthlimit)
- [`array.unique()`](#arrayunique)
- [`boolean`](#boolean)
- [`binary`](#binary)
- [`binary.encoding(encoding)`](#binaryencodingencoding)
- [`binary.min(limit)`](#binaryminlimit)
- [`binary.max(limit)`](#binarymaxlimit)
- [`binary.length(limit)`](#binarylengthlimit)
- [`date`](#date)
- [`date.min(date)`](#datemindate)
- [`date.max(date)`](#datemaxdate)
- [`date.format(format)`](#dateformatformat)
- [`date.iso()`](#dateiso)
- [`func`](#func)
- [`number`](#number)
- [`number.min(limit)`](#numberminlimit)
- [`number.max(limit)`](#numbermaxlimit)
- [`number.greater(limit)`](#numbergreaterlimit)
- [`number.less(limit)`](#numberlesslimit)
- [`number.integer()`](#numberinteger)
- [`number.precision(limit)`](#numberprecisionlimit)
- [`number.multiple(base)`](#numbermultiplebase)
- [`number.positive()`](#numberpositive)
- [`number.negative()`](#numbernegative)
- [`object`](#object)
- [`object.keys([schema])`](#objectkeysschema)
- [`object.min(limit)`](#objectminlimit)
- [`object.max(limit)`](#objectmaxlimit)
- [`object.length(limit)`](#objectlengthlimit)
- [`object.pattern(regex, schema)`](#objectpatternregex-schema)
- [`object.and(peers)`](#objectandpeers)
- [`object.nand(peers)`](#objectnandpeers)
- [`object.or(peers)`](#objectorpeers)
- [`object.xor(peers)`](#objectxorpeers)
- [`object.with(key, peers)`](#objectwithkey-peers)
- [`object.without(key, peers)`](#objectwithoutkey-peers)
- [`object.rename(from, to, [options])`](#objectrenamefrom-to-options)
- [`object.assert(ref, schema, [message])`](#objectassertref-schema-message)
- [`object.unknown([allow])`](#objectunknownallow)
- [`object.type(constructor, [name])`](#objecttypeconstructor-name)
- [`object.requiredKeys(children)`](#objectrequiredkeyschildren)
- [`object.optionalKeys(children)`](#objectoptionalkeyschildren)
- [`string`](#string)
- [`string.insensitive()`](#stringinsensitive)
- [`string.min(limit, [encoding])`](#stringminlimit-encoding)
- [`string.max(limit, [encoding])`](#stringmaxlimit-encoding)
- [`string.creditCard()`](#stringcreditcard)
- [`string.length(limit, [encoding])`](#stringlengthlimit-encoding)
- [`string.regex(pattern, [name])`](#stringregexpattern-name)
- [`string.replace(pattern, replacement)`](#stringreplacepattern-replacement)
- [`string.alphanum()`](#stringalphanum)
- [`string.token()`](#stringtoken)
- [`string.email([options])`](#stringemailoptions)
- [`string.ip([options])`](#stringipoptions)
- [`string.uri([options])`](#stringurioptions)
- [`string.guid()`](#stringguid)
- [`string.hex()`](#stringhex)
- [`string.hostname()`](#stringhostname)
- [`string.lowercase()`](#stringlowercase)
- [`string.uppercase()`](#stringuppercase)
- [`string.trim()`](#stringtrim)
- [`string.isoDate()`](#stringisodate)
- [`alternatives`](#alternatives)
- [`alternatives.try(schemas)`](#alternativestryschemas)
- [`alternatives.when(ref, options)`](#alternativeswhenref-options)
- [`ref(key, [options])`](#refkey-options)
<!-- tocstop -->
# Example
```javascript
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:
```javascript
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:
```javascript
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:
```javascript
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:
```javascript
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.
### `validate(val
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
elasticsearch sense plugin (957个子文件)
sense.light.css 754B
navbar.html 2KB
settings.html 2KB
help.html 1KB
welcome.html 1KB
index.html 1KB
history.html 958B
favicon.ico 4KB
ace.js 591KB
moment-with-locales.js 432KB
index.js 402KB
locales.js 311KB
moment-with-locales.min.js 220KB
locales.min.js 165KB
moment.js 136KB
string.js 114KB
zero_clipboard.js 85KB
worker-json.js 66KB
index.js 66KB
index.js 66KB
isemail.js 63KB
moment.min.js 57KB
ext-language_tools.js 56KB
index.js 47KB
worker.js 45KB
any.js 42KB
object.js 35KB
autocomplete.js 31KB
number.js 26KB
array.js 25KB
minimatch.js 25KB
ui-bootstrap-custom.js 25KB
index.js 24KB
index.js 24KB
mode-json.js 23KB
any.js 23KB
object.js 19KB
editor.js 18KB
index.js 16KB
alternatives.js 15KB
ext-searchbox.js 14KB
string.js 14KB
query.js 12KB
body_completer.js 12KB
day-of-week.js 12KB
array.js 11KB
date.js 11KB
query.js 11KB
errors.js 11KB
ref.js 11KB
engine.js 10KB
aggregations.js 10KB
mode-yaml.js 9KB
template.js 9KB
month.js 9KB
ru.js 9KB
index.js 8KB
kb.js 8KB
ru.js 8KB
aggregations.js 8KB
mappings.js 8KB
mappings.js 7KB
errors.js 7KB
proxy_config.js 7KB
input.js 7KB
cs.js 7KB
offset.js 6KB
sl.js 6KB
index.js 6KB
mr.js 6KB
mappings.js 6KB
uk.js 6KB
index.js 6KB
search.js 6KB
document.js 6KB
cs.js 6KB
sk.js 6KB
rfc3986.js 6KB
sl.js 6KB
be.js 5KB
lodash.js 5KB
mr.js 5KB
search.js 5KB
debounce.js 5KB
language.js 5KB
document.js 5KB
prototype.js 5KB
locales.js 5KB
ta.js 5KB
uk.js 5KB
index.js 5KB
curl.js 5KB
filter.js 5KB
bo.js 5KB
ar.js 5KB
hr.js 5KB
binary.js 5KB
sk.js 5KB
bs.js 5KB
lb.js 5KB
共 957 条
- 1
- 2
- 3
- 4
- 5
- 6
- 10
资源评论
- 萧萧雨歇-2018-06-12下载下来不会用,可能就不是sense
hxpjava1
- 粉丝: 1343
- 资源: 50
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功