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) {
{{? 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 }}

View File

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

View File

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

View File

@ -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{{?}}
});

View File

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

View File

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

View File

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

View File

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