From ed4130f0317a72bebfad56042e63892d61e91623 Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin Date: Mon, 1 Jun 2015 18:15:11 +0100 Subject: [PATCH] inline items --- lib/compile/index.js | 6 +- lib/compile/rules/index.js | 3 +- lib/compile/rules/items.dot.js | 134 ++++++++++++++++++++------------- lib/compile/validate.dot.js | 4 +- package.json | 2 +- 5 files changed, 86 insertions(+), 63 deletions(-) diff --git a/lib/compile/index.js b/lib/compile/index.js index 610206a..3a7608a 100644 --- a/lib/compile/index.js +++ b/lib/compile/index.js @@ -7,9 +7,9 @@ var doT = require('dot') , resolve = require('./resolve'); var RULES = require('./rules') - , validateGenerator = require('./validate'); - // , validateTemplateStr = fs.readFileSync(__dirname + '/validate.dot.js') - // , validateTemplate = doT.compile(validateTemplateStr); + // , validateGenerator = require('./validate'); + , validateTemplateStr = fs.readFileSync(__dirname + '/validate.dot.js') + , validateGenerator = doT.compile(validateTemplateStr); module.exports = compile; diff --git a/lib/compile/rules/index.js b/lib/compile/rules/index.js index 65a1e87..48cc462 100644 --- a/lib/compile/rules/index.js +++ b/lib/compile/rules/index.js @@ -10,8 +10,7 @@ var RULES = module.exports = [ { type: 'string', inline: [ 'maxLength', 'minLength', 'pattern', 'format' ] }, { type: 'array', - inline: [ 'maxItems', 'minItems', 'uniqueItems' ], - func: [ 'items' ] }, + inline: [ 'maxItems', 'minItems', 'uniqueItems', 'items' ] }, { type: 'object', inline: [ 'maxProperties', 'minProperties', 'required', 'properties' ], func: [ 'dependencies' ] }, diff --git a/lib/compile/rules/items.dot.js b/lib/compile/rules/items.dot.js index 89f30bf..49d836d 100644 --- a/lib/compile/rules/items.dot.js +++ b/lib/compile/rules/items.dot.js @@ -1,69 +1,95 @@ +{{## def.ifValid: + {{? $breakOnError }} + {{ $closingBraces += '}'; }} + if (valid) { + {{?}} +#}} + {{## def.validateItems:startFrom: for (var i = {{= startFrom }}; i < data.length; i++) { var _data = data[i] , _dataPath = dataPath + '[' + i + ']'; - {{? !it.opts.allErrors }} var valid = {{?}} + {{? $breakOnError }} valid = {{?}} validateItems(_data, _dataPath); - {{? !it.opts.allErrors }} if (!valid) return false; {{?}} + {{? $breakOnError }} if (!valid) break; {{?}} } #}} -function (data, dataPath) { - {{? it.opts.allErrors }} var errs = validate.errors.length; {{?}} +{{ + var $it = it.copy(it) + , $breakOnError = !it.opts.allErrors + , $closingBraces = '' + , $itemsSchema = it.schema.items; +}} - {{? Array.isArray(it.schema) }} - {{ var $additionalItems = it.parentSchema.additionalItems; }} - {{? $additionalItems === false }} - if (data.length > {{= it.schema.length }}) { - validate.errors.push({ - keyword: 'additionalItems', - dataPath: dataPath, - message: 'should NOT have more than {{= it.schema.length }} items' - {{? it.opts.verbose }}, schema: false, data: data{{?}} - }); +var items_errs = validate.errors.length; +var valid; - {{? !it.opts.allErrors }} return false {{?}} - } +{{? Array.isArray($itemsSchema) }} + {{ var $additionalItems = it.schema.additionalItems; }} + {{? $additionalItems === false }} + valid = data.length <= {{= $itemsSchema.length }}; + if (!valid) { + validate.errors.push({ + keyword: 'additionalItems', + dataPath: dataPath, + message: 'should NOT have more than {{= $itemsSchema.length }} items' + {{? it.opts.verbose }}, schema: false, data: data{{?}} + }); + } + {{? $breakOnError }} + {{ $closingBraces += '}'; }} + else { {{?}} - - {{~ it.schema:$schema:$index }} - {{? Object.keys($schema).length }} - if (data.length > {{= $index }}) { - {{ - var $it = it.copy(it); - $it.schema = $schema; - $it.schemaPath = it.schemaPath + '[' + $index + ']'; - }} - - var _data = data[{{= $index }}] - , _dataPath = dataPath + '[{{= $index }}]'; - - {{? !it.opts.allErrors }} var valid = {{?}} - ({{= it.validate($it) }})(_data, _dataPath); - - {{? !it.opts.allErrors }} if (!valid) return false; {{?}} - } - {{?}} - {{~}} - - {{? typeof $additionalItems == 'object' && Object.keys($additionalItems).length }} - {{ - var $it = it.copy(it); - $it.schema = $additionalItems; - $it.schemaPath = it.parentSchemaPath + '.additionalItems'; - }} - - if (data.length > {{= it.schema.length }}) { - var validateItems = ({{= it.validate($it) }}); - {{# def.validateItems: it.schema.length }} - } - {{?}} - {{?? Object.keys(it.schema).length }} - var validateItems = ({{= it.validate(it) }}); - {{# def.validateItems: 0 }} {{?}} - return {{? it.opts.allErrors }} errs == validate.errors.length {{??}} true {{?}}; -} + {{~ $itemsSchema:$schema:$index }} + {{? Object.keys($schema).length }} + valid = true; + if (data.length > {{= $index }}) { + {{ + $it.schema = $schema; + $it.schemaPath = it.schemaPath + '.items[' + $index + ']'; + }} + + var _data = data[{{= $index }}] + , _dataPath = dataPath + '[{{= $index }}]'; + + {{? $breakOnError }} valid = {{?}} + ({{= it.validate($it) }})(_data, _dataPath); + } + + {{# def.ifValid }} + {{?}} + {{~}} + + {{? typeof $additionalItems == 'object' && Object.keys($additionalItems).length }} + {{ + $it.schema = $additionalItems; + $it.schemaPath = it.schemaPath + '.additionalItems'; + }} + + if (data.length > {{= $itemsSchema.length }}) { + var validateItems = ({{= it.validate($it) }}); + {{# def.validateItems: $itemsSchema.length }} + } + + {{# def.ifValid }} + {{?}} + +{{?? Object.keys($itemsSchema).length }} + {{ + $it.schema = $itemsSchema; + $it.schemaPath = it.schemaPath + '.items'; + }} + var validateItems = ({{= it.validate($it) }}); + {{# def.validateItems: 0 }} + + {{# def.ifValid }} +{{?}} + +{{? $breakOnError }} {{= $closingBraces }} {{?}} + +valid = items_errs == validate.errors.length; diff --git a/lib/compile/validate.dot.js b/lib/compile/validate.dot.js index f20eebe..0e30a8c 100644 --- a/lib/compile/validate.dot.js +++ b/lib/compile/validate.dot.js @@ -9,7 +9,7 @@ * validateRef etc. are defined in the parent scope in index.js */ }} -function (data{{? !it.isRoot }}, dataPath{{?}}) { +function ( data {{? !it.isRoot }}, dataPath {{?}}) { 'use strict'; {{? it.isRoot }} @@ -32,8 +32,6 @@ function (data{{? !it.isRoot }}, dataPath{{?}}) { var $it = it.copy(it); $it.schema = it.schema[$rule.keyword]; $it.schemaPath = it.schemaPath + '.' + $rule.keyword; - $it.parentSchema = it.schema; - $it.parentSchemaPath = it.schemaPath; }} {{? !it.opts.allErrors }} var valid = {{?}} ({{= $rule.code($it) }})(data, dataPath); diff --git a/package.json b/package.json index f8dfa40..c1addf8 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ajv", - "version": "0.1.8", + "version": "0.1.9", "description": "Another JSON schema Validator", "main": "lib/ajv.js", "scripts": {