ajv/spec/json-schema.spec.js

81 lines
2.6 KiB
JavaScript
Raw Normal View History

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',
2015-05-30 13:50:18 +03:00
// 'optional/bignum',
// 'ref'
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
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 13:50:18 +03:00
// if (testSet.description != 'relative pointer ref to object') return;
2015-05-30 11:53:04 +03:00
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 13:50:18 +03:00
// if (test.description != 'match') return;
2015-05-30 11:53:04 +03:00
it(test.description, function() {
2015-05-26 04:11:36 +03:00
var result = validate(test.data);
// console.log('result', result);
assert.equal(result.valid, test.valid);
if (result.valid) assert(result.errors.length == 0);
else assert(result.errors.length > 0);
var result = fullValidate(test.data);
// console.log('full result', result);
2015-05-26 04:11:36 +03:00
assert.equal(result.valid, test.valid);
if (result.valid) assert(result.errors.length == 0);
else assert(result.errors.length > 0);
2015-05-30 11:53:04 +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;
}