2015-05-26 04:11:36 +03:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var fs = require('fs')
|
|
|
|
, path = require('path')
|
|
|
|
, assert = require('assert')
|
|
|
|
, TESTS_PATH = 'JSON-Schema-Test-Suite/tests/draft4/';
|
|
|
|
|
|
|
|
var ONLY_RULES;
|
2015-05-29 17:58:02 +03:00
|
|
|
// ONLY_RULES = [
|
|
|
|
// 'type', 'not', 'allOf', 'anyOf', 'oneOf', 'enum',
|
|
|
|
// 'maximum', 'minimum', 'multipleOf',
|
|
|
|
// 'maxLength', 'minLength', 'pattern',
|
|
|
|
// 'properties', 'patternProperties', 'additionalProperties',
|
|
|
|
// 'dependencies',
|
|
|
|
// 'required', 'maxProperties', 'minProperties',
|
2015-05-30 01:22:11 +03:00
|
|
|
// 'maxItems', 'minItems', 'items', 'additionalItems', 'uniqueItems',
|
|
|
|
// 'optional/format',
|
|
|
|
// 'optional/bignum'
|
2015-05-29 17:58:02 +03:00
|
|
|
// ];
|
2015-05-26 04:11:36 +03:00
|
|
|
|
|
|
|
|
2015-05-30 01:32:47 +03:00
|
|
|
var Ajv = require('../lib/ajv')
|
|
|
|
, ajv = Ajv()
|
|
|
|
, fullAjv = Ajv({ allErrors: true, verbose: true });
|
2015-05-26 04:11:36 +03:00
|
|
|
|
2015-05-28 03:24:42 +03:00
|
|
|
describe.only('JSON-Schema tests', function () {
|
2015-05-26 04:11:36 +03:00
|
|
|
var testsPath = path.join(__dirname, '..', TESTS_PATH);
|
|
|
|
var files = getTestFilesRecursive(testsPath);
|
|
|
|
|
|
|
|
files.forEach(function (file) {
|
|
|
|
if (ONLY_RULES && ONLY_RULES.indexOf(file.name) == -1) return;
|
|
|
|
describe(file.name, function() {
|
|
|
|
var testSets = require(file.path);
|
|
|
|
testSets.forEach(function (testSet) {
|
2015-05-30 01:22:11 +03:00
|
|
|
// if (testSet.description != 'validation of URIs') return;
|
|
|
|
// describe(testSet.description, function() {
|
|
|
|
it(testSet.description, function() {
|
2015-05-30 01:32:47 +03:00
|
|
|
var validate = ajv.compile(testSet.schema);
|
|
|
|
var fullValidate = fullAjv.compile(testSet.schema);
|
2015-05-26 04:11:36 +03:00
|
|
|
|
|
|
|
testSet.tests.forEach(function (test) {
|
2015-05-30 01:22:11 +03:00
|
|
|
// if (test.description != 'a valid date-time string') return;
|
|
|
|
// it(test.description, function() {
|
2015-05-26 04:11:36 +03:00
|
|
|
var result = validate(test.data);
|
2015-05-28 03:24:42 +03:00
|
|
|
// console.log('result', result);
|
|
|
|
assert.equal(result.valid, test.valid);
|
anyOf, enum, maxItems, minItems, maxLength, minLength, maxProperties, minProperties, multipleOf, oneOf, pattern rules
2015-05-29 00:27:16 +03:00
|
|
|
if (result.valid) assert(result.errors.length == 0);
|
|
|
|
else assert(result.errors.length > 0);
|
2015-05-28 03:24:42 +03:00
|
|
|
|
|
|
|
var result = fullValidate(test.data);
|
|
|
|
// console.log('full result', result);
|
2015-05-26 04:11:36 +03:00
|
|
|
assert.equal(result.valid, test.valid);
|
anyOf, enum, maxItems, minItems, maxLength, minLength, maxProperties, minProperties, multipleOf, oneOf, pattern rules
2015-05-29 00:27:16 +03:00
|
|
|
if (result.valid) assert(result.errors.length == 0);
|
|
|
|
else assert(result.errors.length > 0);
|
2015-05-30 01:22:11 +03:00
|
|
|
// });
|
2015-05-26 04:11:36 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
function getTestFilesRecursive(rootPath) {
|
|
|
|
var list = fs.readdirSync(rootPath);
|
|
|
|
var files = [];
|
|
|
|
list.forEach(function (item) {
|
|
|
|
var itemPath = path.join(rootPath, item);
|
|
|
|
var stat = fs.statSync(itemPath);
|
|
|
|
if (stat.isFile()) files.push({ name: path.basename(item, '.json'), path: itemPath });
|
|
|
|
else if (stat.isDirectory()) {
|
|
|
|
var _files = getTestFilesRecursive(itemPath);
|
|
|
|
_files.forEach(function (f) {
|
|
|
|
files.push({ name: path.join(item, f.name), path: f.path })
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return files;
|
|
|
|
}
|