From 2243aa8588fbce804618cb31992d6e3ec4209250 Mon Sep 17 00:00:00 2001 From: Martin Hansen Date: Wed, 17 Feb 2021 15:36:46 +0100 Subject: [PATCH] Eslint fixes. var to const/let, additional fixes and formatting --- .travis.yml | 4 + README.md | 2 +- src/index.js | 221 ++++++++++++++++---------------- test/specs/if-then-else.spec.js | 14 +- test/specs/index.spec.js | 145 ++++++++++----------- test/specs/items.spec.js | 20 +-- test/specs/meta-schema.spec.js | 16 +-- test/specs/options.spec.js | 40 +++--- test/specs/properties.spec.js | 80 ++++++------ 9 files changed, 273 insertions(+), 269 deletions(-) diff --git a/.travis.yml b/.travis.yml index e7ac897..805176c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,3 +2,7 @@ language: node_js after_success: npm run coverage node_js: - 10 + - 12 + - 14 + - node + - lts/* diff --git a/README.md b/README.md index 7523d15..eeba4f3 100644 --- a/README.md +++ b/README.md @@ -122,7 +122,7 @@ Those two resolvers are expected to return an object containing the resolved val Also the resolve function is not passed **mergeSchemas**, but an object **mergers** that contains mergers for each of the related keywords. So properties get passed an object like this: ```js -var mergers = { +const mergers = { properties: function mergeSchemas(schemas, childSchemaName){...}, patternProperties: function mergeSchemas(schemas, childSchemaName){...}, additionalProperties: function mergeSchemas(schemas){...}, diff --git a/src/index.js b/src/index.js index 98f506d..9ace777 100644 --- a/src/index.js +++ b/src/index.js @@ -1,47 +1,48 @@ -var cloneDeep = require('lodash/cloneDeep') -var compare = require('json-schema-compare') -var computeLcm = require('compute-lcm') -var defaultsDeep = require('lodash/defaultsDeep') -var flatten = require('lodash/flatten') -var flattenDeep = require('lodash/flattenDeep') -var intersection = require('lodash/intersection') -var intersectionWith = require('lodash/intersectionWith') -var isEqual = require('lodash/isEqual') -var isPlainObject = require('lodash/isPlainObject') -var pullAll = require('lodash/pullAll') -var sortBy = require('lodash/sortBy') -var forEach = require('lodash/forEach') -var uniq = require('lodash/uniq') -var uniqWith = require('lodash/uniqWith') -var without = require('lodash/without') - -var withoutArr = (arr, ...rest) => without.apply(null, [arr].concat(flatten(rest))) -var isPropertyRelated = (key) => contains(propertyRelated, key) -var isItemsRelated = (key) => contains(itemsRelated, key) -var isConditionalRelated = (key) => contains(conditonalRelated, key) -var contains = (arr, val) => arr.indexOf(val) !== -1 -var isEmptySchema = (obj) => (!keys(obj).length) && obj !== false && obj !== true -var isSchema = (val) => isPlainObject(val) || val === true || val === false -var isFalse = (val) => val === false -var isTrue = (val) => val === true -var schemaResolver = (compacted, key, mergeSchemas) => mergeSchemas(compacted) -var stringArray = (values) => sortBy(uniq(flattenDeep(values))) -var notUndefined = (val) => val !== undefined -var allUniqueKeys = (arr) => uniq(flattenDeep(arr.map(keys))) +const cloneDeep = require('lodash/cloneDeep') +const compare = require('json-schema-compare') +const computeLcm = require('compute-lcm') +const defaultsDeep = require('lodash/defaultsDeep') +const flatten = require('lodash/flatten') +const flattenDeep = require('lodash/flattenDeep') +const intersection = require('lodash/intersection') +const intersectionWith = require('lodash/intersectionWith') +const isEqual = require('lodash/isEqual') +const isPlainObject = require('lodash/isPlainObject') +const pullAll = require('lodash/pullAll') +const sortBy = require('lodash/sortBy') +const forEach = require('lodash/forEach') +const uniq = require('lodash/uniq') +const uniqWith = require('lodash/uniqWith') +const without = require('lodash/without') + +const has = (obj, propName) => Object.prototype.hasOwnProperty.call(obj, propName) +const withoutArr = (arr, ...rest) => without.apply(null, [arr].concat(flatten(rest))) +const isPropertyRelated = (key) => contains(propertyRelated, key) +const isItemsRelated = (key) => contains(itemsRelated, key) +const isConditionalRelated = (key) => contains(conditonalRelated, key) +const contains = (arr, val) => arr.indexOf(val) !== -1 +const isEmptySchema = (obj) => (!keys(obj).length) && obj !== false && obj !== true +const isSchema = (val) => isPlainObject(val) || val === true || val === false +const isFalse = (val) => val === false +const isTrue = (val) => val === true +const schemaResolver = (compacted, key, mergeSchemas) => mergeSchemas(compacted) +const stringArray = (values) => sortBy(uniq(flattenDeep(values))) +const notUndefined = (val) => val !== undefined +const allUniqueKeys = (arr) => uniq(flattenDeep(arr.map(keys))) // resolvers -var first = compacted => compacted[0] -var required = compacted => stringArray(compacted) -var maximumValue = compacted => Math.max.apply(Math, compacted) -var minimumValue = compacted => Math.min.apply(Math, compacted) -var uniqueItems = compacted => compacted.some(isTrue) -var examples = compacted => uniqWith(flatten(compacted), isEqual) +const first = compacted => compacted[0] +const required = compacted => stringArray(compacted) +const maximumValue = compacted => Math.max.apply(Math, compacted) +const minimumValue = compacted => Math.min.apply(Math, compacted) +const uniqueItems = compacted => compacted.some(isTrue) +const examples = compacted => uniqWith(flatten(compacted), isEqual) function compareProp(key) { return function(a, b) { return compare({ [key]: a - }, {[key]: b}) + }, { [key]: b }) } } @@ -58,19 +59,21 @@ function getValues(schemas, key) { function getItemSchemas(subSchemas, key) { return subSchemas.map(function(sub) { if (!sub) { - return + return undefined } if (Array.isArray(sub.items)) { - var schemaAtPos = sub.items[key] + const schemaAtPos = sub.items[key] if (isSchema(schemaAtPos)) { return schemaAtPos - } else if (sub.hasOwnProperty('additionalItems')) { + } else if (has(sub, 'additionalItems')) { return sub.additionalItems } } else { return sub.items } + + return undefined }) } @@ -87,7 +90,7 @@ function tryMergeSchemaGroups(schemaGroups, mergeSchemas) { function getAdditionalSchemas(subSchemas) { return subSchemas.map(function(sub) { if (!sub) { - return + return undefined } if (Array.isArray(sub.items)) { return sub.additionalItems @@ -110,8 +113,8 @@ function getAnyOfCombinations(arrOfArrays, combinations) { return combinations } - var values = arrOfArrays.slice(0).shift() - var rest = arrOfArrays.slice(1) + const values = arrOfArrays.slice(0).shift() + const rest = arrOfArrays.slice(1) if (combinations.length) { return getAnyOfCombinations(rest, flatten(combinations.map(combination => values.map(item => ([item].concat(combination)))))) } @@ -128,7 +131,7 @@ function mergeWithArray(base, newItems) { } function throwIncompatible(values, paths) { - var asJSON + let asJSON try { asJSON = values.map(function(val) { return JSON.stringify(val, null, 2) @@ -141,8 +144,8 @@ function throwIncompatible(values, paths) { function cleanupReturnValue(returnObject) { // cleanup empty - for (var prop in returnObject) { - if (returnObject.hasOwnProperty(prop) && isEmptySchema(returnObject[prop])) { + for (const prop in returnObject) { + if (has(returnObject, prop) && isEmptySchema(returnObject[prop])) { delete returnObject[prop] } } @@ -161,12 +164,12 @@ function createRequiredSubMerger(mergeSchemas, key, parents) { function callGroupResolver(keys, resolverName, schemas, mergeSchemas, options, parents) { if (keys.length) { - var resolver = options.resolvers[resolverName] + const resolver = options.resolvers[resolverName] if (!resolver) { throw new Error('No resolver found for ' + resolverName) } - var compacted = uniqWith(schemas.map(function(schema) { + const compacted = uniqWith(schemas.map(function(schema) { return keys.reduce(function(all, key) { if (schema[key] !== undefined) { all[key] = schema[key] @@ -184,7 +187,7 @@ function callGroupResolver(keys, resolverName, schemas, mergeSchemas, options, p const isIf = resolverName === 'if' const related = map[resolverName] - var mergers = related.reduce(function(all, key) { + const mergers = related.reduce(function(all, key) { if (contains(schemaGroupProps, key)) { all[key] = createRequiredSubMerger(mergeSchemas, key, parents) } else { @@ -202,7 +205,7 @@ function callGroupResolver(keys, resolverName, schemas, mergeSchemas, options, p } } - var result = resolver(compacted, parents.concat(resolverName), mergers, options) + const result = resolver(compacted, parents.concat(resolverName), mergers, options) if (!isPlainObject(result)) { throwIncompatible(compacted, parents.concat(resolverName)) @@ -218,13 +221,13 @@ function callGroupResolver(keys, resolverName, schemas, mergeSchemas, options, p // Provide source when array function mergeSchemaGroup(group, mergeSchemas, source) { - var allKeys = allUniqueKeys(source || group) - var extractor = source + const allKeys = allUniqueKeys(source || group) + const extractor = source ? getItemSchemas : getValues return allKeys.reduce(function(all, key) { - var schemas = extractor(group, key) - var compacted = uniqWith(schemas.filter(notUndefined), compare) + const schemas = extractor(group, key) + const compacted = uniqWith(schemas.filter(notUndefined), compare) all[key] = mergeSchemas(compacted, key) return all }, source @@ -249,15 +252,15 @@ function removeFalseSchemasFromArray(target) { } function createRequiredMetaArray(arr) { - return {required: arr} + return { required: arr } } -var propertyRelated = ['properties', 'patternProperties', 'additionalProperties'] -var itemsRelated = ['items', 'additionalItems'] -var conditonalRelated = ['if', 'then', 'else'] -var schemaGroupProps = ['properties', 'patternProperties', 'definitions', 'dependencies'] -var schemaArrays = ['anyOf', 'oneOf'] -var schemaProps = [ +const propertyRelated = ['properties', 'patternProperties', 'additionalProperties'] +const itemsRelated = ['items', 'additionalItems'] +const conditonalRelated = ['if', 'then', 'else'] +const schemaGroupProps = ['properties', 'patternProperties', 'definitions', 'dependencies'] +const schemaArrays = ['anyOf', 'oneOf'] +const schemaProps = [ 'additionalProperties', 'additionalItems', 'contains', @@ -266,15 +269,15 @@ var schemaProps = [ 'items' ] -var defaultResolvers = { +const defaultResolvers = { type(compacted) { if (compacted.some(Array.isArray)) { - var normalized = compacted.map(function(val) { + const normalized = compacted.map(function(val) { return Array.isArray(val) ? val : [val] }) - var common = intersection.apply(null, normalized) + const common = intersection.apply(null, normalized) if (common.length === 1) { return common[0] @@ -287,14 +290,14 @@ var defaultResolvers = { // first get rid of all non permitted properties if (!options.ignoreAdditionalProperties) { values.forEach(function(subSchema) { - var otherSubSchemas = values.filter(s => s !== subSchema) - var ownKeys = keys(subSchema.properties) - var ownPatternKeys = keys(subSchema.patternProperties) - var ownPatterns = ownPatternKeys.map(k => new RegExp(k)) + const otherSubSchemas = values.filter(s => s !== subSchema) + const ownKeys = keys(subSchema.properties) + const ownPatternKeys = keys(subSchema.patternProperties) + const ownPatterns = ownPatternKeys.map(k => new RegExp(k)) otherSubSchemas.forEach(function(other) { - var allOtherKeys = keys(other.properties) - var keysMatchingPattern = allOtherKeys.filter(k => ownPatterns.some(pk => pk.test(k))) - var additionalKeys = withoutArr(allOtherKeys, ownKeys, keysMatchingPattern) + const allOtherKeys = keys(other.properties) + const keysMatchingPattern = allOtherKeys.filter(k => ownPatterns.some(pk => pk.test(k))) + const additionalKeys = withoutArr(allOtherKeys, ownKeys, keysMatchingPattern) additionalKeys.forEach(function(key) { other.properties[key] = mergers.properties([ other.properties[key], subSchema.additionalProperties @@ -305,19 +308,19 @@ var defaultResolvers = { // remove disallowed patternProperties values.forEach(function(subSchema) { - var otherSubSchemas = values.filter(s => s !== subSchema) - var ownPatternKeys = keys(subSchema.patternProperties) + const otherSubSchemas = values.filter(s => s !== subSchema) + const ownPatternKeys = keys(subSchema.patternProperties) if (subSchema.additionalProperties === false) { otherSubSchemas.forEach(function(other) { - var allOtherPatterns = keys(other.patternProperties) - var additionalPatternKeys = withoutArr(allOtherPatterns, ownPatternKeys) + const allOtherPatterns = keys(other.patternProperties) + const additionalPatternKeys = withoutArr(allOtherPatterns, ownPatternKeys) additionalPatternKeys.forEach(key => delete other.patternProperties[key]) }) } }) } - var returnObject = { + const returnObject = { additionalProperties: mergers.additionalProperties(values.map(s => s.additionalProperties)), patternProperties: mergeSchemaGroup(values.map(s => s.patternProperties), mergers.patternProperties), properties: mergeSchemaGroup(values.map(s => s.properties), mergers.properties) @@ -330,21 +333,21 @@ var defaultResolvers = { return returnObject }, dependencies(compacted, paths, mergeSchemas) { - var allChildren = allUniqueKeys(compacted) + const allChildren = allUniqueKeys(compacted) return allChildren.reduce(function(all, childKey) { - var childSchemas = getValues(compacted, childKey) - var innerCompacted = uniqWith(childSchemas.filter(notUndefined), isEqual) + const childSchemas = getValues(compacted, childKey) + let innerCompacted = uniqWith(childSchemas.filter(notUndefined), isEqual) // to support dependencies - var innerArrays = innerCompacted.filter(Array.isArray) + const innerArrays = innerCompacted.filter(Array.isArray) if (innerArrays.length) { if (innerArrays.length === innerCompacted.length) { all[childKey] = stringArray(innerCompacted) } else { - var innerSchemas = innerCompacted.filter(isSchema) - var arrayMetaScheams = innerArrays.map(createRequiredMetaArray) + const innerSchemas = innerCompacted.filter(isSchema) + const arrayMetaScheams = innerArrays.map(createRequiredMetaArray) all[childKey] = mergeSchemas(innerSchemas.concat(arrayMetaScheams), childKey) } return all @@ -357,9 +360,9 @@ var defaultResolvers = { }, {}) }, items(values, paths, mergers) { - var items = values.map(s => s.items) - var itemsCompacted = items.filter(notUndefined) - var returnObject = {} + const items = values.map(s => s.items) + const itemsCompacted = items.filter(notUndefined) + const returnObject = {} if (itemsCompacted.every(isSchema)) { returnObject.items = mergers.items(items) @@ -367,7 +370,7 @@ var defaultResolvers = { returnObject.items = mergeSchemaGroup(values, mergers.itemsArray, items) } - var schemasAtLastPos + let schemasAtLastPos if (itemsCompacted.every(Array.isArray)) { schemasAtLastPos = values.map(s => s.additionalItems) } else if (itemsCompacted.some(Array.isArray)) { @@ -385,23 +388,23 @@ var defaultResolvers = { return returnObject }, oneOf(compacted, paths, mergeSchemas) { - var combinations = getAnyOfCombinations(cloneDeep(compacted)) - var result = tryMergeSchemaGroups(combinations, mergeSchemas) - var unique = uniqWith(result, compare) + const combinations = getAnyOfCombinations(cloneDeep(compacted)) + const result = tryMergeSchemaGroups(combinations, mergeSchemas) + const unique = uniqWith(result, compare) if (unique.length) { return unique } }, not(compacted) { - return {anyOf: compacted} + return { anyOf: compacted } }, pattern(compacted) { return compacted.map(r => '(?=' + r + ')').join('') }, multipleOf(compacted) { - var integers = compacted.slice(0) - var factor = 1 + let integers = compacted.slice(0) + let factor = 1 while (integers.some(n => !Number.isInteger(n))) { integers = integers.map(n => n * 10) factor = factor * 10 @@ -409,22 +412,22 @@ var defaultResolvers = { return computeLcm(integers) / factor }, enum(compacted) { - var enums = intersectionWith.apply(null, compacted.concat(isEqual)) + const enums = intersectionWith.apply(null, compacted.concat(isEqual)) if (enums.length) { return sortBy(enums) } }, if(values, props, mergers, options) { const allWithConditional = values.filter(schema => - conditonalRelated.some(keyword => schema.hasOwnProperty(keyword))) + conditonalRelated.some(keyword => has(schema, keyword))) // merge sub schemas completely // if,then,else must not be merged to the base schema, but if they contain allOf themselves, that should be merged function merge(schema) { const obj = {} - if (schema.hasOwnProperty('if')) obj.if = mergers.if([schema.if]) - if (schema.hasOwnProperty('then')) obj.then = mergers.then([schema.then]) - if (schema.hasOwnProperty('else')) obj.else = mergers.else([schema.else]) + if (has(schema, 'if')) obj.if = mergers.if([schema.if]) + if (has(schema, 'then')) obj.then = mergers.then([schema.then]) + if (has(schema, 'else')) obj.else = mergers.else([schema.else]) return obj } @@ -474,7 +477,7 @@ function merger(rootSchema, options, totalSchemas) { function mergeSchemas(schemas, base, parents) { schemas = cloneDeep(schemas.filter(notUndefined)) parents = parents || [] - var merged = isPlainObject(base) + const merged = isPlainObject(base) ? base : {} @@ -494,25 +497,25 @@ function merger(rootSchema, options, totalSchemas) { // there are no false and we don't need the true ones as they accept everything schemas = schemas.filter(isPlainObject) - var allKeys = allUniqueKeys(schemas) + const allKeys = allUniqueKeys(schemas) if (options.deep && contains(allKeys, 'allOf')) { return merger({ allOf: schemas }, options, totalSchemas) } - var propertyKeys = allKeys.filter(isPropertyRelated) + const propertyKeys = allKeys.filter(isPropertyRelated) pullAll(allKeys, propertyKeys) - var itemKeys = allKeys.filter(isItemsRelated) + const itemKeys = allKeys.filter(isItemsRelated) pullAll(allKeys, itemKeys) - var conditonalKeys = allKeys.filter(isConditionalRelated) + const conditonalKeys = allKeys.filter(isConditionalRelated) pullAll(allKeys, conditonalKeys) allKeys.forEach(function(key) { - var values = getValues(schemas, key) - var compacted = uniqWith(values.filter(notUndefined), compareProp(key)) + const values = getValues(schemas, key) + const compacted = uniqWith(values.filter(notUndefined), compareProp(key)) // arrayprops like anyOf and oneOf must be merged first, as they contains schemas // allOf is treated differently alltogether @@ -524,13 +527,13 @@ function merger(rootSchema, options, totalSchemas) { } else if (compacted.length === 1 && !contains(schemaGroupProps, key) && !contains(schemaProps, key)) { merged[key] = compacted[0] } else { - var resolver = options.resolvers[key] || options.resolvers.defaultResolver + const resolver = options.resolvers[key] || options.resolvers.defaultResolver if (!resolver) { throw new Error('No resolver found for key ' + key + '. You can provide a resolver for this keyword in the options, or provide a default resolver.') } - var merger + let merger // get custom merger for groups if (contains(schemaGroupProps, key) || contains(schemaArrays, key)) { merger = createRequiredSubMerger(mergeSchemas, key, parents) @@ -540,7 +543,7 @@ function merger(rootSchema, options, totalSchemas) { } } - var calledWithArray = false + let calledWithArray = false const reportUnresolved = unresolvedSchemas => { calledWithArray = Array.isArray(unresolvedSchemas) return addToAllOf(unresolvedSchemas) @@ -567,8 +570,8 @@ function merger(rootSchema, options, totalSchemas) { return merged } - var allSchemas = flattenDeep(getAllOf(rootSchema)) - var merged = mergeSchemas(allSchemas) + const allSchemas = flattenDeep(getAllOf(rootSchema)) + const merged = mergeSchemas(allSchemas) return merged } diff --git a/test/specs/if-then-else.spec.js b/test/specs/if-then-else.spec.js index d5fe6a9..7e2a3be 100644 --- a/test/specs/if-then-else.spec.js +++ b/test/specs/if-then-else.spec.js @@ -1,12 +1,8 @@ -var chai = require('chai') -var merger = require('../../src') -var sinon = require('sinon') -var _ = require('lodash') -var expect = chai.expect -var Ajv = require('ajv').default +const chai = require('chai') +const merger = require('../../src') +const expect = chai.expect -var ajv = new Ajv() -describe.only('if then else', function() { +describe('if then else', function() { it('moves the if then else to the base schema if none there', () => { const result = merger({ allOf: [{ @@ -264,7 +260,7 @@ describe.only('if then else', function() { }, { resolvers: { foo(values, paths, mergeSchemas, options, reportUnresolved) { - var key = paths.pop() + const key = paths.pop() reportUnresolved(values.map((val) => { return { [key]: val diff --git a/test/specs/index.spec.js b/test/specs/index.spec.js index d0fec31..d293c18 100644 --- a/test/specs/index.spec.js +++ b/test/specs/index.spec.js @@ -1,14 +1,14 @@ -var chai = require('chai') -var mergerModule = require('../../src') -var Ajv = require('ajv').default -var _ = require('lodash') -var $RefParser = require('json-schema-ref-parser') +const chai = require('chai') +const mergerModule = require('../../src') +const Ajv = require('ajv').default +const _ = require('lodash') +const $RefParser = require('json-schema-ref-parser') -var expect = chai.expect -var ajv = new Ajv() +const expect = chai.expect +const ajv = new Ajv() function merger(schema, options) { - var result = mergerModule(schema, options) + const result = mergerModule(schema, options) try { if (!ajv.validateSchema(result)) { throw new Error('Schema returned by resolver isn\'t valid.') @@ -23,14 +23,14 @@ function merger(schema, options) { describe('module', function() { it('merges schema with same object reference multiple places', () => { - var commonSchema = { + const commonSchema = { allOf: [{ properties: { test: true } }] } - var result = merger({ + const result = merger({ properties: { list: { items: commonSchema @@ -54,7 +54,7 @@ describe('module', function() { }) it('does not alter original schema', () => { - var schema = { + const schema = { allOf: [{ properties: { test: true @@ -62,7 +62,7 @@ describe('module', function() { }] } - var result = merger(schema) + const result = merger(schema) expect(result).to.eql({ properties: { @@ -103,6 +103,7 @@ describe('module', function() { function innerDeconstruct(schema) { const allChildObj = Object.entries(schema).map(([key, val]) => { if (_.isObject(val)) return innerDeconstruct(val) + else return undefined }) return [schema, ..._.flatten(allChildObj)] } @@ -118,7 +119,7 @@ describe('module', function() { }) it('combines simple usecase', function() { - var result = merger({ + const result = merger({ allOf: [{ type: 'string', minLength: 1 @@ -136,7 +137,7 @@ describe('module', function() { }) it('combines without allOf', function() { - var result = merger({ + const result = merger({ properties: { foo: { type: 'string' @@ -155,7 +156,7 @@ describe('module', function() { describe('simple resolve functionality', function() { it('merges with default resolver if not defined resolver', function() { - var result = merger({ + const result = merger({ title: 'schema1', allOf: [{ title: 'schema2' @@ -168,7 +169,7 @@ describe('module', function() { title: 'schema1' }) - var result3 = merger({ + const result3 = merger({ allOf: [{ title: 'schema2' }, { @@ -182,7 +183,7 @@ describe('module', function() { }) it('merges minLength if conflict', function() { - var result = merger({ + const result = merger({ allOf: [{ minLength: 1 }, { @@ -196,7 +197,7 @@ describe('module', function() { }) it('merges minimum if conflict', function() { - var result = merger({ + const result = merger({ allOf: [{ minimum: 1 }, { @@ -210,7 +211,7 @@ describe('module', function() { }) it('merges exclusiveMinimum if conflict', function() { - var result = merger({ + const result = merger({ allOf: [{ exclusiveMinimum: 1 }, { @@ -224,7 +225,7 @@ describe('module', function() { }) it('merges minItems if conflict', function() { - var result = merger({ + const result = merger({ allOf: [{ minItems: 1 }, { @@ -238,7 +239,7 @@ describe('module', function() { }) it('merges maximum if conflict', function() { - var result = merger({ + const result = merger({ allOf: [{ maximum: 1 }, { @@ -252,7 +253,7 @@ describe('module', function() { }) it('merges exclusiveMaximum if conflict', function() { - var result = merger({ + const result = merger({ allOf: [{ exclusiveMaximum: 1 }, { @@ -266,7 +267,7 @@ describe('module', function() { }) it('merges maxItems if conflict', function() { - var result = merger({ + const result = merger({ allOf: [{ maxItems: 1 }, { @@ -280,7 +281,7 @@ describe('module', function() { }) it('merges maxLength if conflict', function() { - var result = merger({ + const result = merger({ allOf: [{ maxLength: 4 }, { @@ -294,7 +295,7 @@ describe('module', function() { }) it('merges uniqueItems to most restrictive if conflict', function() { - var result = merger({ + const result = merger({ allOf: [{ uniqueItems: true }, { @@ -330,7 +331,7 @@ describe('module', function() { }) it('merges type if conflict', function() { - var result = merger({ + const result = merger({ allOf: [{}, { type: ['string', 'null', 'object', 'array'] }, { @@ -344,7 +345,7 @@ describe('module', function() { type: ['string', 'null'] }) - var result2 = merger({ + const result2 = merger({ allOf: [{}, { type: ['string', 'null', 'object', 'array'] }, { @@ -370,7 +371,7 @@ describe('module', function() { }) it('merges enum', function() { - var result = merger({ + const result = merger({ allOf: [{}, { enum: [ 'string', @@ -430,7 +431,7 @@ describe('module', function() { }) it('merges const', function() { - var result = merger({ + const result = merger({ allOf: [{}, { const: ['string', {}] }, { @@ -444,7 +445,7 @@ describe('module', function() { }) it('merges anyOf', function() { - var result = merger({ + const result = merger({ allOf: [{ anyOf: [{ required: ['123'] @@ -468,7 +469,7 @@ describe('module', function() { }) it('merges anyOf by finding valid combinations', function() { - var result = merger({ + const result = merger({ allOf: [{ anyOf: [{ type: ['null', 'string', 'array'] @@ -496,7 +497,7 @@ describe('module', function() { }) it.skip('extracts common logic', function() { - var result = merger({ + const result = merger({ allOf: [{ anyOf: [{ type: [ @@ -533,7 +534,7 @@ describe('module', function() { }) it.skip('merges anyOf into main schema if left with only one combination', function() { - var result = merger({ + const result = merger({ required: ['abc'], allOf: [{ anyOf: [{ @@ -554,7 +555,7 @@ describe('module', function() { }) it('merges nested allOf if inside singular anyOf', function() { - var result = merger({ + const result = merger({ allOf: [{ anyOf: [{ required: ['123'], @@ -611,7 +612,7 @@ describe('module', function() { }) it('merges more complex oneOf', function() { - var result = merger({ + const result = merger({ allOf: [{ oneOf: [{ type: [ @@ -634,28 +635,28 @@ describe('module', function() { }) expect(result).to.eql({ - 'oneOf': [{ - 'type': 'string', - 'required': ['123'] + oneOf: [{ + type: 'string', + required: ['123'] }, { - 'type': [ + type: [ 'object', 'array' ], - 'required': ['123', 'abc'] + required: ['123', 'abc'] }, { - 'type': ['string'], - 'required': ['abc'] + type: ['string'], + required: ['abc'] }, { - 'type': [ + type: [ 'object', 'array' ], - 'required': ['abc'] + required: ['abc'] }] }) }) it('merges nested allOf if inside singular oneOf', function() { - var result = merger({ + const result = merger({ allOf: [{ type: [ 'array', 'string', 'number' @@ -682,7 +683,7 @@ describe('module', function() { }) it('merges nested allOf if inside multiple oneOf', function() { - var result = merger({ + const result = merger({ allOf: [{ type: [ 'array', 'string', 'number' @@ -755,7 +756,7 @@ describe('module', function() { // not ready to implement this yet it.skip('merges singular oneOf', function() { - var result = merger({ + const result = merger({ properties: { name: { type: 'string' @@ -804,7 +805,7 @@ describe('module', function() { }) it('merges not using allOf', function() { - var result = merger({ + const result = merger({ allOf: [{ not: { properties: { @@ -846,7 +847,7 @@ describe('module', function() { }) it('merges contains', function() { - var result = merger({ + const result = merger({ allOf: [{}, { contains: { properties: { @@ -881,7 +882,7 @@ describe('module', function() { }) it('merges pattern using allOf', function() { - var result = merger({ + const result = merger({ allOf: [{}, { pattern: 'fdsaf' }, { @@ -893,7 +894,7 @@ describe('module', function() { pattern: '(?=fdsaf)(?=abba)' }) - var result2 = merger({ + const result2 = merger({ allOf: [{ pattern: 'abba' }] @@ -907,7 +908,7 @@ describe('module', function() { it('extracts pattern from anyOf and oneOf using | operator in regexp') it.skip('merges multipleOf using allOf or direct assignment', function() { - var result = merger({ + const result = merger({ allOf: [{ title: 'foo', type: [ @@ -930,7 +931,7 @@ describe('module', function() { }] }) - var result2 = merger({ + const result2 = merger({ allOf: [{ multipleOf: 1 }] @@ -942,7 +943,7 @@ describe('module', function() { }) it('merges multipleOf by finding lowest common multiple (LCM)', function() { - var result = merger({ + const result = merger({ allOf: [{}, { multipleOf: 0.2, allOf: [{ @@ -1317,7 +1318,7 @@ describe('module', function() { describe.skip('merging definitions', function() { it('merges circular', function() { - var schema = { + const schema = { properties: { person: { properties: { @@ -1347,7 +1348,7 @@ describe('module', function() { schema.properties.person.properties.child = schema.properties.person - var expected = { + const expected = { person: { properties: { name: { @@ -1364,7 +1365,7 @@ describe('module', function() { expected.person.properties.child = expected.person - var result = merger(schema) + const result = merger(schema) expect(result).to.eql({ properties: expected @@ -1372,7 +1373,7 @@ describe('module', function() { }) it('merges any definitions and circular', function() { - var schema = { + const schema = { properties: { person: { $ref: '#/definitions/person' @@ -1409,7 +1410,7 @@ describe('module', function() { } return $RefParser.dereference(schema).then(function(dereferenced) { - var expected = { + const expected = { person: { properties: { name: { @@ -1426,7 +1427,7 @@ describe('module', function() { expected.person.properties.child = expected.person - var result = merger(schema) + const result = merger(schema) expect(result).to.eql({ properties: expected, @@ -1443,9 +1444,9 @@ describe('module', function() { describe('dependencies', function() { it('merges simliar schemas', function() { - var result = merger({ + const result = merger({ dependencies: { - 'foo': { + foo: { type: [ 'string', 'null', 'integer' ], @@ -1453,11 +1454,11 @@ describe('module', function() { minimum: 5 }] }, - 'bar': ['prop1', 'prop2'] + bar: ['prop1', 'prop2'] }, allOf: [{ dependencies: { - 'foo': { + foo: { type: [ 'string', 'null' ], @@ -1465,28 +1466,28 @@ describe('module', function() { minimum: 7 }] }, - 'bar': ['prop4'] + bar: ['prop4'] } }] }) expect(result).to.eql({ dependencies: { - 'foo': { + foo: { type: [ 'string', 'null' ], minimum: 7 }, - 'bar': ['prop1', 'prop2', 'prop4'] + bar: ['prop1', 'prop2', 'prop4'] } }) }) it('merges mixed mode dependency', function() { - var result = merger({ + const result = merger({ dependencies: { - 'bar': { + bar: { type: [ 'string', 'null', 'integer' ], @@ -1495,14 +1496,14 @@ describe('module', function() { }, allOf: [{ dependencies: { - 'bar': ['prop4'] + bar: ['prop4'] } }] }) expect(result).to.eql({ dependencies: { - 'bar': { + bar: { type: [ 'string', 'null', 'integer' ], @@ -1515,7 +1516,7 @@ describe('module', function() { describe('propertyNames', function() { it('merges simliar schemas', function() { - var result = merger({ + const result = merger({ propertyNames: { type: 'string', allOf: [{ diff --git a/test/specs/items.spec.js b/test/specs/items.spec.js index 81c9f01..2ded916 100644 --- a/test/specs/items.spec.js +++ b/test/specs/items.spec.js @@ -1,10 +1,10 @@ -var chai = require('chai') -var merger = require('../../src') -var expect = chai.expect +const chai = require('chai') +const merger = require('../../src') +const expect = chai.expect describe('items', function() { it('merges additionalItems', function() { - var result = merger({ + const result = merger({ items: { type: 'object' }, @@ -49,7 +49,7 @@ describe('items', function() { describe('when single schema', function() { it('merges them', function() { - var result = merger({ + const result = merger({ items: { type: 'string', allOf: [{ @@ -80,7 +80,7 @@ describe('items', function() { describe('when array', function() { it('merges them in when additionalItems are all undefined', function() { - var result = merger({ + const result = merger({ items: [{ type: 'string', allOf: [{ @@ -110,7 +110,7 @@ describe('items', function() { }) it('merges in additionalItems from one if present', function() { - var result = merger({ + const result = merger({ allOf: [{ items: [{ type: 'string', @@ -142,7 +142,7 @@ describe('items', function() { }) it('merges in additionalItems from one if present', function() { - var result = merger({ + const result = merger({ allOf: [{ items: [{ type: 'string', @@ -175,7 +175,7 @@ describe('items', function() { }) it('merges in additionalItems schema', function() { - var result = merger({ + const result = merger({ allOf: [{ items: [{ type: 'string', @@ -222,7 +222,7 @@ describe('items', function() { describe('when mixed array and object', function() { it('merges in additionalItems schema', function() { - var result = merger({ + const result = merger({ // This should be ignored according to spec when items absent additionalItems: { type: 'integer', diff --git a/test/specs/meta-schema.spec.js b/test/specs/meta-schema.spec.js index edb832d..385b14b 100644 --- a/test/specs/meta-schema.spec.js +++ b/test/specs/meta-schema.spec.js @@ -1,11 +1,11 @@ -var chai = require('chai') -var merger = require('../../src') -var _ = require('lodash') -var $RefParser = require('json-schema-ref-parser') -var metaSchema = require('../fixtures/schemas/meta-schema-v6.json') +const chai = require('chai') +const merger = require('../../src') +const _ = require('lodash') +const $RefParser = require('json-schema-ref-parser') +const metaSchema = require('../fixtures/schemas/meta-schema-v6.json') -var expect = chai.expect -var schema +const expect = chai.expect +let schema describe.skip('simplify the meta schema', function() { beforeEach(function() { return $RefParser.dereference(_.cloneDeep(metaSchema)).then(function(dereferenced) { @@ -14,7 +14,7 @@ describe.skip('simplify the meta schema', function() { }) it('simplifies', function() { - var result = merger(_.cloneDeep(schema)) + const result = merger(_.cloneDeep(schema)) expect(result).to.eql(schema) }) }) diff --git a/test/specs/options.spec.js b/test/specs/options.spec.js index 2123ec0..21e55db 100644 --- a/test/specs/options.spec.js +++ b/test/specs/options.spec.js @@ -1,11 +1,11 @@ -var chai = require('chai') -var merger = require('../../src') +const chai = require('chai') +const merger = require('../../src') -var expect = chai.expect +const expect = chai.expect describe('options', function() { it('allows otherwise incompatible properties if option ignoreAdditionalProperties is true', function() { - var result = merger({ + const result = merger({ allOf: [{ properties: { foo: true @@ -29,7 +29,7 @@ describe('options', function() { additionalProperties: false }) - var result2 = merger({ + const result2 = merger({ allOf: [{ additionalProperties: true }, { @@ -41,7 +41,7 @@ describe('options', function() { }) it('ignoreAdditionalProperties is true, also allows merging of patternProperties', function() { - var result = merger({ + const result = merger({ allOf: [{ properties: { foo: true @@ -75,7 +75,7 @@ describe('options', function() { additionalProperties: false }) - var result2 = merger({ + const result2 = merger({ allOf: [{ additionalProperties: true }, { @@ -98,7 +98,7 @@ describe('options', function() { }) it('uses supplied resolver for unknown keyword', function() { - var result = merger({ + const result = merger({ foo: 3, allOf: [{ foo: 7 @@ -117,7 +117,7 @@ describe('options', function() { }) it('uses default merger if no resolver found', function() { - var result = merger({ + const result = merger({ foo: 3, allOf: [{ foo: 7 @@ -136,14 +136,14 @@ describe('options', function() { }) it('merges deep by default', function() { - var result = merger({ + const result = merger({ allOf: [{ properties: { - foo: {type: 'string'}, + foo: { type: 'string' }, bar: { allOf: [{ properties: { - baz: {type: 'string'} + baz: { type: 'string' } } }] } @@ -153,10 +153,10 @@ describe('options', function() { expect(result).to.eql({ properties: { - foo: {type: 'string'}, + foo: { type: 'string' }, bar: { properties: { - baz: {type: 'string'} + baz: { type: 'string' } } } } @@ -164,28 +164,28 @@ describe('options', function() { }) it('doesn\'t merge deep when deep is false', function() { - var result = merger({ + const result = merger({ allOf: [{ properties: { - foo: {type: 'string'}, + foo: { type: 'string' }, bar: { allOf: [{ properties: { - baz: {type: 'string'} + baz: { type: 'string' } } }] } } }] - }, {deep: false}) + }, { deep: false }) expect(result).to.eql({ properties: { - foo: {type: 'string'}, + foo: { type: 'string' }, bar: { allOf: [{ properties: { - baz: {type: 'string'} + baz: { type: 'string' } } }] } diff --git a/test/specs/properties.spec.js b/test/specs/properties.spec.js index f597510..32a393c 100644 --- a/test/specs/properties.spec.js +++ b/test/specs/properties.spec.js @@ -1,17 +1,17 @@ -var chai = require('chai') -var merger = require('../../src') -var sinon = require('sinon') -var _ = require('lodash') -var expect = chai.expect -var Ajv = require('ajv').default - -var ajv = new Ajv({ +const chai = require('chai') +const merger = require('../../src') +const sinon = require('sinon') +const _ = require('lodash') +const expect = chai.expect +const Ajv = require('ajv').default + +const ajv = new Ajv({ allowMatchingProperties: true }) describe('properties', function() { describe('when property name has same as a reserved word', function() { it('does not treat it as a reserved word', function() { - var stub = sinon.stub().returns({ + const stub = sinon.stub().returns({ properties: { properties: { type: 'string', @@ -48,7 +48,7 @@ describe('properties', function() { describe('additionalProperties', function() { it('allows no extra properties if additionalProperties is false', function() { - var result = merger({ + const result = merger({ allOf: [{ additionalProperties: true }, { @@ -62,7 +62,7 @@ describe('properties', function() { }) it('allows only intersecting properties', function() { - var result = merger({ + const result = merger({ allOf: [{ properties: { foo: true @@ -85,7 +85,7 @@ describe('properties', function() { }) it('allows intersecting patternproperties', function() { - var result = merger({ + const result = merger({ allOf: [{ properties: { foo: true, @@ -116,7 +116,7 @@ describe('properties', function() { }) it('disallows all except matching patternProperties if both false', function() { - var result = merger({ + const result = merger({ allOf: [{ properties: { foo: true, @@ -143,7 +143,7 @@ describe('properties', function() { }) it('disallows all except matching patternProperties if both false', function() { - var result = merger({ + const result = merger({ allOf: [{ properties: { foo: true, @@ -182,7 +182,7 @@ describe('properties', function() { }) it('disallows all except matching patternProperties if both false', function() { - var schema = { + const schema = { allOf: [{ type: 'object', properties: { @@ -205,8 +205,8 @@ describe('properties', function() { additionalProperties: false }] } - var origSchema = _.cloneDeep(schema) - var result = merger(schema) + const origSchema = _.cloneDeep(schema) + const result = merger(schema) expect(result).not.to.eql(origSchema) expect(result).to.eql({ @@ -243,7 +243,7 @@ describe('properties', function() { }) it('disallows all except matching patternProperties if both true', function() { - var schema = { + const schema = { allOf: [{ type: 'object', properties: { @@ -264,8 +264,8 @@ describe('properties', function() { } }] } - var origSchema = _.cloneDeep(schema) - var result = merger(schema) + const origSchema = _.cloneDeep(schema) + const result = merger(schema) expect(result).not.to.eql(origSchema) expect(result).to.eql({ @@ -307,7 +307,7 @@ describe('properties', function() { }) it('disallows all except matching patternProperties if one false', function() { - var schema = { + const schema = { allOf: [{ type: 'object', properties: { @@ -326,8 +326,8 @@ describe('properties', function() { additionalProperties: false }] } - var origSchema = _.cloneDeep(schema) - var result = merger(schema) + const origSchema = _.cloneDeep(schema) + const result = merger(schema) expect(result).not.to.eql(origSchema) expect(result).to.eql({ @@ -368,7 +368,7 @@ describe('properties', function() { }) it('disallows all if no patternProperties and if both false', function() { - var result = merger({ + const result = merger({ allOf: [{ properties: { foo: true, @@ -389,7 +389,7 @@ describe('properties', function() { }) it('applies additionalProperties to other schemas properties if they have any', function() { - var result = merger({ + const result = merger({ properties: { common: true, root: true @@ -449,7 +449,7 @@ describe('properties', function() { }) it('considers patternProperties before merging additionalProperties to other schemas properties if they have any', function() { - var result = merger({ + const result = merger({ properties: { common: true, root: true @@ -538,7 +538,7 @@ describe('properties', function() { }) it('combines additionalProperties when schemas', function() { - var result = merger({ + const result = merger({ additionalProperties: true, allOf: [{ additionalProperties: { @@ -570,7 +570,7 @@ describe('properties', function() { describe('patternProperties', function() { it('merges simliar schemas', function() { - var result = merger({ + const result = merger({ patternProperties: { '^\\$.+': { type: [ @@ -616,7 +616,7 @@ describe('properties', function() { describe('when patternProperties present', function() { it('merges patternproperties', function() { - var result = merger({ + const result = merger({ allOf: [{ patternProperties: { '.*': { @@ -645,17 +645,17 @@ describe('properties', function() { }) it('merges with properties if matching property name', function() { - var schema = { + const schema = { allOf: [{ type: 'object', properties: { - 'name': { + name: { type: 'string', minLength: 1 } }, patternProperties: { - '_long$': { + _long$: { type: 'string', minLength: 7 } @@ -663,7 +663,7 @@ describe('properties', function() { }, { type: 'object', properties: { - 'foo_long': { + foo_long: { type: 'string', minLength: 9 } @@ -677,25 +677,25 @@ describe('properties', function() { }] } - var origSchema = _.cloneDeep(schema) - var result = merger(schema) + const origSchema = _.cloneDeep(schema) + const result = merger(schema) expect(result).not.to.eql(origSchema) expect(result).to.eql({ type: 'object', properties: { - 'foo_long': { + foo_long: { type: 'string', minLength: 9 }, - 'name': { + name: { type: 'string', minLength: 1 } }, patternProperties: { - '_long$': { + _long$: { type: 'string', minLength: 7 }, @@ -727,7 +727,7 @@ describe('properties', function() { }) function validateInputOutput(schema, transformedSchema, obj) { - var validOriginal = ajv.validate(schema, obj) - var validNew = ajv.validate(transformedSchema, obj) + const validOriginal = ajv.validate(schema, obj) + const validNew = ajv.validate(transformedSchema, obj) expect(validOriginal).to.eql(validNew) }