style: eslint tests, closes #250

master
Evgeny Poberezkin 2016-07-28 00:50:30 +01:00
parent 0630539aa3
commit b853218249
No known key found for this signature in database
GPG Key ID: 016D62451CED9D8E
20 changed files with 282 additions and 280 deletions

View File

@ -12,7 +12,7 @@
],
"scripts": {
"jshint": "jshint lib/*.js lib/**/*.js --exclude lib/dotjs/**/*",
"eslint": "if-node-version '>=4' eslint lib/*.js lib/compile/*.js",
"eslint": "if-node-version '>=4' eslint lib/*.js lib/compile/*.js spec",
"test-spec": "mocha spec/*.spec.js -R spec",
"test-fast": "AJV_FAST_TEST=true npm run test-spec",
"test-debug": "mocha spec/*.spec.js --debug-brk -R spec",

13
spec/.eslintrc.json Normal file
View File

@ -0,0 +1,13 @@
{
"rules": {
"no-console": 0,
"no-empty": [ 2, { "allowEmptyCatch": true } ],
"quotes": 0,
"no-invalid-this": 0
},
"globals": {
"describe": false,
"it": false,
"beforeEach": false
}
}

View File

@ -1 +1,3 @@
'use strict';
module.exports = typeof window == 'object' ? window.Ajv : require('' + '../lib/ajv');

View File

@ -1,6 +1,5 @@
'use strict';
var Ajv = require('./ajv')
, should = require('./chai').should()
, stableStringify = require('json-stable-stringify');
@ -31,14 +30,14 @@ describe('Ajv', function () {
var v1 = ajv.compile({ id: '//e.com/int.json', type: 'integer', minimum: 1 });
var v2 = ajv.compile({ id: '//e.com/int.json', minimum: 1, type: 'integer' });
v1 .should.equal(v2);
})
});
it('should throw if different schema has the same id', function() {
ajv.compile({ id: '//e.com/int.json', type: 'integer' });
should.throw(function() {
ajv.compile({ id: '//e.com/int.json', type: 'integer', minimum: 1 });
});
})
});
it('should throw if invalid schema is compiled', function() {
should.throw(function() {
@ -53,13 +52,13 @@ describe('Ajv', function () {
validate(2) .should.equal(true);
validate(3) .should.equal(false);
var schema = { even: false };
schema = { even: false };
should.throw(function() {
var validate = ajv.compile(schema);
ajv.compile(schema);
});
function badEvenCode(it, keyword, schema) {
var op = schema ? '===' : '!==='; // invalid on purpose
function badEvenCode(it, keyword, _schema) {
var op = _schema ? '===' : '!==='; // invalid on purpose
return 'data' + (it.dataLevel || '') + ' % 2 ' + op + ' 0';
}
});
@ -81,7 +80,7 @@ describe('Ajv', function () {
ajv.compile({ id: '//e.com/str.json', type: 'string' }) .should.be.a('function');
ajv.validate('//e.com/str.json', 'a') .should.equal(true);
ajv.validate('//e.com/str.json', 1) .should.equal(false);
ajv.validate('//e.com/str.json', 1) .should.equal(false);
});
it('should throw exception if no schema with ref', function() {
@ -181,7 +180,7 @@ describe('Ajv', function () {
});
it('should throw if schema is not an object', function() {
should.throw(function() { ajv.addSchema('foo') });
should.throw(function() { ajv.addSchema('foo'); });
});
});
@ -219,7 +218,7 @@ describe('Ajv', function () {
var schema = { type: 'integer' }
, str = stableStringify(schema);
ajv.addSchema(schema, 'int');
var v = ajv.getSchema('int')
var v = ajv.getSchema('int');
v .should.be.a('function');
ajv._cache.get(str).validate .should.equal(v);
@ -234,7 +233,7 @@ describe('Ajv', function () {
, str = stableStringify(schema);
ajv.addSchema(schema);
var v = ajv.getSchema('//e.com/int.json')
var v = ajv.getSchema('//e.com/int.json');
v .should.be.a('function');
ajv._cache.get(str).validate .should.equal(v);

View File

@ -20,7 +20,8 @@ function _getAjvInstances(opts, useOpts) {
var instances = _getAjvInstances(opts, useOpts)
, instances1 = _getAjvInstances(opts, useOpts1);
return instances.concat(instances1);
} else return [ new Ajv(useOpts) ];
}
return [ new Ajv(useOpts) ];
}

View File

@ -5,12 +5,12 @@ var fullTest = isBrowser || !process.env.AJV_FAST_TEST;
var options = fullTest
? {
allErrors: true,
verbose: true,
format: 'full',
inlineRefs: false,
jsonPointers: true
}
allErrors: true,
verbose: true,
format: 'full',
inlineRefs: false,
jsonPointers: true
}
: { allErrors: true };
if (fullTest && !isBrowser) options.beautify = true;

View File

@ -1,12 +1,11 @@
'use strict';
var Ajv = require('./ajv')
, should = require('./chai').should();
describe('compileAsync method', function() {
var ajv, loadCallCount;
var ajv, loadCallCount;
var SCHEMAS = {
"http://example.com/object.json": {
@ -22,32 +21,32 @@ describe('compileAsync method', function() {
"minimum": 2
},
"http://example.com/tree.json": {
"id": "http://example.com/tree.json",
"type": "array",
"id": "http://example.com/tree.json",
"type": "array",
"items": { "$ref": "leaf.json" }
},
"http://example.com/leaf.json": {
"id": "http://example.com/leaf.json",
"properties": {
"name": { "type": "string" },
"subtree": { "$ref": "tree.json" }
}
"id": "http://example.com/leaf.json",
"properties": {
"name": { "type": "string" },
"subtree": { "$ref": "tree.json" }
}
},
"http://example.com/recursive.json": {
"id": "http://example.com/recursive.json",
"properties": {
"b": { "$ref": "parent.json" }
},
"required": ["b"]
"id": "http://example.com/recursive.json",
"properties": {
"b": { "$ref": "parent.json" }
},
"required": ["b"]
},
"http://example.com/invalid.json": {
"id": "http://example.com/recursive.json",
"properties": {
"invalid": { "type": "number" }
},
"required": "invalid"
},
"required": "invalid"
}
}
};
beforeEach(function() {
loadCallCount = 0;
@ -102,12 +101,12 @@ describe('compileAsync method', function() {
should.not.exist(err);
validate .should.be.a('function');
var validData = { tree: [
{ name: 'a', subtree: [ { name: 'a.a' } ] },
{ name: 'b' }
] };
var invalidData = { tree: [
{ name: 'a', subtree: [ { name: 1 } ] }
] };
{ name: 'a', subtree: [ { name: 'a.a' } ] },
{ name: 'b' }
] };
var invalidData = { tree: [
{ name: 'a', subtree: [ { name: 1 } ] }
] };
validate(validData) .should.equal(true);
validate(invalidData) .should.equal(false);
done();
@ -161,17 +160,19 @@ describe('compileAsync method', function() {
"type": "integer",
"minimum": 2
};
var beforeCallback1;
ajv.compileAsync(schema, function (err, validate) {
beforeCallback1 .should.equal(true);
spec(err, validate);
ajv.compileAsync(schema, function (err, validate) {
var beforeCallback2;
ajv.compileAsync(schema, function (_err, _validate) {
beforeCallback2 .should.equal(true);
spec(err, validate);
spec(_err, _validate);
done();
});
var beforeCallback2 = true;
beforeCallback2 = true;
});
var beforeCallback1 = true;
beforeCallback1 = true;
function spec(err, validate) {
should.not.exist(err);
@ -222,7 +223,7 @@ describe('compileAsync method', function() {
"type": "integer",
"minimum": 2
};
var ajv = new Ajv();
ajv = new Ajv;
should.throw(function() {
ajv.compileAsync(schema, function() {
done(new Error('it should have thrown exception'));
@ -284,7 +285,7 @@ describe('compileAsync method', function() {
"a": { "$ref": "object.json" }
}
};
var ajv = new Ajv({ loadSchema: badLoadSchema });
ajv = new Ajv({ loadSchema: badLoadSchema });
ajv.compileAsync(schema, function (err, validate) {
should.exist(err);
should.not.exist(validate);
@ -305,7 +306,7 @@ describe('compileAsync method', function() {
done();
});
function badCompile(schema) {
function badCompile(/* schema */) {
throw new Error('cant compile keyword schema');
}
});
@ -315,8 +316,8 @@ describe('compileAsync method', function() {
function loadSchema(uri, callback) {
loadCallCount++;
setTimeout(function() {
if (SCHEMAS[uri]) callback(null, SCHEMAS[uri]);
else callback(new Error('404'));
if (SCHEMAS[uri]) return callback(null, SCHEMAS[uri]);
callback(new Error('404'));
}, 10);
}
});

View File

@ -3,7 +3,6 @@
var jsonSchemaTest = require('json-schema-test')
, Promise = require('./promise')
, getAjvInstances = require('./ajv_async_instances')
, assert = require('./chai').assert
, Ajv = require('./ajv');
@ -32,8 +31,9 @@ jsonSchemaTest(instances, {
function testSuites() {
var suites;
if (typeof window == 'object') {
var suites = {
suites = {
'async schemas': require('./async/{**/,}*.json', {mode: 'list'})
};
for (var suiteName in suites) {
@ -42,9 +42,9 @@ function testSuites() {
});
}
} else {
var suites = {
suites = {
'async schemas': './async/{**/,}*.json'
}
};
}
return suites;
}
@ -106,16 +106,15 @@ function checkIdExistsWithError(schema, data) {
default: throw new Error('no such table');
}
function check(table, IDs) {
if (IDs.indexOf(data) >= 0) {
function check(_table, IDs) {
if (IDs.indexOf(data) >= 0)
return Promise.resolve(true);
} else {
var error = {
keyword: 'idExistsWithError',
message: 'id not found in table ' + table
};
return Promise.reject(new Ajv.ValidationError([error]));
}
var error = {
keyword: 'idExistsWithError',
message: 'id not found in table ' + _table
};
return Promise.reject(new Ajv.ValidationError([error]));
}
}

View File

@ -16,8 +16,8 @@ describe('async schemas, formats and keywords', function() {
ajv = instances[0];
});
function useCo(ajv) {
var async = ajv._opts.async;
function useCo(_ajv) {
var async = _ajv._opts.async;
return async == 'es7' || async == 'co*' ? identity : co;
}
@ -33,9 +33,9 @@ describe('async schemas, formats and keywords', function() {
return repeat(function() { return Promise.map(instances, test); });
function test(ajv) {
var validate = ajv.compile(schema);
var _co = useCo(ajv);
function test(_ajv) {
var validate = _ajv.compile(schema);
var _co = useCo(_ajv);
return Promise.all([
shouldBeValid( _co(validate('abc')) ),
@ -72,17 +72,17 @@ describe('async schemas, formats and keywords', function() {
it('should fail compilation if async format is inside sync schema', function() {
instances.forEach(function (ajv) {
instances.forEach(function (_ajv) {
var schema = {
type: 'string',
format: 'english_word'
};
shouldThrowFunc('async format in sync schema', function() {
ajv.compile(schema);
})
_ajv.compile(schema);
});
schema.$async = true;
ajv.compile(schema);
_ajv.compile(schema);
});
});
});
@ -90,15 +90,15 @@ describe('async schemas, formats and keywords', function() {
describe('async custom keywords', function() {
beforeEach(function() {
instances.forEach(function (ajv) {
ajv.addKeyword('idExists', {
instances.forEach(function (_ajv) {
_ajv.addKeyword('idExists', {
async: true,
type: 'number',
validate: checkIdExists,
errors: false
});
ajv.addKeyword('idExistsWithError', {
_ajv.addKeyword('idExistsWithError', {
async: true,
type: 'number',
validate: checkIdExistsWithError,
@ -109,7 +109,7 @@ describe('async schemas, formats and keywords', function() {
it('should fail compilation if async keyword is inside sync schema', function() {
instances.forEach(function (ajv) {
instances.forEach(function (_ajv) {
var schema = {
type: 'object',
properties: {
@ -121,17 +121,17 @@ describe('async schemas, formats and keywords', function() {
};
shouldThrowFunc('async keyword in sync schema', function() {
ajv.compile(schema);
})
_ajv.compile(schema);
});
schema.$async = true;
ajv.compile(schema);
_ajv.compile(schema);
});
});
it('should return custom error', function() {
return Promise.all(instances.map(function (ajv) {
return Promise.all(instances.map(function (_ajv) {
var schema = {
$async: true,
type: 'object',
@ -147,8 +147,8 @@ describe('async schemas, formats and keywords', function() {
}
};
var validate = ajv.compile(schema);
var _co = useCo(ajv);
var validate = _ajv.compile(schema);
var _co = useCo(_ajv);
return Promise.all([
shouldBeInvalid(_co(validate({ userId: 5, postId: 10 })), [ 'id not found in table posts' ]),
@ -178,16 +178,13 @@ describe('async schemas, formats and keywords', function() {
default: throw new Error('no such table');
}
function check(table, IDs) {
if (IDs.indexOf(data) >= 0) {
return Promise.resolve(true);
} else {
var error = {
keyword: 'idExistsWithError',
message: 'id not found in table ' + table
};
return Promise.reject(new Ajv.ValidationError([error]));
}
function check(_table, IDs) {
if (IDs.indexOf(data) >= 0) return Promise.resolve(true);
var error = {
keyword: 'idExistsWithError',
message: 'id not found in table ' + _table
};
return Promise.reject(new Ajv.ValidationError([error]));
}
}
});
@ -214,9 +211,9 @@ describe('async schemas, formats and keywords', function() {
}
};
return repeat(function() { return Promise.map(instances, function (ajv) {
var validate = ajv.compile(schema);
var _co = useCo(ajv);
return repeat(function() { return Promise.map(instances, function (_ajv) {
var validate = _ajv.compile(schema);
var _co = useCo(_ajv);
return Promise.all([
shouldBeValid( _co(validate({ word: 'tomorrow' })) ),
@ -333,10 +330,10 @@ describe('async schemas, formats and keywords', function() {
});
function recursiveTest(schema, refSchema) {
return repeat(function() { return Promise.map(instances, function (ajv) {
if (refSchema) try { ajv.addSchema(refSchema); } catch(e) {};
var validate = ajv.compile(schema);
var _co = useCo(ajv);
return repeat(function() { return Promise.map(instances, function (_ajv) {
if (refSchema) try { _ajv.addSchema(refSchema); } catch(e) {}
var validate = _ajv.compile(schema);
var _co = useCo(_ajv);
return Promise.all([
shouldBeValid( _co(validate({ foo: 'tomorrow' })) ),
@ -358,8 +355,8 @@ describe('async schemas, formats and keywords', function() {
function addFormatEnglishWord() {
instances.forEach(function (ajv) {
ajv.addFormat('english_word', {
instances.forEach(function (_ajv) {
_ajv.addFormat('english_word', {
async: true,
validate: checkWordOnServer
});
@ -405,13 +402,13 @@ function checkWordOnServer(str) {
function shouldThrowFunc(message, func) {
var err;
should.throw(function() {
try { func(); }
catch(e) { err = e; throw e; }
});
var err;
should.throw(function() {
try { func(); }
catch(e) { err = e; throw e; }
});
err.message .should.equal(message);
err.message .should.equal(message);
}
@ -448,14 +445,14 @@ function shouldThrow(p, exception) {
function checkNotValid(p) {
return p.then(function (valid) {
return p.then(function (/* valid */) {
throw new Error(SHOULD_BE_INVALID);
})
.catch(function (err) {
err. should.be.instanceof(Error);
if (err.message == SHOULD_BE_INVALID) throw err;
return err;
});
});
}

View File

@ -1 +1,3 @@
'use strict';
module.exports = typeof window == 'object' ? window.chai : require('' + 'chai');

View File

@ -1,7 +1,7 @@
'use strict';
var Ajv = require('./ajv')
, should = require('./chai').should();
var Ajv = require('./ajv');
require('./chai').should();
var coercionRules = {
@ -191,9 +191,9 @@ describe('Type coercion', function () {
it('should coerce scalar values', function() {
testRules(coercionRules, function (test, schema, canCoerce, toType, fromType) {
instances.forEach(function (ajv) {
var valid = ajv.validate(schema, test.from);
testRules(coercionRules, function (test, schema, canCoerce/*, toType, fromType*/) {
instances.forEach(function (_ajv) {
var valid = _ajv.validate(schema, test.from);
//if (valid !== canCoerce) console.log('true', toType, fromType, test, ajv.errors);
valid. should.equal(canCoerce);
});
@ -206,8 +206,8 @@ describe('Type coercion', function () {
instances = [ ajv, fullAjv ];
testRules(coercionArrayRules, function (test, schema, canCoerce, toType, fromType) {
instances.forEach(function (ajv) {
var valid = ajv.validate(schema, test.from);
instances.forEach(function (_ajv) {
var valid = _ajv.validate(schema, test.from);
if (valid !== canCoerce) console.log(toType, '.', fromType, test, schema, ajv.errors);
valid. should.equal(canCoerce);
});
@ -215,7 +215,7 @@ describe('Type coercion', function () {
});
it('should coerce values in objects/arrays and update properties/items', function() {
testRules(coercionRules, function (test, schema, canCoerce, toType, fromType) {
testRules(coercionRules, function (test, schema, canCoerce/*, toType, fromType*/) {
var schemaObject = {
type: 'object',
properties: {
@ -234,14 +234,14 @@ describe('Type coercion', function () {
};
instances.forEach(function (ajv) {
testCoercion(schemaArray, [ test.from ], [ test.to ]);
testCoercion(schemaObject, { foo: test.from }, { foo: test.to });
testCoercion(schemaArrObj, [ { foo: test.from } ], [ { foo: test.to } ]);
instances.forEach(function (_ajv) {
testCoercion(_ajv, schemaArray, [ test.from ], [ test.to ]);
testCoercion(_ajv, schemaObject, { foo: test.from }, { foo: test.to });
testCoercion(_ajv, schemaArrObj, [ { foo: test.from } ], [ { foo: test.to } ]);
});
function testCoercion(schema, fromData, toData) {
var valid = ajv.validate(schema, fromData);
function testCoercion(_ajv, _schema, fromData, toData) {
var valid = _ajv.validate(_schema, fromData);
//if (valid !== canCoerce) console.log(schema, fromData, toData);
valid. should.equal(canCoerce);
if (valid) fromData.should.eql(toData);
@ -260,34 +260,34 @@ describe('Type coercion', function () {
}
};
instances.forEach(function (ajv) {
instances.forEach(function (_ajv) {
var data;
ajv.validate(schema, data = { foo: '1' }) .should.equal(true);
_ajv.validate(schema, data = { foo: '1' }) .should.equal(true);
data .should.eql({ foo: 1 });
ajv.validate(schema, data = { foo: '1.5' }) .should.equal(true);
_ajv.validate(schema, data = { foo: '1.5' }) .should.equal(true);
data .should.eql({ foo: 1.5 });
ajv.validate(schema, data = { foo: 'false' }) .should.equal(true);
_ajv.validate(schema, data = { foo: 'false' }) .should.equal(true);
data .should.eql({ foo: false });
ajv.validate(schema, data = { foo: 1 }) .should.equal(true);
_ajv.validate(schema, data = { foo: 1 }) .should.equal(true);
data .should.eql({ foo: 1 }); // no coercion
ajv.validate(schema, data = { foo: true }) .should.equal(true);
_ajv.validate(schema, data = { foo: true }) .should.equal(true);
data .should.eql({ foo: true }); // no coercion
ajv.validate(schema, data = { foo: null }) .should.equal(true);
_ajv.validate(schema, data = { foo: null }) .should.equal(true);
data .should.eql({ foo: null }); // no coercion
ajv.validate(schema, data = { foo: 'abc' }) .should.equal(false);
_ajv.validate(schema, data = { foo: 'abc' }) .should.equal(false);
data .should.eql({ foo: 'abc' }); // can't coerce
ajv.validate(schema, data = { foo: {} }) .should.equal(false);
_ajv.validate(schema, data = { foo: {} }) .should.equal(false);
data .should.eql({ foo: {} }); // can't coerce
ajv.validate(schema, data = { foo: [] }) .should.equal(false);
_ajv.validate(schema, data = { foo: [] }) .should.equal(false);
data .should.eql({ foo: [] }); // can't coerce
});
});
@ -307,13 +307,13 @@ describe('Type coercion', function () {
items: { type: 'number' }
};
instances.forEach(function (ajv) {
instances.forEach(function (_ajv) {
var data = { foo: '123', bar: 'bar' };
ajv.validate(schema, data) .should.equal(false);
_ajv.validate(schema, data) .should.equal(false);
data .should.eql({ foo: 123, bar: 'bar' });
var data2 = [ '123', 'bar' ];
ajv.validate(schema2, data2) .should.equal(false);
_ajv.validate(schema2, data2) .should.equal(false);
data2 .should.eql([ 123, 'bar' ]);
});
});
@ -377,8 +377,8 @@ describe('Type coercion', function () {
testCoercion(schemaRecursive2, { foo: { foo: { foo: '1' } } },
{ foo: { foo: { foo: 1 } } });
function testCoercion(schema, fromData, toData) {
var valid = _ajv.validate(schema, fromData);
function testCoercion(_schema, fromData, toData) {
var valid = _ajv.validate(_schema, fromData);
// if (!valid) console.log(schema, fromData, toData);
valid. should.equal(true);
fromData .should.eql(toData);
@ -393,16 +393,11 @@ describe('Type coercion', function () {
var tests = rules[toType][fromType];
tests.forEach(function (test) {
var canCoerce = test.to !== undefined;
var schema;
if (canCoerce) {
if (Array.isArray(test.to)) {
schema = {type: toType, "items": { "type": fromType, "enum":[test.from] }};
} else {
schema = {type: toType, "enum": [test.to]};
}
} else {
schema = { type: toType };
}
var schema = canCoerce
? (Array.isArray(test.to)
? { "type": toType, "items": { "type": fromType, "enum": [ test.to[0] ] } }
: { "type": toType, "enum": [ test.to ] })
: { type: toType };
cb(test, schema, canCoerce, toType, fromType);
});
}

View File

@ -183,7 +183,7 @@ describe('Custom keywords', function () {
return parentSchema.exclusiveRange === true
? function (data) { return data > min && data < max; }
: function (data) { return data >= min && data <= max; }
: function (data) { return data >= min && data <= max; };
}
});
@ -210,9 +210,9 @@ describe('Custom keywords', function () {
});
it('should recursively expand macro keywords', function() {
instances.forEach(function (ajv) {
ajv.addKeyword('deepProperties', { type: 'object', macro: macroDeepProperties });
ajv.addKeyword('range', { type: 'number', macro: macroRange });
instances.forEach(function (_ajv) {
_ajv.addKeyword('deepProperties', { type: 'object', macro: macroDeepProperties });
_ajv.addKeyword('range', { type: 'number', macro: macroRange });
var schema = {
"deepProperties": {
@ -266,7 +266,7 @@ describe('Custom keywords', function () {
}
*/
var validate = ajv.compile(schema);
var validate = _ajv.compile(schema);
shouldBeValid(validate, {
a: {b: {c: 3}},
@ -288,20 +288,20 @@ describe('Custom keywords', function () {
d: {e: {f: {g: 2}}} // not string
}, 5);
function macroDeepProperties(schema) {
if (typeof schema != 'object')
function macroDeepProperties(_schema) {
if (typeof _schema != 'object')
throw new Error('schema of deepProperty should be an object');
var expanded = [];
for (var prop in schema) {
for (var prop in _schema) {
var path = prop.split('.');
var properties = {};
if (path.length == 1) {
properties[prop] = schema[prop];
properties[prop] = _schema[prop];
} else {
var deepProperties = {};
deepProperties[path.slice(1).join('.')] = schema[prop];
deepProperties[path.slice(1).join('.')] = _schema[prop];
properties[path[0]] = { "deepProperties": deepProperties };
}
expanded.push({ "properties": properties });
@ -313,17 +313,17 @@ describe('Custom keywords', function () {
});
it('should correctly expand multiple macros on the same level', function() {
instances.forEach(function (ajv) {
ajv.addKeyword('range', { type: 'number', macro: macroRange });
ajv.addKeyword('even', { type: 'number', macro: macroEven });
instances.forEach(function (_ajv) {
_ajv.addKeyword('range', { type: 'number', macro: macroRange });
_ajv.addKeyword('even', { type: 'number', macro: macroEven });
var schema = {
"range": [4,6],
"even": true
};
var validate = ajv.compile(schema);
var numErrors = ajv._opts.allErrors ? 4 : 2;
var validate = _ajv.compile(schema);
var numErrors = _ajv._opts.allErrors ? 4 : 2;
shouldBeInvalid(validate, 2, 2);
shouldBeInvalid(validate, 3, numErrors);
@ -336,15 +336,15 @@ describe('Custom keywords', function () {
});
it('should validate macro keyword when it resolves to the same keyword as exists', function() {
instances.forEach(function (ajv) {
ajv.addKeyword('range', { type: 'number', macro: macroRange });
instances.forEach(function (_ajv) {
_ajv.addKeyword('range', { type: 'number', macro: macroRange });
var schema = {
"range": [1,4],
"minimum": 2.5
};
var validate = ajv.compile(schema);
var validate = _ajv.compile(schema);
shouldBeValid(validate, 3);
shouldBeInvalid(validate, 2);
@ -352,17 +352,17 @@ describe('Custom keywords', function () {
});
it('should correctly expand macros in subschemas', function() {
instances.forEach(function (ajv) {
ajv.addKeyword('range', { type: 'number', macro: macroRange });
instances.forEach(function (_ajv) {
_ajv.addKeyword('range', { type: 'number', macro: macroRange });
var schema = {
"allOf": [
{ "range": [4,8] },
{ "range": [2,6] }
]
}
};
var validate = ajv.compile(schema);
var validate = _ajv.compile(schema);
shouldBeInvalid(validate, 2, 2);
shouldBeInvalid(validate, 3, 2);
@ -375,9 +375,9 @@ describe('Custom keywords', function () {
});
it('should correctly expand macros in macro expansions', function() {
instances.forEach(function (ajv) {
ajv.addKeyword('range', { type: 'number', macro: macroRange });
ajv.addKeyword('contains', { type: 'array', macro: macroContains });
instances.forEach(function (_ajv) {
_ajv.addKeyword('range', { type: 'number', macro: macroRange });
_ajv.addKeyword('contains', { type: 'array', macro: macroContains });
var schema = {
"contains": {
@ -387,7 +387,7 @@ describe('Custom keywords', function () {
}
};
var validate = ajv.compile(schema);
var validate = _ajv.compile(schema);
shouldBeInvalid(validate, [1,2,3], 2);
shouldBeInvalid(validate, [2,3,4], 2);
@ -396,8 +396,8 @@ describe('Custom keywords', function () {
shouldBeInvalid(validate, [7,8,9], 2);
shouldBeInvalid(validate, [8,9,10], 2);
function macroContains(schema) {
return { "not": { "items": { "not": schema } } };
function macroContains(_schema) {
return { "not": { "items": { "not": _schema } } };
}
});
});
@ -407,10 +407,10 @@ describe('Custom keywords', function () {
var schema = { "invalid": true };
should.throw(function() {
var validate = ajv.compile(schema);
ajv.compile(schema);
});
function macroInvalid(schema) {
function macroInvalid(/* schema */) {
return { "type": "invalid" };
}
});
@ -421,7 +421,7 @@ describe('Custom keywords', function () {
throw new Error('Schema for "even" keyword should be boolean');
}
function macroConstant(schema, parentSchema) {
function macroConstant(schema/*, parentSchema */) {
return { "enum": [schema] };
}
@ -684,10 +684,10 @@ describe('Custom keywords', function () {
function testEvenKeyword(definition, numErrors) {
instances.forEach(function (ajv) {
ajv.addKeyword('even', definition);
instances.forEach(function (_ajv) {
_ajv.addKeyword('even', definition);
var schema = { "even": true };
var validate = ajv.compile(schema);
var validate = _ajv.compile(schema);
shouldBeValid(validate, 2);
shouldBeValid(validate, 'abc');
@ -697,11 +697,11 @@ describe('Custom keywords', function () {
}
function testEvenKeyword$data(definition, numErrors) {
instances.forEach(function (ajv) {
ajv.addKeyword('even', definition);
instances.forEach(function (_ajv) {
_ajv.addKeyword('even', definition);
var schema = { "even": true };
var validate = ajv.compile(schema);
var validate = _ajv.compile(schema);
shouldBeValid(validate, 2);
shouldBeValid(validate, 'abc');
@ -714,7 +714,7 @@ describe('Custom keywords', function () {
"evenValue": {}
}
};
validate = ajv.compile(schema);
validate = _ajv.compile(schema);
shouldBeValid(validate, { data: 2, evenValue: true });
shouldBeInvalid(validate, { data: 2, evenValue: false });
@ -730,11 +730,11 @@ describe('Custom keywords', function () {
}
function testConstantKeyword(definition, numErrors) {
instances.forEach(function (ajv) {
ajv.addKeyword('constant', definition);
instances.forEach(function (_ajv) {
_ajv.addKeyword('constant', definition);
var schema = { "constant": "abc" };
var validate = ajv.compile(schema);
var validate = _ajv.compile(schema);
shouldBeValid(validate, 'abc');
shouldBeInvalid(validate, 2, numErrors);
@ -743,8 +743,8 @@ describe('Custom keywords', function () {
}
function testMultipleConstantKeyword(definition, numErrors) {
instances.forEach(function (ajv) {
ajv.addKeyword('constant', definition);
instances.forEach(function (_ajv) {
_ajv.addKeyword('constant', definition);
var schema = {
"properties": {
@ -754,7 +754,7 @@ describe('Custom keywords', function () {
"additionalProperties": { "constant": { "foo": "bar" } },
"items": { "constant": { "foo": "bar" } }
};
var validate = ajv.compile(schema);
var validate = _ajv.compile(schema);
shouldBeValid(validate, {a:1, b:1});
shouldBeInvalid(validate, {a:2, b:1}, numErrors);
@ -770,11 +770,11 @@ describe('Custom keywords', function () {
}
function testRangeKeyword(definition, customErrors, numErrors) {
instances.forEach(function (ajv) {
ajv.addKeyword('range', definition);
instances.forEach(function (_ajv) {
_ajv.addKeyword('range', definition);
var schema = { "range": [2, 4] };
var validate = ajv.compile(schema);
var validate = _ajv.compile(schema);
shouldBeValid(validate, 2);
shouldBeValid(validate, 3);
@ -786,7 +786,7 @@ describe('Custom keywords', function () {
shouldBeInvalid(validate, 4.01, numErrors);
if (customErrors) shouldBeRangeError(validate.errors[0], '', '#/range','<=', 4);
var schema = {
schema = {
"properties": {
"foo": {
"range": [2, 4],
@ -794,7 +794,7 @@ describe('Custom keywords', function () {
}
}
};
var validate = ajv.compile(schema);
validate = _ajv.compile(schema);
shouldBeValid(validate, { foo: 2.01 });
shouldBeValid(validate, { foo: 3 });
@ -808,8 +808,8 @@ describe('Custom keywords', function () {
}
function testMultipleRangeKeyword(definition, numErrors) {
instances.forEach(function (ajv) {
ajv.addKeyword('range', definition);
instances.forEach(function (_ajv) {
_ajv.addKeyword('range', definition);
var schema = {
"properties": {
@ -819,7 +819,7 @@ describe('Custom keywords', function () {
"additionalProperties": { "range": [5, 7] },
"items": { "range": [5, 7] }
};
var validate = ajv.compile(schema);
var validate = _ajv.compile(schema);
shouldBeValid(validate, {a:3.99, b:4});
shouldBeInvalid(validate, {a:4, b:4}, numErrors);
@ -870,9 +870,9 @@ describe('Custom keywords', function () {
}
function shouldBeInvalidSchema(schema) {
instances.forEach(function (ajv) {
instances.forEach(function (_ajv) {
should.throw(function() {
ajv.compile(schema);
_ajv.compile(schema);
});
});
}

View File

@ -1,7 +1,7 @@
'use strict';
var equal = require('../lib/compile/equal')
, should = require('./chai').should();
var equal = require('../lib/compile/equal');
require('./chai').should();
describe('equal', function() {

View File

@ -1,6 +1,5 @@
'use strict';
var Ajv = require('./ajv')
, should = require('./chai').should();
@ -65,7 +64,6 @@ describe('Validation errors', function () {
, invalidData = { foo: 1, bar: 2, baz: 3, quux: 4 };
var path = pathFunc(errorDataPath);
var msg = msgFunc(errorDataPath);
var validate = ajv.compile(schema);
shouldBeValid(validate, data);
@ -105,7 +103,7 @@ describe('Validation errors', function () {
testAdditionalIsSchema();
});
function testAdditionalIsSchema(errorDataPath) {
function testAdditionalIsSchema() {
var schema = {
properties: {
foo: { type: 'integer' },
@ -114,7 +112,7 @@ describe('Validation errors', function () {
additionalProperties: {
type: 'object',
properties: {
quux: { type: 'string' }
quux: { type: 'string' }
}
}
};
@ -190,7 +188,7 @@ describe('Validation errors', function () {
test();
var schema = { anyOf: [ schema ] };
schema = { anyOf: [ schema ] };
test(1, '#/anyOf/0');
function test(extraErrors, schemaPathPrefix) {
@ -265,7 +263,7 @@ describe('Validation errors', function () {
it('should not validate required twice in large schemas with loopRequired option', function() {
var ajv = new Ajv({ loopRequired: 1, allErrors: true });
ajv = new Ajv({ loopRequired: 1, allErrors: true });
var schema = {
properties: {
@ -283,7 +281,7 @@ describe('Validation errors', function () {
it('should not validate required twice with $data ref', function() {
var ajv = new Ajv({ v5: true, allErrors: true });
ajv = new Ajv({ v5: true, allErrors: true });
var schema = {
properties: {
@ -452,7 +450,7 @@ describe('Validation errors', function () {
items: [{ minimum: 10 }, { minimum: 9 }, { minimum: 12 }]
};
var validate = ajv.compile(schema2);
validate = ajv.compile(schema2);
shouldBeValid(validate, data);
shouldBeInvalid(validate, invalidData1);
shouldBeError(validate.errors[0], 'minimum', '#/items/0/minimum', '[0]', 'should be >= 10');
@ -475,8 +473,8 @@ describe('Validation errors', function () {
test(ajvJP);
test(fullAjv);
function test(ajv) {
var validate = ajv.compile(schema);
function test(_ajv) {
var validate = _ajv.compile(schema);
shouldBeValid(validate, data);
shouldBeInvalid(validate, invalidData);
shouldBeError(validate.errors[0], 'additionalItems', '#/additionalItems', '', 'should NOT have more than 2 items');
@ -487,20 +485,20 @@ describe('Validation errors', function () {
function testSchema1(schema, schemaPathPrefix) {
_testSchema1(ajv, schema, schemaPathPrefix);
_testSchema1(ajvJP, schema, schemaPathPrefix);
_testSchema1(fullAjv, schema, schemaPathPrefix)
_testSchema1(fullAjv, schema, schemaPathPrefix);
}
function _testSchema1(ajv, schema, schemaPathPrefix) {
function _testSchema1(_ajv, schema, schemaPathPrefix) {
var schPath = (schemaPathPrefix || '#/properties/foo') + '/type';
var data = { foo: 1 }
, invalidData = { foo: 'bar' };
var validate = ajv.compile(schema);
var validate = _ajv.compile(schema);
shouldBeValid(validate, data);
shouldBeInvalid(validate, invalidData);
shouldBeError(validate.errors[0], 'type', schPath, ajv._opts.jsonPointers ? '/foo' : '.foo');
shouldBeError(validate.errors[0], 'type', schPath, _ajv._opts.jsonPointers ? '/foo' : '.foo');
}
@ -512,7 +510,7 @@ describe('Validation errors', function () {
function shouldBeInvalid(validate, data, numErrors) {
validate(data) .should.equal(false);
should.equal(validate.errors.length, numErrors || 1)
should.equal(validate.errors.length, numErrors || 1);
}

View File

@ -32,7 +32,7 @@ describe('issue #8: schema with shared references', function() {
var result = ajv.validate('obj.json#', { foo: 'abc', bar: 'def' });
result .should.equal(true);
var result = ajv.validate('obj.json#', { foo: 'abcde', bar: 'fghg' });
result = ajv.validate('obj.json#', { foo: 'abcde', bar: 'fghg' });
result .should.equal(false);
ajv.errors .should.have.length(1);
};
@ -85,12 +85,6 @@ describe('issue #50: references with "definitions"', function () {
describe('issue #182, NaN validation', function() {
var ajv;
before(function(){
ajv = new Ajv();
});
it('should not pass minimum/maximum validation', function() {
testNaN({ minimum: 1 }, false);
testNaN({ maximum: 1 }, false);
@ -135,7 +129,7 @@ describe('issue #181, custom keyword is not validated in allErrors mode if there
testCustomKeywordErrors({
type:'object',
errors: true,
validate: function v(value) {
validate: function v(/* value */) {
return false;
}
});
@ -145,7 +139,7 @@ describe('issue #181, custom keyword is not validated in allErrors mode if there
testCustomKeywordErrors({
type:'object',
errors: true,
validate: function v(value) {
validate: function v(/* value */) {
v.errors = v.errors || [];
v.errors.push({
keyword: 'alwaysFails',
@ -371,11 +365,11 @@ describe('issue #240, mutually recursive fragment refs reference a common schema
$ref: 'schema://api.schema#resource_identifier'
}
]
}
};
ajv.addSchema(librarySchema);
ajv.addSchema(catalogItemSchema);
ajv.addSchema(catalogItemResourceIdentifierSchema)
ajv.addSchema(catalogItemResourceIdentifierSchema);
ajv.addSchema(apiSchema);
var validate = ajv.compile(domainSchema);

View File

@ -8,12 +8,12 @@ var jsonSchemaTest = require('json-schema-test')
var instances = getAjvInstances(options);
var remoteRefs = {
// for JSON-Schema-Test-Suite
'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 tests
'http://localhost:1234/name.json': require('./remotes/name.json')
// for JSON-Schema-Test-Suite
'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 tests
'http://localhost:1234/name.json': require('./remotes/name.json')
};
var remoteRefsWithIds = [
@ -54,8 +54,9 @@ jsonSchemaTest(instances, {
afterEach: function (res) {
// console.log(res.errors);
res.valid .should.be.a('boolean');
if (res.valid === true ) should.equal(res.errors, null);
else {
if (res.valid === true ) {
should.equal(res.errors, null);
} else {
res.errors .should.be.an('array');
for (var i=0; i<res.errors.length; i++)
res.errors[i] .should.be.an('object');
@ -68,8 +69,9 @@ jsonSchemaTest(instances, {
function testSuites() {
var suites;
if (typeof window == 'object') {
var suites = {
suites = {
'JSON-Schema tests draft4': require('./JSON-Schema-Test-Suite/tests/draft4/{**/,}*.json', {mode: 'list'}),
'Advanced schema tests': require('./tests/{**/,}*.json', {mode: 'list'})
};
@ -79,7 +81,7 @@ function testSuites() {
});
}
} else {
var suites = {
suites = {
'JSON-Schema tests draft4': './JSON-Schema-Test-Suite/tests/draft4/{**/,}*.json',
'Advanced schema tests': './tests/{**/,}*.json'
};

View File

@ -1,9 +1,8 @@
'use strict';
var Ajv = require('./ajv')
, getAjvInstances = require('./ajv_instances')
, should = require('./chai').should()
, should = require('./chai').should();
describe('Ajv Options', function () {
@ -61,10 +60,10 @@ describe('Ajv Options', function () {
};
ajv.validate('//test/fooBar', object).should.equal(true);
object.should.have.property('foo');
object.should.have.property('bar');
object.should.have.property('baz');
object.should.not.have.property('fizz');
object.should.have.property('foo');
object.should.have.property('bar');
object.should.have.property('baz');
object.should.not.have.property('fizz');
ajv.addSchema({
id: '//test/fooBar2',
@ -72,15 +71,15 @@ describe('Ajv Options', function () {
additionalProperties: { type: 'string', pattern: '^to-be-', maxLength: 10 }
});
var object = {
object = {
foo: 'foo', bar: 'bar', baz: 'to-be-kept', quux: 'to-be-removed', fizz: 1000
};
ajv.validate('//test/fooBar2', object).should.equal(true);
object.should.have.property('foo');
object.should.have.property('bar');
object.should.have.property('baz');
object.should.not.have.property('fizz');
object.should.have.property('foo');
object.should.have.property('bar');
object.should.have.property('baz');
object.should.not.have.property('fizz');
});
});
@ -167,32 +166,32 @@ describe('Ajv Options', function () {
it('should throw if meta: false and validateSchema: true', function() {
var ajv = new Ajv({ meta: false });
should.not.exist(ajv.getSchema('http://json-schema.org/draft-04/schema'));
should.throw(function() { ajv.addSchema({ type: 'integer' }, 'integer') });
should.throw(function() { ajv.addSchema({ type: 'integer' }, 'integer'); });
});
it('should skip schema validation with validateSchema: false', function() {
var ajv = new Ajv();
should.throw(function() { ajv.addSchema({ type: 123 }, 'integer') });
should.throw(function() { ajv.addSchema({ type: 123 }, 'integer'); });
var ajv = new Ajv({ validateSchema: false });
should.not.throw(function() { ajv.addSchema({ type: 123 }, 'integer') });
ajv = new Ajv({ validateSchema: false });
should.not.throw(function() { ajv.addSchema({ type: 123 }, 'integer'); });
var ajv = new Ajv({ validateSchema: false, meta: false });
should.not.throw(function() { ajv.addSchema({ type: 123 }, 'integer') });
ajv = new Ajv({ validateSchema: false, meta: false });
should.not.throw(function() { ajv.addSchema({ type: 123 }, 'integer'); });
});
it('should not throw on invalid schema with validateSchema: "log"', function() {
var logError = console.error;
var loggedError = false;
console.error = function() { loggedError = true; logError.apply(console, arguments); }
console.error = function() { loggedError = true; logError.apply(console, arguments); };
var ajv = new Ajv({ validateSchema: 'log' });
should.not.throw(function() { ajv.addSchema({ type: 123 }, 'integer') });
should.not.throw(function() { ajv.addSchema({ type: 123 }, 'integer'); });
loggedError .should.equal(true);
console.error = logError;
var ajv = new Ajv({ validateSchema: 'log', meta: false });
should.throw(function() { ajv.addSchema({ type: 123 }, 'integer') });
ajv = new Ajv({ validateSchema: 'log', meta: false });
should.throw(function() { ajv.addSchema({ type: 123 }, 'integer'); });
});
it('should validate v5 schema', function() {
@ -200,7 +199,7 @@ describe('Ajv Options', function () {
ajv.validateSchema({ contains: { minimum: 2 } }) .should.equal(true);
ajv.validateSchema({ contains: 2 }). should.equal(false);
var ajv = new Ajv();
ajv = new Ajv();
ajv.validateSchema({ contains: 2 }). should.equal(true);
});
@ -219,7 +218,7 @@ describe('Ajv Options', function () {
myKeyword: 2
}) .should.equal(true);
var ajv = new Ajv();
ajv = new Ajv();
ajv.validateSchema({ myKeyword: true }) .should.equal(true);
ajv.validateSchema({ myKeyword: 2 }) .should.equal(true);
});
@ -363,8 +362,8 @@ describe('Ajv Options', function () {
validateWithUnicode('😀') .should.equal(false);
validate('😀') .should.equal(true);
var validateWithUnicode = ajvUnicode.compile({ maxLength: 1 });
var validate = ajv.compile({ maxLength: 1 });
validateWithUnicode = ajvUnicode.compile({ maxLength: 1 });
validate = ajv.compile({ maxLength: 1 });
validateWithUnicode('😀') .should.equal(true);
validate('😀') .should.equal(false);
@ -408,8 +407,8 @@ describe('Ajv Options', function () {
validate(4.18) .should.equal(true);
validate(4.181) .should.equal(false);
var schema = { multipleOf: 0.0000001 };
var validate = ajv.compile(schema);
schema = { multipleOf: 0.0000001 };
validate = ajv.compile(schema);
validate(53.198098) .should.equal(true);
validate(53.1980981) .should.equal(true);
@ -449,7 +448,7 @@ describe('Ajv Options', function () {
validate(data) .should.equal(true);
data .should.eql({ foo: 'abc', bar: 1, baz: false, nil: null, obj: {}, arr:[] });
var data = { foo: 'foo', bar: 2, obj: { test: true } };
data = { foo: 'foo', bar: 2, obj: { test: true } };
validate(data) .should.equal(true);
data .should.eql({ foo: 'foo', bar: 2, baz: false, nil: null, obj: { test: true }, arr:[] });
}
@ -475,11 +474,11 @@ describe('Ajv Options', function () {
validate(data) .should.equal(true);
data .should.eql([ 'abc', 1, false ]);
var data = [ 'foo' ];
data = [ 'foo' ];
validate(data) .should.equal(true);
data .should.eql([ 'foo', 1, false ]);
var data = ['foo', 2,'false'];
data = ['foo', 2,'false'];
validate(data) .should.equal(false);
validate.errors .should.have.length(1);
data .should.eql([ 'foo', 2, 'false' ]);
@ -552,7 +551,7 @@ describe('Ajv Options', function () {
validate(1) .should.equal(false);
ajv.getSchema('str') .should.equal(validate);
var schema = { id: 'int', type: 'integer' };
schema = { id: 'int', type: 'integer' };
ajv.validate(schema, 1) .should.equal(true);
ajv.validate(schema, 'abc') .should.equal(false);
ajv.getSchema('int') .should.be.a('function');
@ -591,7 +590,7 @@ describe('Ajv Options', function () {
validate(1) .should.equal(false);
should.equal(ajv.getSchema('str'), undefined);
var schema = { id: 'int', type: 'integer' };
schema = { id: 'int', type: 'integer' };
ajv.validate(schema, 1) .should.equal(true);
ajv.validate(schema, 'abc') .should.equal(false);
should.equal(ajv.getSchema('int'), undefined);
@ -620,7 +619,7 @@ describe('Ajv Options', function () {
beforeEach(function() {
contexts = [];
})
});
describe('= true', function() {
it('should pass this value as context to custom keyword validation function', function() {
@ -644,7 +643,7 @@ describe('Ajv Options', function () {
ctx .should.equal(ajv);
});
});
})
});
function getValidate(passContext) {
ajv = new Ajv({ passContext: passContext, inlineRefs: false });

View File

@ -1,4 +1,4 @@
'use strict'
'use strict';
var Promise = require('bluebird');

View File

@ -1,6 +1,5 @@
'use strict';
var Ajv = require('./ajv')
, should = require('./chai').should()
, getAjvInstances = require('./ajv_instances');

View File

@ -24,8 +24,9 @@ jsonSchemaTest(instances, {
function testSuites() {
var suites;
if (typeof window == 'object') {
var suites = {
suites = {
'v5 proposals': require('./v5/{**/,}*.json', {mode: 'list'})
};
for (var suiteName in suites) {
@ -34,9 +35,9 @@ function testSuites() {
});
}
} else {
var suites = {
suites = {
'v5 proposals': './v5/{**/,}*.json'
}
};
}
return suites;
}