From ddf192e17259364fb972882166e1ebd7c7959761 Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin Date: Sun, 31 May 2015 21:46:25 +0100 Subject: [PATCH] inlined not, anyOf, oneOf, allOf --- lib/compile/rules/allOf.dot.js | 30 +++++++++-------- lib/compile/rules/anyOf.dot.js | 35 +++++++++++--------- lib/compile/rules/index.js | 3 +- lib/compile/rules/not.dot.js | 28 ++++++++-------- lib/compile/rules/oneOf.dot.js | 60 ++++++++++++++++------------------ package.json | 2 +- spec/compile.spec.js | 7 ++-- spec/json-schema.spec.js | 8 +++-- 8 files changed, 90 insertions(+), 83 deletions(-) diff --git a/lib/compile/rules/allOf.dot.js b/lib/compile/rules/allOf.dot.js index 81edfe0..363bb08 100644 --- a/lib/compile/rules/allOf.dot.js +++ b/lib/compile/rules/allOf.dot.js @@ -1,18 +1,20 @@ -function (data, dataPath) { - {{? it.opts.allErrors }} var errs = validate.errors.length; {{?}} +var valid = true; - {{~ it.schema:$schema:$i }} - {{ - var $it = it.copy(it); - $it.schema = $schema; - $it.schemaPath = it.schemaPath + '[' + $i + ']'; - }} +{{ var $closingBraces = ''; }} +{{ var $it = it.copy(it); }} - {{? !it.opts.allErrors }} var valid = {{?}} - ({{= it.validate($it) }})(data, dataPath); +{{~ it.schema.allOf:$schema:$i }} + {{? !it.opts.allErrors && $i }} + {{ $closingBraces += '}'; }} + if (valid) { + {{?}} - {{? !it.opts.allErrors }} if (!valid) return false; {{?}} - {{~}} + {{ + $it.schema = $schema; + $it.schemaPath = it.schemaPath + '.allOf[' + $i + ']'; + }} - return {{? it.opts.allErrors }} errs == validate.errors.length {{??}} true {{?}}; -} + valid = valid && ({{= it.validate($it) }})(data, dataPath); +{{~}} + +{{= $closingBraces }} diff --git a/lib/compile/rules/anyOf.dot.js b/lib/compile/rules/anyOf.dot.js index 498b5ad..3615be6 100644 --- a/lib/compile/rules/anyOf.dot.js +++ b/lib/compile/rules/anyOf.dot.js @@ -1,20 +1,23 @@ -function (data, dataPath) { - var errs = validate.errors.length; +var anyOf_errs = validate.errors.length; - {{~ it.schema:$schema:$i }} - {{ - var $it = it.copy(it); - $it.schema = $schema; - $it.schemaPath = it.schemaPath + '[' + $i + ']'; - }} +var valid = false; +{{ var $closingBraces = ''; }} +{{~ it.schema.anyOf:$schema:$i }} + {{? $i }} + {{ $closingBraces += '}'; }} + if (!valid) { + {{?}} - var valid = ({{= it.validate($it) }})(data, dataPath); + {{ + var $it = it.copy(it); + $it.schema = $schema; + $it.schemaPath = it.schemaPath + '.anyOf[' + $i + ']'; + }} - if (valid) { - validate.errors.length = errs; - return true; - } - {{~}} + valid = valid || ({{= it.validate($it) }})(data, dataPath); - return false; -} +{{~}} + +{{= $closingBraces }} + +if (valid) validate.errors.length = anyOf_errs; diff --git a/lib/compile/rules/index.js b/lib/compile/rules/index.js index 65b519f..b74c840 100644 --- a/lib/compile/rules/index.js +++ b/lib/compile/rules/index.js @@ -15,8 +15,7 @@ var RULES = module.exports = [ { type: 'object', inline: [ 'maxProperties', 'minProperties', 'required' ], func: [ 'dependencies', 'properties' ] }, - { inline: [ '$ref', 'type', 'enum' ], - func: [ 'not', 'anyOf', 'oneOf', 'allOf' ] } + { inline: [ '$ref', 'type', 'enum', 'not', 'anyOf', 'oneOf', 'allOf' ] } ]; diff --git a/lib/compile/rules/not.dot.js b/lib/compile/rules/not.dot.js index 214219b..ddd4375 100644 --- a/lib/compile/rules/not.dot.js +++ b/lib/compile/rules/not.dot.js @@ -1,16 +1,18 @@ -function (data, dataPath) { - var errs = validate.errors.length; +var not_errs = validate.errors.length; - var valid = ({{= it.validate(it) }})(data, dataPath); - valid = !valid; +{{ + var $it = it.copy(it); + $it.schema = it.schema.not; + $it.schemaPath = it.schemaPath + '.not'; +}} - if (valid) validate.errors.length = errs; - else validate.errors.push({ - keyword: 'not', - dataPath: dataPath, - message: 'should NOT be valid' - {{? it.opts.verbose }}, schema: validate.schema{{= it.schemaPath }}, data: data{{?}} - }); +var valid = ({{= it.validate($it) }})(data, dataPath); +valid = !valid; - return valid; -} +if (valid) validate.errors.length = not_errs; +else validate.errors.push({ + keyword: 'not', + dataPath: dataPath, + message: 'should NOT be valid' + {{? it.opts.verbose }}, schema: validate.schema{{= it.schemaPath + '.not' }}, data: data{{?}} +}); diff --git a/lib/compile/rules/oneOf.dot.js b/lib/compile/rules/oneOf.dot.js index e88fb20..dca69cf 100644 --- a/lib/compile/rules/oneOf.dot.js +++ b/lib/compile/rules/oneOf.dot.js @@ -1,37 +1,33 @@ -function (data, dataPath) { - var foundValid = false - , errs = validate.errors.length; +var foundValid = false + , oneOf_errs = validate.errors.length; - {{~ it.schema:$schema:$i }} - {{ - var $it = it.copy(it); - $it.schema = $schema; - $it.schemaPath = it.schemaPath + '[' + $i + ']'; - }} +var validCount = 0; +{{ var $closingBraces = ''; }} +{{ var $it = it.copy(it); }} - var valid = ({{= it.validate($it) }})(data, dataPath); +{{~ it.schema.oneOf:$schema:$i }} + {{? $i }} + {{ $closingBraces += '}'; }} + if (validCount < 2) { + {{?}} - if (valid) { - if (foundValid) { - validate.errors.push({ - keyword: 'oneOf', - dataPath: dataPath, - message: 'should match exactly one schema in oneOf' - {{? it.opts.verbose }}, schema: validate.schema{{= it.schemaPath }}, data: data{{?}} - }); - return false; - } - foundValid = true; - } - {{~}} + {{ + $it.schema = $schema; + $it.schemaPath = it.schemaPath + '.oneOf[' + $i + ']'; + }} - if (foundValid) validate.errors.length = errs; - else validate.errors.push({ - keyword: 'oneOf', - dataPath: dataPath, - message: 'should match exactly one schema in oneOf' - {{? it.opts.verbose }}, schema: validate.schema{{= it.schemaPath }}, data: data{{?}} - }); + var valid = ({{= it.validate($it) }})(data, dataPath); + if (valid) validCount++; +{{~}} - return foundValid; -} +{{= $closingBraces }} + +if (validCount == 1) validate.errors.length = oneOf_errs; +else validate.errors.push({ + keyword: 'oneOf', + dataPath: dataPath, + message: 'should match exactly one schema in oneOf' + {{? it.opts.verbose }}, schema: validate.schema{{= it.schemaPath + '.oneOf'}}, data: data{{?}} +}); + +var valid = validCount == 1; diff --git a/package.json b/package.json index 56bccf9..dff7df7 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ajv", - "version": "0.1.6", + "version": "0.1.7", "description": "Another JSON schema Validator", "main": "lib/ajv.js", "scripts": { diff --git a/spec/compile.spec.js b/spec/compile.spec.js index 7c4bcd8..a76c104 100644 --- a/spec/compile.spec.js +++ b/spec/compile.spec.js @@ -2,10 +2,11 @@ var compileSchema = require('../lib/compile'); var mockInstance = { opts: {} }; +var assert = require('assert'); describe('Schema compilation', function() { - it.skip('works', function() { - var compiled = compileSchema.call(mockInstance, { type: 'string' }); - console.log(compiled.validate.toString()); + it('works', function() { + var validate = compileSchema.call(mockInstance, { type: 'string' }); + assert.equal(typeof validate, 'function'); }); }); diff --git a/spec/json-schema.spec.js b/spec/json-schema.spec.js index de042d8..8756b22 100644 --- a/spec/json-schema.spec.js +++ b/spec/json-schema.spec.js @@ -7,7 +7,11 @@ var glob = require('glob') var ONLY_RULES, SKIP_RULES; // ONLY_RULES = [ // 'type', -// 'not', 'allOf', 'anyOf', 'oneOf', 'enum', +// 'not', +// 'allOf', +// 'anyOf', +'oneOf', +// 'enum', // 'maximum', 'minimum', 'multipleOf', // 'maxLength', 'minLength', 'pattern', // 'properties', 'patternProperties', 'additionalProperties', @@ -61,7 +65,7 @@ describe('JSON-Schema tests', function () { var fullValidate = fullAjv.compile(testSet.schema); testSet.tests.forEach(function (test) { - // if (test.description != 'an integer is valid') return; + // if (test.description != 'mismatch base schema') return; it(test.description, function() { var valid = validate(test.data); // console.log('result', result);