ajv/spec/json-schema.spec.js

103 lines
3.5 KiB
JavaScript
Raw Normal View History

2015-05-26 04:11:36 +03:00
'use strict';
2015-05-31 15:26:54 +03:00
var glob = require('glob')
2015-05-26 04:11:36 +03:00
, path = require('path')
2015-05-31 15:26:54 +03:00
, assert = require('assert');
2015-05-26 04:11:36 +03:00
var ONLY_RULES, SKIP_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', 'maxItems', 'minItems',
// 'items', 'additionalItems', 'uniqueItems',
// 'optional/format', 'optional/bignum',
// 'ref',
// 'definitions'
// 'schemas/complex'
2015-05-29 17:58:02 +03:00
// ];
2015-05-26 04:11:36 +03:00
SKIP_RULES = [
2015-05-31 15:26:54 +03:00
'refRemote',
'optional/zeroTerminatedFloats',
'schemas/complex'
];
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 = {
2015-05-31 15:26:54 +03:00
'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);
}
2015-06-06 18:18:52 +03:00
addTests('JSON-Schema tests draft4', './json-schema-test-suite/tests/draft4/{**/,}*.json');
addTests('Advanced schema tests', './tests/{**/,}*.json');
function addTests(description, testsPath) {
describe(description, function() {
var files = getTestFiles(testsPath);
files.forEach(function (file) {
var skip = (ONLY_RULES && ONLY_RULES.indexOf(file.name) == -1) ||
(SKIP_RULES && SKIP_RULES.indexOf(file.name) >= 0);
if (skip) return;
2015-06-06 18:18:52 +03:00
(skip ? describe.skip : describe) (file.name, function() {
2015-06-06 18:18:52 +03:00
var testSets = require(file.path);
testSets.forEach(function (testSet) {
// if (testSet.description != 'allOf with base schema') return;
describe(testSet.description, function() {
var validate, fullValidate;
2015-06-06 18:18:52 +03:00
// it(testSet.description, function() {
before(function() {
validate = ajv.compile(testSet.schema);
fullValidate = fullAjv.compile(testSet.schema);
});
2015-06-06 18:18:52 +03:00
testSet.tests.forEach(function (test) {
// if (test.description != 'one supplementary Unicode code point is not long enough') return;
// console.log(testSet.schema, '\n\n***\n\n', validate.toString());
it(test.description, function() {
var valid = validate(test.data);
// console.log('result', valid, validate.errors);
assert.equal(valid, test.valid);
if (valid) assert(validate.errors.length == 0);
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.length == 0);
else assert(fullValidate.errors.length > 0);
2015-05-31 15:26:54 +03:00
});
2015-05-30 11:53:04 +03:00
});
2015-05-26 04:11:36 +03:00
});
});
});
});
2015-06-06 18:18:52 +03:00
});
}
2015-05-26 04:11:36 +03:00
2015-05-31 15:26:54 +03:00
function getTestFiles(testsPath) {
var files = glob.sync(testsPath, { cwd: __dirname });
return files.map(function (file) {
var match = file.match(/(\w+\/)\w+\.json/)
var folder = match ? match[1] : '';
if (folder == 'draft4/') folder = '';
return { path: file, name: folder + path.basename(file, '.json') };
2015-05-26 04:11:36 +03:00
});
}