is-my-json-valid
Advanced tools
Comparing version 2.0.3 to 2.1.0
28
index.js
var genobj = require('generate-object-property') | ||
var genfun = require('generate-function') | ||
var jsonpointer = require('jsonpointer') | ||
var xtend = require('xtend') | ||
var formats = require('./formats') | ||
var a = function(type) { | ||
switch (type) { | ||
case 'array': | ||
case 'object': | ||
case 'integer': | ||
return 'an '+type | ||
default: | ||
return 'a '+type | ||
} | ||
} | ||
var get = function(obj, ptr) { | ||
@@ -95,4 +84,5 @@ if (/^https?:\/\//.test(ptr)) return null | ||
var compile = function(schema, cache, root, reporter) { | ||
var scope = {unique:unique, formats:formats} | ||
var compile = function(schema, cache, root, reporter, opts) { | ||
var fmts = opts ? xtend(formats, opts.formats) : formats | ||
var scope = {unique:unique, formats:fmts} | ||
@@ -191,5 +181,5 @@ var syms = {} | ||
if (node.format && formats[node.format]) { | ||
if (node.format && fmts[node.format]) { | ||
var n = gensym('format') | ||
scope[n] = formats[node.format] | ||
scope[n] = fmts[node.format] | ||
@@ -296,3 +286,3 @@ validate('if (!%s.test(%s)) {', n, name) | ||
} | ||
fn = compile(sub, cache, root, false) | ||
fn = compile(sub, cache, root, false, opts) | ||
} | ||
@@ -531,5 +521,5 @@ var n = gensym('ref') | ||
module.exports = function(schema) { | ||
module.exports = function(schema, opts) { | ||
if (typeof schema === 'string') schema = JSON.parse(schema) | ||
return compile(schema, {}, schema, true) | ||
return compile(schema, {}, schema, true, opts) | ||
} |
{ | ||
"name": "is-my-json-valid", | ||
"version": "2.0.3", | ||
"version": "2.1.0", | ||
"description": "A JSONSchema validator that uses code generation to be extremely fast", | ||
@@ -9,3 +9,4 @@ "main": "index.js", | ||
"generate-object-property": "^1.1.0", | ||
"jsonpointer": "^1.1.0" | ||
"jsonpointer": "^1.1.0", | ||
"xtend": "^4.0.0" | ||
}, | ||
@@ -12,0 +13,0 @@ "devDependencies": { |
@@ -51,6 +51,22 @@ # is-my-json-valid | ||
var validate = validator('my-schema.json') | ||
``` | ||
// or load a orderly schema | ||
## Custom formats | ||
var validate = validator('my-orderly-schema.schema') | ||
is-my-json-valid supports the formats specified in JSON schema v4 (such as date-time). | ||
If you want to add your own custom formats pass them as the formats options to the validator | ||
``` js | ||
var validate = validator({ | ||
type: 'string', | ||
required: true, | ||
format: 'only-a' | ||
}, { | ||
formats: { | ||
'only-a': /^a+$/ | ||
} | ||
}) | ||
console.log(validate('aa')) // true | ||
console.log(validate('ab')) // false | ||
``` | ||
@@ -57,0 +73,0 @@ |
@@ -135,2 +135,21 @@ var tape = require('tape') | ||
tape('custom format', function(t) { | ||
var validate = validator({ | ||
type: 'object', | ||
properties: { | ||
foo: { | ||
type: 'string', | ||
format: 'as' | ||
} | ||
} | ||
}, {formats: {as:/^a+$/}}) | ||
t.notOk(validate({foo:''}), 'not as') | ||
t.notOk(validate({foo:'b'}), 'not as') | ||
t.notOk(validate({foo:'aaab'}), 'not as') | ||
t.ok(validate({foo:'a'}), 'as') | ||
t.ok(validate({foo:'aaaaaa'}), 'as') | ||
t.end() | ||
}) | ||
var files = fs.readdirSync(__dirname+'/json-schema-draft4') | ||
@@ -137,0 +156,0 @@ .map(function(file) { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
91999
2959
98
4
+ Addedxtend@^4.0.0
+ Addedxtend@4.0.2(transitive)