是... 我的... json... 验证
临时叉从 是... 我的... json... 验证 以获得我的修补
A 类 JSON计划 使用代码生成的验证符 {\fn黑体\fs22\bord1\shad0\3ahbe\4ah00\fscx67\fscy66\2chffffff\3ch808080}快一点
npm install is-my-json-valid
它通过整个 JSONSchema v4测试套房 除了 remoteRefs 财务报告和审定财务报表 maxLength编号 :minLength 当使用 unicode 代对时.
使用量
只要通过一个计划来编译
var validator = require('is-my-json-valid')
var validate = validator({
required: true,
type: 'object',
properties: {
hello: {
required: true,
type: 'string'
}
}
})
console.log('should be valid', validate({hello: 'world'}))
console.log('should not be valid', validate({}))
// get the last list of errors by checking validate.errors
// the following will print [{field: 'data.hello', message: 'is required'}]
console.log(validate.errors)
也可以通过这个图案作为字符串
var validate = validator('{"type": ... }')
可以选择使用所需的子模块从中加载一个 schema __dirname
var validator = require('is-my-json-valid/require')
var validate = validator('my-schema.json')
自定义格式
is-my-json-valid 支持 JSON schema v4(例如日期-时间)中指定的格式. 如果您想要添加自定义格式, 将会随格式选项传入验证器
var validate = validator({
type: 'string',
required: true,
format: 'only-a'
}, {
formats: {
'only-a': /^a+$/
}
})
console.log(validate('aa')) // true
console.log(validate('ab')) // false
外部计划
你可以通过外部计划,你引用 $ref 属性为 schemas 选项
var ext = {
required: true,
type: 'string'
}
var schema = {
$ref: '#ext' // references another schema called ext
}
// pass the external schemas as an option
var validate = validator(schema, {schemas: {ext: ext}})
validate('hello') // returns true
validate(42) // return false
过滤去除其它属性
is-my-json-valid 支持过滤去除不在计划内的属性
var filter = validator.filter({
required: true,
type: 'object',
properties: {
hello: {type: 'string', required: true}
},
additionalProperties: false
})
var doc = {hello: 'world', notInSchema: true}
console.log(filter(doc)) // {hello: 'world'}
Verbose 模式显示更多关于出错源的信息
当 verbose 选项设置为 true, (中文(简体) ), is-my-json-valid 产出:
value: 导致错误的数据值schemaPath: 显示哪个子schema失败的密钥数组
var schema = {
required: true,
type: 'object',
properties: {
hello: {
required: true,
type: 'string'
}
}
}
var validate = validator(schema, {
verbose: true
})
validate({hello: 100});
console.log(validate.errors)
// [ { field: 'data.hello',
// message: 'is the wrong type',
// value: 100,
// type: 'string',
// schemaPath: [ 'properties', 'hello' ] } ]
许多受欢迎的图书馆 都很容易检索失败的规则 schemaPath数字 :
var schemaPath = validate.errors[0].schemaPath
var R = require('ramda')
console.log( 'All evaluate to the same thing: ', R.equals(
schema.properties.hello,
{ required: true, type: 'string' },
R.path(schemaPath, schema),
require('lodash').get(schema, schemaPath),
require('jsonpointer').get(schema, [""].concat(schemaPath))
))
// All evaluate to the same thing: true
贪婪模式尽量验证
默认是... 我的Json - 有效的保释 在第一个验证错误,但当贪婪 设定为真实, 尽量验证:
var validate = validator({
type: 'object',
properties: {
x: {
type: 'number'
}
},
required: ['x', 'y']
}, {
greedy: true
});
validate({x: 'string'});
console.log(validate.errors) // [{field: 'data.y', message: 'is required'},
// {field: 'data.x', message: 'is the wrong type'}]
错误消息
这是一份可能的列表 message 错误值 :
is requiredis the wrong typehas additional itemsmust be FORMAT format(格式为format从计划属性)must be uniquemust be an enum valuedependencies not sethas additional propertiesreferenced schema does not matchnegative schema matchespattern mismatchno schemas matchno (or more than one) schemas matchhas a remainderhas more properties than allowedhas less properties than allowedhas more items than allowedhas less items than allowedhas longer length than allowedhas less length than allowedis less than minimumis more than maximum
业绩
is-my-json-valid 使用代码生成将您的 JSON schema 转换为基本的 Javascript 代码,这些代码很容易被v8优化.
在撰写本报告时,是 快速验证器 运行时
如果你知道任何其他相关基准 打开公关,我会添加它们.
许可证
麻省理工学院