ajv
Advanced tools
Comparing version 0.2.7 to 0.2.8
@@ -19,4 +19,6 @@ 'use strict'; | ||
function compile(schema) { | ||
var self = this; | ||
function compile(schema, _rootSchema) { | ||
var self = this, refs = [], refIds = {}; | ||
_rootSchema = _rootSchema || schema; | ||
var validateCode = validateGenerator({ | ||
@@ -36,3 +38,4 @@ isRoot: true, | ||
if (this.opts.beautify) { | ||
if (beautify) validateCode = beautify(validateCode, { indent_size: 2 }); | ||
var opts = this.opts.beautify === true ? { indent_size: 2 } : this.opts.beautify; | ||
if (beautify) validateCode = beautify(validateCode, opts); | ||
else console.error('"npm install js-beautify" to use beautify option'); | ||
@@ -50,10 +53,11 @@ } | ||
function resolveRef(ref) { | ||
return resolve.call(self, compile, schema, ref); | ||
if (refIds[ref]) return refIds[ref]; | ||
var v = resolve.call(self, compile, _rootSchema, ref); | ||
if (v) { | ||
var id = refs.length; | ||
refs.push(v); | ||
refIds[ref] = id; | ||
return id; | ||
} | ||
} | ||
function validateRef(ref, data) { | ||
var v = ref == '#' ? validate : self._schemas[ref]; | ||
var valid = v(data); | ||
return { valid: valid, errors: v.errors }; | ||
} | ||
} | ||
@@ -60,0 +64,0 @@ |
@@ -5,22 +5,21 @@ 'use strict'; | ||
module.exports = function resolve(compile, rootSchema, ref) { | ||
var schema = rootSchema; | ||
if (ref[0] != '#') | ||
schema = undefined; | ||
else if (ref != '#') { | ||
if (this._schemas[ref]) | ||
schema = this._schemas[ref]; | ||
else { | ||
var parts = ref.split('/'); | ||
for (var i = 1; i < parts.length; i++) { | ||
if (!schema) break; | ||
var part = unescape(parts[i]); | ||
schema = schema[part]; | ||
if (schema.$ref) | ||
schema = resolve.call(this, compile, rootSchema, schema.$ref); | ||
} | ||
if (schema) this._schemas[ref] = compile.call(this, schema); | ||
} | ||
if (ref[0] != '#') return; | ||
if (this._schemas[ref]) return this._schemas[ref]; | ||
var schema = _resolve(rootSchema, ref); | ||
if (schema) return this._schemas[ref] = compile.call(this, schema, rootSchema); | ||
}; | ||
function _resolve(rootSchema, ref) { | ||
var schema = rootSchema | ||
, parts = ref.split('/'); | ||
for (var i = 1; i < parts.length; i++) { | ||
if (!schema) break; | ||
var part = unescape(parts[i]); | ||
schema = schema[part]; | ||
if (schema.$ref) | ||
schema = _resolve(rootSchema, schema.$ref); | ||
} | ||
return schema; | ||
}; | ||
} | ||
@@ -27,0 +26,0 @@ |
{{# def.definitions }} | ||
{{# def.setup:'$ref' }} | ||
{{? it.resolveRef($schema) }} | ||
var result{{=$lvl}} = validateRef('{{=$schema}}', {{=$data}}, (dataPath || '') + {{= it.errorPath }}); | ||
var {{=$valid}} = result{{=$lvl}}.valid; | ||
if (!{{=$valid}}) validate.errors.push.apply(validate.errors, result{{=$lvl}}.errors); | ||
{{? $schema == '#' }} | ||
var {{=$valid}} = validate({{=$data}}, (dataPath || '') + {{= it.errorPath }}); | ||
{{??}} | ||
{{# def.error:'$ref' }} | ||
var {{=$valid}} = false; | ||
{{ $refId = it.resolveRef($schema); }} | ||
{{? $refId === undefined }} | ||
{{# def.error:'$ref' }} | ||
var {{=$valid}} = false; | ||
{{??}} | ||
var {{=$valid}} = refs[{{=$refId}}]({{=$data}}, (dataPath || '') + {{= it.errorPath }}); | ||
if (!{{=$valid}}) validate.errors.push.apply(validate.errors, refs[{{=$refId}}].errors); | ||
{{?}} | ||
{{?}} |
@@ -5,4 +5,4 @@ {{# def.definitions }} | ||
{{ new RegExp($schema); /* test if regexp is valid to fail at compile time rather than in eval */}} | ||
var {{=$valid}} = /{{=$schema}}/.test({{=$data}}); | ||
var {{=$valid}} = new RegExp("{{=$schema}}").test({{=$data}}); | ||
{{# def.checkError:'pattern' }} |
{ | ||
"name": "ajv", | ||
"version": "0.2.7", | ||
"version": "0.2.8", | ||
"description": "Another JSON schema Validator", | ||
@@ -5,0 +5,0 @@ "main": "lib/ajv.js", |
@@ -41,38 +41,38 @@ 'use strict'; | ||
describe('JSON-Schema tests', function () { | ||
addTests('draft4: ', './json-schema-test-suite/tests/draft4/{**/,}*.json'); | ||
addTests('JSON-Schema tests draft4', './json-schema-test-suite/tests/draft4/{**/,}*.json'); | ||
addTests('Advanced schema tests', './tests/{**/,}*.json'); | ||
function addTests(description, testsPath) { | ||
describe(description, function() { | ||
var files = getTestFiles(testsPath); | ||
files.forEach(function (file) { | ||
if (ONLY_RULES && ONLY_RULES.indexOf(file.name) == -1) return; | ||
if (SKIP_RULES && SKIP_RULES.indexOf(file.name) >= 0) return; | ||
function addTests(description, testsPath) { | ||
describe(description, function() { | ||
var files = getTestFiles(testsPath); | ||
describe(file.name, function() { | ||
var testSets = require(file.path); | ||
testSets.forEach(function (testSet) { | ||
// if (testSet.description != 'allOf with base schema') return; | ||
describe(testSet.description, function() { | ||
// it(testSet.description, function() { | ||
var validate = ajv.compile(testSet.schema); | ||
var fullValidate = fullAjv.compile(testSet.schema); | ||
files.forEach(function (file) { | ||
if (ONLY_RULES && ONLY_RULES.indexOf(file.name) == -1) return; | ||
if (SKIP_RULES && SKIP_RULES.indexOf(file.name) >= 0) return; | ||
testSet.tests.forEach(function (test) { | ||
// if (test.description != 'one supplementary Unicode code point is not long enough') return; | ||
// console.log(testSet.schema, '\n\n***\n\n', validate.toString()); | ||
it(test.description, function() { | ||
var valid = validate(test.data); | ||
// console.log('result', valid, validate.errors); | ||
assert.equal(valid, test.valid); | ||
if (valid) assert(validate.errors.length == 0); | ||
else assert(validate.errors.length > 0); | ||
describe(file.name, function() { | ||
var testSets = require(file.path); | ||
testSets.forEach(function (testSet) { | ||
// if (testSet.description != 'allOf with base schema') return; | ||
describe(testSet.description, function() { | ||
// it(testSet.description, function() { | ||
var validate = ajv.compile(testSet.schema); | ||
var fullValidate = fullAjv.compile(testSet.schema); | ||
var valid = fullValidate(test.data); | ||
// console.log('full result', valid, fullValidate.errors); | ||
assert.equal(valid, test.valid); | ||
if (valid) assert(fullValidate.errors.length == 0); | ||
else assert(fullValidate.errors.length > 0); | ||
}); | ||
testSet.tests.forEach(function (test) { | ||
// if (test.description != 'one supplementary Unicode code point is not long enough') return; | ||
// console.log(testSet.schema, '\n\n***\n\n', validate.toString()); | ||
it(test.description, function() { | ||
var valid = validate(test.data); | ||
// console.log('result', valid, validate.errors); | ||
assert.equal(valid, test.valid); | ||
if (valid) assert(validate.errors.length == 0); | ||
else assert(validate.errors.length > 0); | ||
var valid = fullValidate(test.data); | ||
// console.log('full result', valid, fullValidate.errors); | ||
assert.equal(valid, test.valid); | ||
if (valid) assert(fullValidate.errors.length == 0); | ||
else assert(fullValidate.errors.length > 0); | ||
}); | ||
@@ -84,4 +84,4 @@ }); | ||
}); | ||
} | ||
}); | ||
}); | ||
} | ||
@@ -88,0 +88,0 @@ |
203568
110
6173