A simple data validator for JavaScript
-
npm:
npm install navy-schema
-
yarn:
yarn add navy-schema
import { Navy } from 'navy-schema'
const schema = Navy.object()
.keys({
username: Navy.string()
.alphanum()
.min(6)
.max(18),
password: Navy.string()
.regexp(/^[a-z0-9_]{8, 20}$/i),
repeat_password: Navy.ref('password'),
access_token: Navy.array()
.items([
Navy.string().required(),
Navy.number().required()
])
})
.optional({
birthday: Navy.date().required(),
email: Navy.string().mail()
})
const data = {
username: 'username001',
password: 'password',
repeat_password: 'password',
access_token: ['access_token', 1024, 2048]
}
const another = Object.assign({
birthday: '2020-02-02',
email: 'example@mail.com'
}, data)
// -> true
schema.validate(data)
// -> true
schema.validate(another)
// -> false
schema.validate({})
对指定数据进行校验,并返回一个 Promise
对象
对指定数据进行校验,并返回布尔值
schema.validate
在v1.1.0
后的版本移除,schema.validateSync
更名为schema.validate
向 Navy
添加钩子,在检验后调用;替换原本的 schema.validate
函数
-
status
- 检验结果,值为passed
|failed
-
names
- 需要添加钩子的规则名,值为string
类型或包含string
类型的数组 -
hook
- 钩子函数,会按顺序传入规则函数的参数ref
和schema.validate
函数的参数val
// Example
import { Navy } from 'navy-schema'
let i = 0, j = 0, k = 0
const schema = Navy.object()
.keys({
username: Navy.string()
.alphanum()
.effect('passed', 'alphanum', () => i = 1),
password: Navy.string()
.regexp(/^[a-z0-9_]{8, 20}$/i)
.effect('passed', 'regexp', () => j = 1),
repeat_password: Navy.ref('password'),
})
.effect('passed', 'keys', () => k = 1)
const data = {
username: 'username001',
password: 'password',
repeat_password: 'password',
}
// -> true
schema.validate(data)
// -> 1, 1, 1
console.log(i, j, k)
指定该值任意类型且不为空
指定该值为 undefined
或 null
指定该值与参照对象完全一致
-
ref
-any
,参照对象
指定该值必须为 val == true
指定该值必须为 val == false
指定该值为 number
类型且不为空
指定该值为 number
类型且大于参照对象
-
ref
-number
,参照对象
指定该值为 number
类型且小于参照对象
-
ref
-number
,参照对象
指定该值为 number
类型且小于等于参照对象
-
ref
-number
,参照对象
指定该值为 number
类型且大于等于参照对象
-
ref
-number
,参照对象
指定该值为 number
类型且等于参照对象
-
ref
-number
,参照对象
指定该值为 number
类型且为整数
指定该值为 number
类型且为参照对象的倍数
-
ref
-number
,参照对象
指定该值为 number
类型且为参照对象的因数
-
ref
-number
,参照对象
指定该值为 number
类型且为正数
指定该值为 number
类型且为负数
指定该值为 string
类型且不为空
指定该值为 string
类型且符合正则表达式
-
ref
-RegExp
,正则表达式
指定该值为 string
类型且包含参照对象
-
ref
-string
,参照对象
指定该值为 string
类型且与参照对象相等
-
ref
-string
,参照对象
指定该值为 string
类型且字符串长度小于等于参照对象
-
ref
-number
,参照对象
指定该值为 string
类型且字符串长度大于等于参照对象
-
ref
-number
,参照对象
指定该值为 string
类型且字符串长度等于参照对象
-
ref
-number
,参照对象
指定该值为 string
类型且只有数字组成
指定该值为 string
类型且只有字母组成
指定该值为 string
类型且只有字母和数字组成
指定该值为 string
类型且为合法 URL
地址
指定该值为 string
类型且为合法电子邮箱
指定该值为 string
类型且符合中华人民共和国手机号段规则
指定该值为 string
类型且符合中华人民共和国身份证号码验证规则
指定该值为 ISOString
、number
或 Date
类型且不为空
指定该值为 ISOString
、number
或 Date
类型且日期在参照对象之后
-
ref
-ISOString
、number
或Date
,参照对象
指定该值为 ISOString
、number
或 Date
类型且日期在参照对象之前
-
ref
-ISOString
、number
或Date
,参照对象
指定该值为 ISOString
、number
或 Date
类型且日期等于参照对象
-
ref
-ISOString
、number
或Date
,参照对象
指定该值为 ISOString
、number
或 Date
类型且日期在参照对象之前(包含参照对象)
-
ref
-ISOString
、number
或Date
,参照对象
指定该值为 ISOString
、number
或 Date
类型且日期在参照对象之后(包含参照对象)
-
ref
-ISOString
、number
或Date
,参照对象
指定该值为 array
类型且不为空
指定该值为 array
类型且数组长度小于等于参照对象
-
ref
-number
,参照对象
指定该值为 array
类型且数组长度大于等于参照对象
-
ref
-number
,参照对象
指定该值为 array
类型且数组长度等于参照对象
-
ref
-number
,参照对象
指定该值为 array
类型且数组内的值符合参照数组中的任意规则
-
ref
-schema[]
,值为Navy
实例的数组
指定该值为 array
类型且数组内的值符合参照对象规则
-
ref
-schema
,Navy
实例
指定该值为 object
类型且不为空
指定该值为 object
类型且键值符合参照对象规则
-
ref
-object<string, schema>
,参照对象
/** keys 和 optional 方法会根据调用顺序覆盖冲突的键值 */
指定该值为 object
类型,该值可以为未定义或符合对象规则
-
ref
-object<string, schema>
,参照对象
/** keys 和 optional 方法会根据调用顺序覆盖冲突的键值 */
创建一个值的引用对象,其他 Navy
实例可以使用引用对象作为参照对象。这个方法只能在 schema<object>
中使用
-
key
-string
,需要引用的键名 -
ancestor
-number
,向父对象偏移的次数,每有一代object
则偏移量加一
// Example
import { Navy } from 'navy-schema'
const schema = Navy.object()
.keys({
a: Navy.any().equal('luke'),
b: Navy.any().equal(Navy.ref('a')),
c: Navy.ref('a'),
d: {
e: Navy.ref('a', 1)
}
})
// -> true
schema.validate({ a: 'luke', b: 'luke', c: 'luke', d: { e: 'luke' } })