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
|
|
|
|
2015-05-30 21:11:06 +03:00
|
|
|
var ONLY_RULES, SKIP_RULES;
|
2015-05-29 17:58:02 +03:00
|
|
|
// ONLY_RULES = [
|
2015-06-05 01:08:45 +03:00
|
|
|
// 'type', 'not', 'allOf', 'anyOf', 'oneOf', 'enum',
|
|
|
|
// 'maximum', 'minimum', 'multipleOf', 'maxLength', 'minLength', 'pattern',
|
2015-05-31 01:04:46 +03:00
|
|
|
// 'properties', 'patternProperties', 'additionalProperties',
|
2015-06-05 01:08:45 +03:00
|
|
|
// 'dependencies', 'required',
|
2015-05-31 01:04:46 +03:00
|
|
|
// '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
|
|
|
|
2015-05-30 21:11:06 +03:00
|
|
|
SKIP_RULES = [
|
2015-05-31 15:26:54 +03:00
|
|
|
'refRemote',
|
|
|
|
'optional/zeroTerminatedFloats'
|
2015-05-30 21:11:06 +03:00
|
|
|
];
|
|
|
|
|
2015-05-26 04:11:36 +03:00
|
|
|
|
2015-05-30 01:32:47 +03:00
|
|
|
var Ajv = require('../lib/ajv')
|
2015-06-05 01:08:45 +03:00
|
|
|
, ajv = Ajv({ unicode: true })
|
|
|
|
, fullAjv = Ajv({ unicode: true, allErrors: true, verbose: true });
|
2015-05-26 04:11:36 +03:00
|
|
|
|
2015-05-30 21:11:06 +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')
|
2015-05-30 21:11:06 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
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) {
|
2015-06-04 23:18:31 +03:00
|
|
|
// 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) {
|
2015-06-05 01:08:45 +03:00
|
|
|
// 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);
|
2015-06-03 03:36:59 +03:00
|
|
|
// 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);
|
2015-06-03 03:36:59 +03:00
|
|
|
// console.log('full result', valid, fullValidate.errors);
|
2015-05-31 15:26:54 +03:00
|
|
|
assert.equal(valid, test.valid);
|
2015-06-03 03:36:59 +03:00
|
|
|
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
|
|
|
});
|
|
|
|
}
|