ajv/spec/json-schema.spec.js

94 lines
3.3 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',
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-31 15:26:54 +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({ unicode: true })
, fullAjv = Ajv({ unicode: true, 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);
}
describe('JSON-Schema tests', function () {
2015-05-31 15:26:54 +03:00
addTests('draft4: ', './json-schema-test-suite/tests/draft4/{**/,}*.json');
function addTests(description, testsPath) {
describe(description, function() {
var files = getTestFiles(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;
describe(file.name, function() {
var testSets = require(file.path);
testSets.forEach(function (testSet) {
// if (testSet.description != 'allOf with base schema') return;
2015-05-31 15:26:54 +03:00
describe(testSet.description, function() {
// it(testSet.description, function() {
var validate = ajv.compile(testSet.schema);
var fullValidate = fullAjv.compile(testSet.schema);
testSet.tests.forEach(function (test) {
// if (test.description != 'one supplementary Unicode code point is not long enough') return;
2015-06-02 03:59:32 +03:00
// console.log(testSet.schema, '\n\n***\n\n', validate.toString());
2015-05-31 15:26:54 +03:00
it(test.description, function() {
var valid = validate(test.data);
// console.log('result', valid, validate.errors);
2015-05-31 15:26:54 +03:00
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);
2015-05-31 15:26:54 +03:00
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-05-31 15:26:54 +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 optional = /optional\/\w+\.json/.test(file) ? 'optional/' : '';
return { path: file, name: optional + path.basename(file, '.json') };
2015-05-26 04:11:36 +03:00
});
}