added anyOf error, refactored test

master
Evgeny Poberezkin 2015-06-18 23:01:39 +01:00
parent 46812e0ac6
commit 8bfb3f9b4a
27 changed files with 63 additions and 52 deletions

View File

@ -9,6 +9,8 @@ var glob = require('glob')
var defs = fs.readFileSync(path.join(__dirname, '../lib/dot/definitions.def'));
var files = glob.sync('../lib/dot/*.jst', { cwd: __dirname });
console.log('\n\nCompiling:')
files.forEach(function (f) {
var template = fs.readFileSync(path.join(__dirname, f));
var code = doT.compile(template, { definitions: defs });

View File

@ -71,8 +71,7 @@ function compile(schema, root) {
return addLocalRef(ref, root.refVal[rootRefId]);
}
var v = resolve.call(self, compile, root, ref);
if (v)
return addLocalRef(ref, v);
if (v) return addLocalRef(ref, v);
}
function addLocalRef(ref, v) {

View File

@ -27,7 +27,9 @@
{{= $closingBraces }}
if ({{=$valid}}) {
if (!{{=$valid}}) {
{{# def.addError:'anyOf' }}
} else {
errors = {{=$errs}};
if (validate.errors !== null) {
if ({{=$errs}}) validate.errors.length = {{=$errs}};

View File

@ -100,15 +100,20 @@
#}}
{{## def.addError:_rule:
var err = {{# def._error:_rule }};
if (validate.errors === null) validate.errors = [err];
else validate.errors.push(err);
errors++;
#}}
{{## def.error:_rule:
{{? it.wasTop && $breakOnError }}
validate.errors = [{{# def._error:_rule }}];
return false;
{{??}}
var err = {{# def._error:_rule }};
if (validate.errors === null) validate.errors = [err];
else validate.errors.push(err);
errors++;
{{# def.addError:_rule }}
{{?}}
#}}
@ -124,6 +129,7 @@
$ref: "'can\\\'t resolve reference {{=$schema}}'",
additionalItems: "'should NOT have more than {{=$schema.length}} items'",
additionalProperties: "'additional properties NOT allowed'",
anyOf: "'should match some schema in anyOf'",
dependencies: "'{{? $deps.length == 1 }}property {{= $deps[0] }} is{{??}}properties {{= $deps.join(\", \") }} are{{?}} required when property {{= $property }} is present'",
enum: "'should be equal to one of values'",
format: "'should match format {{=$schema}}'",
@ -149,6 +155,7 @@
$ref: "'{{=$schema}}'",
additionalItems: "false",
additionalProperties: "false",
anyOf: "validate.schema{{=$schemaPath}}",
dependencies: "validate.schema{{=$schemaPath}}",
enum: "validate.schema{{=$schemaPath}}",
format: "'{{=$schema}}'",

View File

@ -41,7 +41,7 @@ module.exports = function anonymous(it) {
}
out += ' }]; return false; ';
} else {
out += ' var err = { keyword: \'' + ('$ref') + '\', dataPath: (dataPath || \'\') + ' + (it.errorPath) + ', message: \'can\\\'t resolve reference ' + ($schema) + '\' ';
out += ' var err = { keyword: \'' + ('$ref') + '\', dataPath: (dataPath || \'\') + ' + (it.errorPath) + ', message: \'can\\\'t resolve reference ' + ($schema) + '\' ';
if (it.opts.verbose) {
out += ', schema: \'' + ($schema) + '\', data: ' + ($data);
}

View File

@ -32,7 +32,11 @@ module.exports = function anonymous(it) {
$closingBraces += '}';
}
}
out += ' ' + ($closingBraces) + ' if (' + ($valid) + ') { errors = ' + ($errs) + '; if (validate.errors !== null) { if (' + ($errs) + ') validate.errors.length = ' + ($errs) + '; else validate.errors = null; } ';
out += ' ' + ($closingBraces) + ' if (!' + ($valid) + ') { var err = { keyword: \'' + ('anyOf') + '\', dataPath: (dataPath || \'\') + ' + (it.errorPath) + ', message: \'should match some schema in anyOf\' ';
if (it.opts.verbose) {
out += ', schema: validate.schema' + ($schemaPath) + ', data: ' + ($data);
}
out += ' }; if (validate.errors === null) validate.errors = [err]; else validate.errors.push(err); errors++; } else { errors = ' + ($errs) + '; if (validate.errors !== null) { if (' + ($errs) + ') validate.errors.length = ' + ($errs) + '; else validate.errors = null; } ';
if (it.opts.allErrors) {
out += ' } ';
}

View File

@ -53,7 +53,7 @@ module.exports = function anonymous(it) {
}
out += ' }]; return false; ';
} else {
out += ' var err = { keyword: \'' + ('dependencies') + '\', dataPath: (dataPath || \'\') + ' + (it.errorPath) + ', message: \'';
out += ' var err = { keyword: \'' + ('dependencies') + '\', dataPath: (dataPath || \'\') + ' + (it.errorPath) + ', message: \'';
if ($deps.length == 1) {
out += 'property ' + ($deps[0]) + ' is';
} else {

View File

@ -21,7 +21,7 @@ module.exports = function anonymous(it) {
}
out += ' }]; return false; ';
} else {
out += ' var err = { keyword: \'' + ('enum') + '\', dataPath: (dataPath || \'\') + ' + (it.errorPath) + ', message: \'should be equal to one of values\' ';
out += ' var err = { keyword: \'' + ('enum') + '\', dataPath: (dataPath || \'\') + ' + (it.errorPath) + ', message: \'should be equal to one of values\' ';
if (it.opts.verbose) {
out += ', schema: validate.schema' + ($schemaPath) + ', data: ' + ($data);
}

View File

@ -28,7 +28,7 @@ module.exports = function anonymous(it) {
}
out += ' }]; return false; ';
} else {
out += ' var err = { keyword: \'' + ('format') + '\', dataPath: (dataPath || \'\') + ' + (it.errorPath) + ', message: \'should match format ' + ($schema) + '\' ';
out += ' var err = { keyword: \'' + ('format') + '\', dataPath: (dataPath || \'\') + ' + (it.errorPath) + ', message: \'should match format ' + ($schema) + '\' ';
if (it.opts.verbose) {
out += ', schema: \'' + ($schema) + '\', data: ' + ($data);
}

View File

@ -29,7 +29,7 @@ module.exports = function anonymous(it) {
}
out += ' }]; return false; ';
} else {
out += ' var err = { keyword: \'' + ('additionalItems') + '\', dataPath: (dataPath || \'\') + ' + (it.errorPath) + ', message: \'should NOT have more than ' + ($schema.length) + ' items\' ';
out += ' var err = { keyword: \'' + ('additionalItems') + '\', dataPath: (dataPath || \'\') + ' + (it.errorPath) + ', message: \'should NOT have more than ' + ($schema.length) + ' items\' ';
if (it.opts.verbose) {
out += ', schema: false, data: ' + ($data);
}

View File

@ -20,7 +20,7 @@ module.exports = function anonymous(it) {
}
out += ' }]; return false; ';
} else {
out += ' var err = { keyword: \'' + ('maxItems') + '\', dataPath: (dataPath || \'\') + ' + (it.errorPath) + ', message: \'should NOT have more than ' + ($schema) + ' items\' ';
out += ' var err = { keyword: \'' + ('maxItems') + '\', dataPath: (dataPath || \'\') + ' + (it.errorPath) + ', message: \'should NOT have more than ' + ($schema) + ' items\' ';
if (it.opts.verbose) {
out += ', schema: ' + ($schema) + ', data: ' + ($data);
}

View File

@ -26,7 +26,7 @@ module.exports = function anonymous(it) {
}
out += ' }]; return false; ';
} else {
out += ' var err = { keyword: \'' + ('maxLength') + '\', dataPath: (dataPath || \'\') + ' + (it.errorPath) + ', message: \'should NOT be longer than ' + ($schema) + ' characters\' ';
out += ' var err = { keyword: \'' + ('maxLength') + '\', dataPath: (dataPath || \'\') + ' + (it.errorPath) + ', message: \'should NOT be longer than ' + ($schema) + ' characters\' ';
if (it.opts.verbose) {
out += ', schema: ' + ($schema) + ', data: ' + ($data);
}

View File

@ -20,7 +20,7 @@ module.exports = function anonymous(it) {
}
out += ' }]; return false; ';
} else {
out += ' var err = { keyword: \'' + ('maxProperties') + '\', dataPath: (dataPath || \'\') + ' + (it.errorPath) + ', message: \'should NOT have more than ' + ($schema) + ' properties\' ';
out += ' var err = { keyword: \'' + ('maxProperties') + '\', dataPath: (dataPath || \'\') + ' + (it.errorPath) + ', message: \'should NOT have more than ' + ($schema) + ' properties\' ';
if (it.opts.verbose) {
out += ', schema: ' + ($schema) + ', data: ' + ($data);
}

View File

@ -23,7 +23,7 @@ module.exports = function anonymous(it) {
}
out += ' }]; return false; ';
} else {
out += ' var err = { keyword: \'' + ('maximum') + '\', dataPath: (dataPath || \'\') + ' + (it.errorPath) + ', message: \'should be ' + ($op) + ' ' + ($schema) + '\' ';
out += ' var err = { keyword: \'' + ('maximum') + '\', dataPath: (dataPath || \'\') + ' + (it.errorPath) + ', message: \'should be ' + ($op) + ' ' + ($schema) + '\' ';
if (it.opts.verbose) {
out += ', schema: ' + ($schema) + ', data: ' + ($data);
}

View File

@ -20,7 +20,7 @@ module.exports = function anonymous(it) {
}
out += ' }]; return false; ';
} else {
out += ' var err = { keyword: \'' + ('minItems') + '\', dataPath: (dataPath || \'\') + ' + (it.errorPath) + ', message: \'should NOT have less than ' + ($schema) + ' items\' ';
out += ' var err = { keyword: \'' + ('minItems') + '\', dataPath: (dataPath || \'\') + ' + (it.errorPath) + ', message: \'should NOT have less than ' + ($schema) + ' items\' ';
if (it.opts.verbose) {
out += ', schema: ' + ($schema) + ', data: ' + ($data);
}

View File

@ -26,7 +26,7 @@ module.exports = function anonymous(it) {
}
out += ' }]; return false; ';
} else {
out += ' var err = { keyword: \'' + ('minLength') + '\', dataPath: (dataPath || \'\') + ' + (it.errorPath) + ', message: \'should NOT be shorter than ' + ($schema) + ' characters\' ';
out += ' var err = { keyword: \'' + ('minLength') + '\', dataPath: (dataPath || \'\') + ' + (it.errorPath) + ', message: \'should NOT be shorter than ' + ($schema) + ' characters\' ';
if (it.opts.verbose) {
out += ', schema: ' + ($schema) + ', data: ' + ($data);
}

View File

@ -20,7 +20,7 @@ module.exports = function anonymous(it) {
}
out += ' }]; return false; ';
} else {
out += ' var err = { keyword: \'' + ('minProperties') + '\', dataPath: (dataPath || \'\') + ' + (it.errorPath) + ', message: \'should NOT have less than ' + ($schema) + ' properties\' ';
out += ' var err = { keyword: \'' + ('minProperties') + '\', dataPath: (dataPath || \'\') + ' + (it.errorPath) + ', message: \'should NOT have less than ' + ($schema) + ' properties\' ';
if (it.opts.verbose) {
out += ', schema: ' + ($schema) + ', data: ' + ($data);
}

View File

@ -23,7 +23,7 @@ module.exports = function anonymous(it) {
}
out += ' }]; return false; ';
} else {
out += ' var err = { keyword: \'' + ('minimum') + '\', dataPath: (dataPath || \'\') + ' + (it.errorPath) + ', message: \'should be ' + ($op) + ' ' + ($schema) + '\' ';
out += ' var err = { keyword: \'' + ('minimum') + '\', dataPath: (dataPath || \'\') + ' + (it.errorPath) + ', message: \'should be ' + ($op) + ' ' + ($schema) + '\' ';
if (it.opts.verbose) {
out += ', schema: ' + ($schema) + ', data: ' + ($data);
}

View File

@ -20,7 +20,7 @@ module.exports = function anonymous(it) {
}
out += ' }]; return false; ';
} else {
out += ' var err = { keyword: \'' + ('multipleOf') + '\', dataPath: (dataPath || \'\') + ' + (it.errorPath) + ', message: \'should be multiple of ' + ($schema) + '\' ';
out += ' var err = { keyword: \'' + ('multipleOf') + '\', dataPath: (dataPath || \'\') + ' + (it.errorPath) + ', message: \'should be multiple of ' + ($schema) + '\' ';
if (it.opts.verbose) {
out += ', schema: ' + ($schema) + ', data: ' + ($data);
}

View File

@ -26,7 +26,7 @@ module.exports = function anonymous(it) {
}
out += ' }]; return false; ';
} else {
out += ' var err = { keyword: \'' + ('not') + '\', dataPath: (dataPath || \'\') + ' + (it.errorPath) + ', message: \'should NOT be valid\' ';
out += ' var err = { keyword: \'' + ('not') + '\', dataPath: (dataPath || \'\') + ' + (it.errorPath) + ', message: \'should NOT be valid\' ';
if (it.opts.verbose) {
out += ', schema: validate.schema' + ($schemaPath) + ', data: ' + ($data);
}
@ -44,7 +44,7 @@ module.exports = function anonymous(it) {
}
out += ' }]; return false; ';
} else {
out += ' var err = { keyword: \'' + ('not') + '\', dataPath: (dataPath || \'\') + ' + (it.errorPath) + ', message: \'should NOT be valid\' ';
out += ' var err = { keyword: \'' + ('not') + '\', dataPath: (dataPath || \'\') + ' + (it.errorPath) + ', message: \'should NOT be valid\' ';
if (it.opts.verbose) {
out += ', schema: validate.schema' + ($schemaPath) + ', data: ' + ($data);
}

View File

@ -44,7 +44,7 @@ module.exports = function anonymous(it) {
}
out += ' }]; return false; ';
} else {
out += ' var err = { keyword: \'' + ('oneOf') + '\', dataPath: (dataPath || \'\') + ' + (it.errorPath) + ', message: \'should match exactly one schema in oneOf\' ';
out += ' var err = { keyword: \'' + ('oneOf') + '\', dataPath: (dataPath || \'\') + ' + (it.errorPath) + ', message: \'should match exactly one schema in oneOf\' ';
if (it.opts.verbose) {
out += ', schema: validate.schema' + ($schemaPath) + ', data: ' + ($data);
}

View File

@ -21,7 +21,7 @@ module.exports = function anonymous(it) {
}
out += ' }]; return false; ';
} else {
out += ' var err = { keyword: \'' + ('pattern') + '\', dataPath: (dataPath || \'\') + ' + (it.errorPath) + ', message: \'should match pattern "' + ($schema) + '"\' ';
out += ' var err = { keyword: \'' + ('pattern') + '\', dataPath: (dataPath || \'\') + ' + (it.errorPath) + ', message: \'should match pattern "' + ($schema) + '"\' ';
if (it.opts.verbose) {
out += ', schema: \'' + ($schema) + '\', data: ' + ($data);
}

View File

@ -57,7 +57,7 @@ module.exports = function anonymous(it) {
}
out += ' }]; return false; ';
} else {
out += ' var err = { keyword: \'' + ('additionalProperties') + '\', dataPath: (dataPath || \'\') + ' + (it.errorPath) + ', message: \'additional properties NOT allowed\' ';
out += ' var err = { keyword: \'' + ('additionalProperties') + '\', dataPath: (dataPath || \'\') + ' + (it.errorPath) + ', message: \'additional properties NOT allowed\' ';
if (it.opts.verbose) {
out += ', schema: false, data: ' + ($data);
}

View File

@ -38,7 +38,7 @@ module.exports = function anonymous(it) {
}
out += ' }]; return false; ';
} else {
out += ' var err = { keyword: \'' + ('required') + '\', dataPath: (dataPath || \'\') + ' + (it.errorPath) + ', message: \'properties ' + ($schema.slice(0, 7).join(", "));
out += ' var err = { keyword: \'' + ('required') + '\', dataPath: (dataPath || \'\') + ' + (it.errorPath) + ', message: \'properties ' + ($schema.slice(0, 7).join(", "));
if ($schema.length > 7) {
out += '...';
}
@ -65,7 +65,7 @@ module.exports = function anonymous(it) {
}
out += ' }]; return false; ';
} else {
out += ' var err = { keyword: \'' + ('required') + '\', dataPath: (dataPath || \'\') + ' + (it.errorPath) + ', message: \'properties ' + ($schema.slice(0, 7).join(", "));
out += ' var err = { keyword: \'' + ('required') + '\', dataPath: (dataPath || \'\') + ' + (it.errorPath) + ', message: \'properties ' + ($schema.slice(0, 7).join(", "));
if ($schema.length > 7) {
out += '...';
}

View File

@ -21,7 +21,7 @@ module.exports = function anonymous(it) {
}
out += ' }]; return false; ';
} else {
out += ' var err = { keyword: \'' + ('uniqueItems') + '\', dataPath: (dataPath || \'\') + ' + (it.errorPath) + ', message: \'items ## \' + j + \' and \' + i + \' are duplicate\' ';
out += ' var err = { keyword: \'' + ('uniqueItems') + '\', dataPath: (dataPath || \'\') + ' + (it.errorPath) + ', message: \'items ## \' + j + \' and \' + i + \' are duplicate\' ';
if (it.opts.verbose) {
out += ', schema: ' + ($schema) + ', data: ' + ($data);
}

View File

@ -79,7 +79,7 @@ module.exports = function anonymous(it) {
}
out += ' }]; return false; ';
} else {
out += ' var err = { keyword: \'' + ('type') + '\', dataPath: (dataPath || \'\') + ' + (it.errorPath) + ', message: \'should be ';
out += ' var err = { keyword: \'' + ('type') + '\', dataPath: (dataPath || \'\') + ' + (it.errorPath) + ', message: \'should be ';
if ($isArray) {
out += '' + ($typeSchema.join(","));
} else {
@ -137,7 +137,7 @@ module.exports = function anonymous(it) {
}
out += ' }]; return false; ';
} else {
out += ' var err = { keyword: \'' + ('type') + '\', dataPath: (dataPath || \'\') + ' + (it.errorPath) + ', message: \'should be ';
out += ' var err = { keyword: \'' + ('type') + '\', dataPath: (dataPath || \'\') + ' + (it.errorPath) + ', message: \'should be ';
if ($isArray) {
out += '' + ($typeSchema.join(","));
} else {

View File

@ -5,7 +5,7 @@ var glob = require('glob')
, assert = require('assert');
var ONLY_FILES, SKIP_FILES;
// ONLY_FILES = [
ONLY_FILES = [
// 'type', 'not', 'allOf', 'anyOf', 'oneOf', 'enum',
// 'maximum', 'minimum', 'multipleOf', 'maxLength', 'minLength', 'pattern',
// 'properties', 'patternProperties', 'additionalProperties',
@ -15,7 +15,7 @@ var ONLY_FILES, SKIP_FILES;
// 'optional/format', 'optional/bignum',
// 'ref', 'refRemote', 'definitions',
// 'schemas/complex', 'schemas/basic', 'schemas/advanced',
// ];
];
SKIP_FILES = [
'optional/zeroTerminatedFloats',
@ -64,38 +64,35 @@ function addTests(description, testsPath) {
var files = getTestFiles(testsPath);
files.forEach(function (file) {
var skip = (ONLY_FILES && ONLY_FILES.indexOf(file.name) == -1) ||
(SKIP_FILES && SKIP_FILES.indexOf(file.name) >= 0);
// if (skip) return;
var filter = {
skip: SKIP_FILES && SKIP_FILES.indexOf(file.name) >= 0,
only: ONLY_FILES && ONLY_FILES.indexOf(file.name) >= 0
}
(skip ? describe.skip : describe) (file.name, function() {
skipOrOnly(filter, describe)(file.name, function() {
var testSets = require(file.path);
testSets.forEach(function (testSet) {
// if (testSet.description != 'change resolution scope') return;
skipOrOnly(testSet, describe)(testSet.description, function() {
var validate, fullValidate;
// it(testSet.description, function() {
before(function() {
validate = ajv.compile(testSet.schema);
// console.log('validate', validate.toString());
fullValidate = fullAjv.compile(testSet.schema);
// fullValidate = fullAjv.compile(testSet.schema);
});
testSet.tests.forEach(function (test) {
// if (test.description != 'valid number') return;
skipOrOnly(test, it)(test.description, function() {
doTest(validate);
// doTest(fullValidate);
});
function doTest(validate) {
var valid = validate(test.data);
// if (valid !== test.valid) console.log('result', valid, test.valid, validate.errors);
if (valid !== test.valid) console.log('result', valid, test.valid, validate.errors);
assert.equal(valid, test.valid);
if (valid) assert(validate.errors === null);
else assert(validate.errors.length > 0);
var valid = fullValidate(test.data);
// console.log('full result', valid, fullValidate.errors);
assert.equal(valid, test.valid);
if (valid) assert(fullValidate.errors === null);
else assert(fullValidate.errors.length > 0);
});
}
});
});
});
@ -105,8 +102,8 @@ function addTests(description, testsPath) {
}
function skipOrOnly(test, func) {
return test.only ? func.only : test.skip ? func.skip : func;
function skipOrOnly(filter, func) {
return filter.only ? func.only : filter.skip ? func.skip : func;
}