inlined not, anyOf, oneOf, allOf

master
Evgeny Poberezkin 2015-05-31 21:46:25 +01:00
parent 99bec2b8c4
commit ddf192e172
8 changed files with 90 additions and 83 deletions

View File

@ -1,18 +1,20 @@
function (data, dataPath) { var valid = true;
{{? it.opts.allErrors }} var errs = validate.errors.length; {{?}}
{{~ it.schema:$schema:$i }} {{ var $closingBraces = ''; }}
{{ {{ var $it = it.copy(it); }}
var $it = it.copy(it);
$it.schema = $schema;
$it.schemaPath = it.schemaPath + '[' + $i + ']';
}}
{{? !it.opts.allErrors }} var valid = {{?}} {{~ it.schema.allOf:$schema:$i }}
({{= it.validate($it) }})(data, dataPath); {{? !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 }}

View File

@ -1,20 +1,23 @@
function (data, dataPath) { var anyOf_errs = validate.errors.length;
var errs = validate.errors.length;
{{~ it.schema:$schema:$i }} var valid = false;
{{ {{ var $closingBraces = ''; }}
var $it = it.copy(it); {{~ it.schema.anyOf:$schema:$i }}
$it.schema = $schema; {{? $i }}
$it.schemaPath = it.schemaPath + '[' + $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) { valid = valid || ({{= it.validate($it) }})(data, dataPath);
validate.errors.length = errs;
return true;
}
{{~}}
return false; {{~}}
}
{{= $closingBraces }}
if (valid) validate.errors.length = anyOf_errs;

View File

@ -15,8 +15,7 @@ var RULES = module.exports = [
{ type: 'object', { type: 'object',
inline: [ 'maxProperties', 'minProperties', 'required' ], inline: [ 'maxProperties', 'minProperties', 'required' ],
func: [ 'dependencies', 'properties' ] }, func: [ 'dependencies', 'properties' ] },
{ inline: [ '$ref', 'type', 'enum' ], { inline: [ '$ref', 'type', 'enum', 'not', 'anyOf', 'oneOf', 'allOf' ] }
func: [ 'not', 'anyOf', 'oneOf', 'allOf' ] }
]; ];

View File

@ -1,16 +1,18 @@
function (data, dataPath) { var not_errs = validate.errors.length;
var 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; var valid = ({{= it.validate($it) }})(data, dataPath);
else validate.errors.push({ valid = !valid;
keyword: 'not',
dataPath: dataPath,
message: 'should NOT be valid'
{{? it.opts.verbose }}, schema: validate.schema{{= it.schemaPath }}, data: data{{?}}
});
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{{?}}
});

View File

@ -1,37 +1,33 @@
function (data, dataPath) { var foundValid = false
var foundValid = false , oneOf_errs = validate.errors.length;
, errs = validate.errors.length;
{{~ it.schema:$schema:$i }} var validCount = 0;
{{ {{ var $closingBraces = ''; }}
var $it = it.copy(it); {{ var $it = it.copy(it); }}
$it.schema = $schema;
$it.schemaPath = it.schemaPath + '[' + $i + ']';
}}
var valid = ({{= it.validate($it) }})(data, dataPath); {{~ it.schema.oneOf:$schema:$i }}
{{? $i }}
{{ $closingBraces += '}'; }}
if (validCount < 2) {
{{?}}
if (valid) { {{
if (foundValid) { $it.schema = $schema;
validate.errors.push({ $it.schemaPath = it.schemaPath + '.oneOf[' + $i + ']';
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;
}
{{~}}
if (foundValid) validate.errors.length = errs; var valid = ({{= it.validate($it) }})(data, dataPath);
else validate.errors.push({ if (valid) validCount++;
keyword: 'oneOf', {{~}}
dataPath: dataPath,
message: 'should match exactly one schema in oneOf'
{{? it.opts.verbose }}, schema: validate.schema{{= it.schemaPath }}, data: data{{?}}
});
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;

View File

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

View File

@ -2,10 +2,11 @@
var compileSchema = require('../lib/compile'); var compileSchema = require('../lib/compile');
var mockInstance = { opts: {} }; var mockInstance = { opts: {} };
var assert = require('assert');
describe('Schema compilation', function() { describe('Schema compilation', function() {
it.skip('works', function() { it('works', function() {
var compiled = compileSchema.call(mockInstance, { type: 'string' }); var validate = compileSchema.call(mockInstance, { type: 'string' });
console.log(compiled.validate.toString()); assert.equal(typeof validate, 'function');
}); });
}); });

View File

@ -7,7 +7,11 @@ var glob = require('glob')
var ONLY_RULES, SKIP_RULES; var ONLY_RULES, SKIP_RULES;
// ONLY_RULES = [ // ONLY_RULES = [
// 'type', // 'type',
// 'not', 'allOf', 'anyOf', 'oneOf', 'enum', // 'not',
// 'allOf',
// 'anyOf',
'oneOf',
// 'enum',
// 'maximum', 'minimum', 'multipleOf', // 'maximum', 'minimum', 'multipleOf',
// 'maxLength', 'minLength', 'pattern', // 'maxLength', 'minLength', 'pattern',
// 'properties', 'patternProperties', 'additionalProperties', // 'properties', 'patternProperties', 'additionalProperties',
@ -61,7 +65,7 @@ describe('JSON-Schema tests', function () {
var fullValidate = fullAjv.compile(testSet.schema); var fullValidate = fullAjv.compile(testSet.schema);
testSet.tests.forEach(function (test) { testSet.tests.forEach(function (test) {
// if (test.description != 'an integer is valid') return; // if (test.description != 'mismatch base schema') return;
it(test.description, function() { it(test.description, function() {
var valid = validate(test.data); var valid = validate(test.data);
// console.log('result', result); // console.log('result', result);