From b853218249ae03a97ec635673a39b1f7473269de Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin Date: Thu, 28 Jul 2016 00:50:30 +0100 Subject: [PATCH] style: eslint tests, closes #250 --- package.json | 2 +- spec/.eslintrc.json | 13 +++++ spec/ajv.js | 2 + spec/ajv.spec.js | 21 ++++--- spec/ajv_instances.js | 3 +- spec/ajv_options.js | 12 ++-- spec/async.spec.js | 65 +++++++++++----------- spec/async_schemas.spec.js | 25 ++++----- spec/async_validate.spec.js | 89 ++++++++++++++--------------- spec/chai.js | 2 + spec/coercion.spec.js | 73 ++++++++++++------------ spec/custom.spec.js | 108 ++++++++++++++++++------------------ spec/equal.spec.js | 4 +- spec/errors.spec.js | 28 +++++----- spec/issues.spec.js | 16 ++---- spec/json-schema.spec.js | 22 ++++---- spec/options.spec.js | 67 +++++++++++----------- spec/promise.js | 2 +- spec/resolve.spec.js | 1 - spec/v5.spec.js | 7 ++- 20 files changed, 282 insertions(+), 280 deletions(-) create mode 100644 spec/.eslintrc.json diff --git a/package.json b/package.json index 82f464a..37b05c8 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/spec/.eslintrc.json b/spec/.eslintrc.json new file mode 100644 index 0000000..b3d3b07 --- /dev/null +++ b/spec/.eslintrc.json @@ -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 + } +} diff --git a/spec/ajv.js b/spec/ajv.js index c4638a4..d91f924 100644 --- a/spec/ajv.js +++ b/spec/ajv.js @@ -1 +1,3 @@ +'use strict'; + module.exports = typeof window == 'object' ? window.Ajv : require('' + '../lib/ajv'); diff --git a/spec/ajv.spec.js b/spec/ajv.spec.js index fdd4952..57a21b3 100644 --- a/spec/ajv.spec.js +++ b/spec/ajv.spec.js @@ -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); diff --git a/spec/ajv_instances.js b/spec/ajv_instances.js index 4e9843e..f056d5a 100644 --- a/spec/ajv_instances.js +++ b/spec/ajv_instances.js @@ -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) ]; } diff --git a/spec/ajv_options.js b/spec/ajv_options.js index b8af604..a667bd7 100644 --- a/spec/ajv_options.js +++ b/spec/ajv_options.js @@ -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; diff --git a/spec/async.spec.js b/spec/async.spec.js index fa18f32..fcf7ee6 100644 --- a/spec/async.spec.js +++ b/spec/async.spec.js @@ -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); } }); diff --git a/spec/async_schemas.spec.js b/spec/async_schemas.spec.js index 9ad7b62..29380a6 100644 --- a/spec/async_schemas.spec.js +++ b/spec/async_schemas.spec.js @@ -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])); } } diff --git a/spec/async_validate.spec.js b/spec/async_validate.spec.js index ee450f7..5c2bdcb 100644 --- a/spec/async_validate.spec.js +++ b/spec/async_validate.spec.js @@ -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; - }); + }); } diff --git a/spec/chai.js b/spec/chai.js index 16998c1..b3cd0ed 100644 --- a/spec/chai.js +++ b/spec/chai.js @@ -1 +1,3 @@ +'use strict'; + module.exports = typeof window == 'object' ? window.chai : require('' + 'chai'); diff --git a/spec/coercion.spec.js b/spec/coercion.spec.js index a00e102..43d3dfe 100644 --- a/spec/coercion.spec.js +++ b/spec/coercion.spec.js @@ -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); }); } diff --git a/spec/custom.spec.js b/spec/custom.spec.js index 40e72bf..af16b50 100644 --- a/spec/custom.spec.js +++ b/spec/custom.spec.js @@ -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); }); }); } diff --git a/spec/equal.spec.js b/spec/equal.spec.js index c2b7a71..3c920a0 100644 --- a/spec/equal.spec.js +++ b/spec/equal.spec.js @@ -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() { diff --git a/spec/errors.spec.js b/spec/errors.spec.js index c6ac134..e6bddf9 100644 --- a/spec/errors.spec.js +++ b/spec/errors.spec.js @@ -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); } diff --git a/spec/issues.spec.js b/spec/issues.spec.js index 1706fe9..4c30c8d 100644 --- a/spec/issues.spec.js +++ b/spec/issues.spec.js @@ -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); diff --git a/spec/json-schema.spec.js b/spec/json-schema.spec.js index bf7a727..45e3786 100644 --- a/spec/json-schema.spec.js +++ b/spec/json-schema.spec.js @@ -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