inline items

master
Evgeny Poberezkin 2015-06-01 18:15:11 +01:00
parent 8f48bb1392
commit ed4130f031
5 changed files with 86 additions and 63 deletions

View File

@ -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;

View File

@ -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' ] },

View File

@ -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;

View File

@ -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);

View File

@ -1,6 +1,6 @@
{
"name": "ajv",
"version": "0.1.8",
"version": "0.1.9",
"description": "Another JSON schema Validator",
"main": "lib/ajv.js",
"scripts": {