ajv/spec/json-schema.spec.js

100 lines
3.2 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, SKIP_RULES;
2015-05-29 17:58:02 +03:00
// ONLY_RULES = [
2015-05-31 12:46:44 +03:00
// 'type',
// 'not', 'allOf', 'anyOf', 'oneOf', 'enum',
// 'maximum', 'minimum', 'multipleOf',
// 'maxLength', 'minLength', 'pattern',
// 'properties', 'patternProperties', 'additionalProperties',
// 'dependencies', 'required',
// 'maxProperties', 'minProperties', 'maxItems', 'minItems',
// 'items', 'additionalItems', 'uniqueItems',
// 'optional/format', 'optional/bignum',
2015-05-30 13:50:18 +03:00
// 'ref'
2015-05-29 17:58:02 +03:00
// ];
2015-05-26 04:11:36 +03:00
SKIP_RULES = [
2015-05-30 23:08:31 +03:00
// 'refRemote',
// 'optional/zeroTerminatedFloats'
];
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
var remoteRefs = {
'http://localhost:1234/integer.json': require('../JSON-Schema-Test-Suite/remotes/integer.json'),
'http://localhost:1234/subSchemas.json': require('../JSON-Schema-Test-Suite/remotes/subSchemas.json'),
'http://localhost:1234/folder/folderInteger.json': require('../JSON-Schema-Test-Suite/remotes/folder/folderInteger.json')
};
for (var id in remoteRefs) {
ajv.addSchema(remoteRefs[id], id);
fullAjv.addSchema(remoteRefs[id], id);
}
describe('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;
if (SKIP_RULES && SKIP_RULES.indexOf(file.name) >= 0) return;
2015-05-26 04:11:36 +03:00
describe(file.name, function() {
var testSets = require(file.path);
testSets.forEach(function (testSet) {
2015-05-31 12:46:44 +03:00
// if (testSet.description != 'multiple types can be specified in an array') 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-31 12:46:44 +03:00
// if (test.description != 'an integer is valid') return;
2015-05-30 11:53:04 +03:00
it(test.description, function() {
2015-05-30 23:08:31 +03:00
var valid = validate(test.data);
// console.log('result', result);
2015-05-30 23:08:31 +03:00
assert.equal(valid, test.valid);
if (valid) assert(validate.errors.length == 0);
else assert(validate.errors.length > 0);
2015-05-30 23:08:31 +03:00
var valid = fullValidate(test.data);
// console.log('full result', result);
2015-05-30 23:08:31 +03:00
assert.equal(valid, test.valid);
if (valid) assert(validate.errors.length == 0);
else assert(validate.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;
}