From c768357bdf9f6b6dd2aee99b310e2ae46f44602e Mon Sep 17 00:00:00 2001 From: Maciej <100693724+m-kusnierz@users.noreply.github.com> Date: Mon, 11 Dec 2023 09:52:53 +0100 Subject: [PATCH] Refactor/bump node (#19) * Bump node 16 * var --> const and let * Moved source files to lib folder * Update package.json Co-authored-by: Daniel Hillmann * Bump version 7.0.0 * Add CI workflow --------- Co-authored-by: Daniel Hillmann --- .github/workflows/ci.yaml | 46 ++ .travis.yml | 5 - README.md | 17 +- cache.js => lib/cache.js | 0 .../foreignKeyValidationFactory.js | 22 +- formats.js => lib/formats.js | 30 +- .../generateExampleJson.js | 48 +- .../getSchemaFromObject.js | 10 +- .../getSchemasDirectory.js | 12 +- index.js => lib/index.js | 52 +- .../parseExampleJson.js | 4 +- readRawSchemas.js => lib/readRawSchemas.js | 20 +- schemaFactory.js => lib/schemaFactory.js | 94 +-- package-lock.json | 758 +++++++++++++++++- package.json | 8 +- test/common.js | 4 +- .../dirCacheTest/test1/requireSchemagic.js | 2 +- .../dirCacheTest/test2/requireSchemagic.js | 2 +- .../test2/test2subdir/requireSchemagic.js | 2 +- test/integration/getSchemasDirectory.spec.js | 8 +- test/integration/readRawSchemas.spec.js | 6 +- test/integration/schemaFactory.spec.js | 86 +- test/integration/schemagic.spec.js | 66 +- test/unit/exampleJson.spec.js | 70 +- test/unit/foreignKeyValidationFactory.spec.js | 26 +- 25 files changed, 1096 insertions(+), 302 deletions(-) create mode 100644 .github/workflows/ci.yaml delete mode 100644 .travis.yml rename cache.js => lib/cache.js (100%) rename foreignKeyValidationFactory.js => lib/foreignKeyValidationFactory.js (74%) rename formats.js => lib/formats.js (86%) rename generateExampleJson.js => lib/generateExampleJson.js (83%) rename getSchemaFromObject.js => lib/getSchemaFromObject.js (78%) rename getSchemasDirectory.js => lib/getSchemasDirectory.js (58%) rename index.js => lib/index.js (71%) rename parseExampleJson.js => lib/parseExampleJson.js (75%) rename readRawSchemas.js => lib/readRawSchemas.js (59%) rename schemaFactory.js => lib/schemaFactory.js (56%) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml new file mode 100644 index 0000000..9c32bac --- /dev/null +++ b/.github/workflows/ci.yaml @@ -0,0 +1,46 @@ +name: Continuous integration (CI) + +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + +on: + pull_request: + +jobs: + test: + runs-on: ubuntu-latest + timeout-minutes: 10 + + permissions: + id-token: write + contents: read + checks: write + + steps: + - uses: actions/checkout@v3 + - id: determine-node-npm-version + run: | + # tr-d '<=>' to trim '>=8.19.x' to '8.19.x' to prevent major version bumps (like npm@9.3.0 installed by npm i npm@>=8.19.x) + NODE_VERSION=$(jq -r '.engines.node' ./package.json | tr -d '<=>') + echo "node-version=${NODE_VERSION/null/16}" >> $GITHUB_OUTPUT + NPM_VERSION=$(jq -r '.engines.npm' ./package.json | tr -d '<=>') + echo "npm-version=${NPM_VERSION/null/}" >> $GITHUB_OUTPUT + NPM_POSTINSTALL=$(jq -r '.scripts.postinstall' ./package.json) + echo "npm-postinstall=${NPM_POSTINSTALL/null/}" >> $GITHUB_OUTPUT + - name: Use Node.js + uses: actions/setup-node@v3 + with: + node-version: ${{ steps.determine-node-npm-version.outputs.node-version }} + cache: 'npm' + - name: Update NPM + if: steps.determine-node-npm-version.outputs.npm-version != '' + run: npm i npm@${{ steps.determine-node-npm-version.outputs.npm-version }} -g + - name: NPM CI + run: npm ci + - name: Lint + run: npm run lint --if-present + - name: ⚙️ Build + run: npm run build --if-present + - name: 🧪 NPM test + run: npm test diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 0934f79..0000000 --- a/.travis.yml +++ /dev/null @@ -1,5 +0,0 @@ -language: node_js -node_js: - - "6" - - "8" - - stable diff --git a/README.md b/README.md index 75978d4..b3079f3 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ Each schema will be loaded with `require`. This is an example of a schema in the ```js //JSON Schemas defined according to the standard json-schema http://json-schema.org/latest/json-schema-core.html -var regexpPatternUtil = require("./util/regexpPatternUtil"); +const regexpPatternUtil = require("./util/regexpPatternUtil"); module.exports = { "description":"Login", "required":true, @@ -36,7 +36,7 @@ module.exports = { When you require schemagic ``` -var schemagic = require("schemagic"); +const schemagic = require("schemagic"); ``` You will find the following things on `schemagic.login` @@ -188,9 +188,10 @@ like this: ```js function getForeignKeyChecker(collectionName, propertyName) { return function (documentIds, options, callback) { - var ids = [], formatErrors = [], anyFormatError = false; + const ids = [], formatErrors = []; + let anyFormatError = false; documentIds.forEach(function (invoiceId) { - var id; + let id; try { id = new options.mongo.ObjectID(invoiceId); formatErrors.push(true); @@ -203,9 +204,9 @@ function getForeignKeyChecker(collectionName, propertyName) { if(anyFormatError){ return callback(null, formatErrors); } - var query = {}; + const query = {}; query[propertyName] = {$in: ids}; - var fields = {}; + const fields = {}; fields[propertyName] = 1; return options.mongo(collectionName).find(query, fields, getArray); @@ -217,7 +218,7 @@ function getForeignKeyChecker(collectionName, propertyName) { } function checkResults(err, documentsInDb) { - var result; + let result; if (err) { return callback(err); } @@ -226,7 +227,7 @@ function getForeignKeyChecker(collectionName, propertyName) { result = documentsInDb.map(Boolean); //truthy values become TRUE return callback(null, result); //result array must have same order as array passed in documentIds param } - var idsInDb = documentsInDb.map(function (invoice) { + const idsInDb = documentsInDb.map(function (invoice) { return traverse(invoice).get(propertyName.split(".")).toString(); }); result = documentIds.map(function (id) { diff --git a/cache.js b/lib/cache.js similarity index 100% rename from cache.js rename to lib/cache.js diff --git a/foreignKeyValidationFactory.js b/lib/foreignKeyValidationFactory.js similarity index 74% rename from foreignKeyValidationFactory.js rename to lib/foreignKeyValidationFactory.js index ddd702a..b9486ad 100644 --- a/foreignKeyValidationFactory.js +++ b/lib/foreignKeyValidationFactory.js @@ -1,30 +1,30 @@ -var traverse = require('traverse'); -var async = require('async'); +const traverse = require('traverse'); +const async = require('async'); module.exports = function foreignKeyValidationFactory(foreignKeys) { return foreignKeyValidation; function foreignKeyValidation(document, options, callback) { - var valuesToCheck = getValuesToCheck(foreignKeys, document); + const valuesToCheck = getValuesToCheck(foreignKeys, document); if (Object.keys(valuesToCheck).length === 0) { return callback(null, []); } - var tasks = Object.keys(valuesToCheck).map(function (propertyName) { - var foreignKeyCheckFunction = foreignKeys[propertyName]; + const tasks = Object.keys(valuesToCheck).map(function (propertyName) { + const foreignKeyCheckFunction = foreignKeys[propertyName]; return getForeignKeyCheckTask(foreignKeyCheckFunction, valuesToCheck[propertyName], options); }); async.parallel(tasks, function (err, results) { if (err) { return callback(err); } - var errors = Array.prototype.concat.apply([], results); + const errors = Array.prototype.concat.apply([], results); return callback(null, errors); }); } }; function getValuesToCheck(foreignKeys, document) { - var memo = {}; + let memo = {}; Object.keys(foreignKeys).forEach(function (propertyName) { memo[propertyName] = []; }); @@ -47,7 +47,7 @@ function getValuesToCheck(foreignKeys, document) { function getForeignKeyCheckTask(foreignKeyCheckFunction, valuesToCheck, options) { return function foreignKeyCheck(callback) { - var values = valuesToCheck.map(function (o) { + const values = valuesToCheck.map(function (o) { return o.value; }); return foreignKeyCheckFunction(values, options, returnErrors); @@ -57,14 +57,14 @@ function getForeignKeyCheckTask(foreignKeyCheckFunction, valuesToCheck, options) return callback(err); } if (validArray.length !== valuesToCheck.length) { - var error = new Error('Foreign key check function did not return array of same length as values array passed to it'); + const error = new Error('Foreign key check function did not return array of same length as values array passed to it'); error.forignKeyFunction = foreignKeyCheckFunction.toString(); error.valuesPassed = values; error.valuesReturned = validArray; return callback(error); } - var errors = []; - for (var i = 0; i < validArray.length; i++) { + const errors = []; + for (let i = 0; i < validArray.length; i++) { if (!validArray[i]) { errors.push({ property: valuesToCheck[i].path.join('.'), diff --git a/formats.js b/lib/formats.js similarity index 86% rename from formats.js rename to lib/formats.js index 5f4c2b3..a01406a 100644 --- a/formats.js +++ b/lib/formats.js @@ -1,6 +1,6 @@ -var moment = require('moment'); -var format = require('util').format; -var validUrl = require('valid-url'); +const moment = require('moment'); +const format = require('util').format; +const validUrl = require('valid-url'); module.exports = { 'date-time': datetimeFormatCheck, @@ -37,10 +37,10 @@ function datetimeISOFormatCheck(value) { datetimeFormatCheck.doc = format('Must be a date and time in the format %s', dateTimeFormat); datetimeISOFormatCheck.doc = format('Must be a date and time in the iso format %s', isoDateTimeFormat); -var datePattern = /^\d{4}-\d{2}-\d{2}$/; -var dateFormat = 'YYYY-MM-DD'; +const datePattern = /^\d{4}-\d{2}-\d{2}$/; +const dateFormat = 'YYYY-MM-DD'; function dateFormatCheck(value) { - var dateTime = moment(value, dateFormat); + const dateTime = moment(value, dateFormat); if (!dateTime || !dateTime.isValid() || !datePattern.test(value)) { return false; } else if (dateTime.year() < minYear) { @@ -51,9 +51,9 @@ function dateFormatCheck(value) { dateFormatCheck.doc = format('Must be a date in the format %s', dateFormat); -var MAX_SAFE_INTEGER = Math.pow(2, 53) - 1; -var maxCurrency = MAX_SAFE_INTEGER / 100; -var minCurrency = -MAX_SAFE_INTEGER / 100; +const MAX_SAFE_INTEGER = Math.pow(2, 53) - 1; +const maxCurrency = MAX_SAFE_INTEGER / 100; +const minCurrency = -MAX_SAFE_INTEGER / 100; function currencyFormatCheck(value) { if (typeof value !== 'number') { return true; @@ -67,8 +67,8 @@ function currencyFormatCheck(value) { currencyFormatCheck.doc = format('Must be a number with a maximum of two decimals after the decimal point. ' + 'Must be between %s and %s', minCurrency, maxCurrency); -var maxRate = 100; -var minRate = 0; +const maxRate = 100; +const minRate = 0; function rateFormat(value) { if (typeof value !== 'number') { return true; @@ -82,8 +82,8 @@ function rateFormat(value) { rateFormat.doc = format('Must be a number with a maximum of two decimals after the decimal point. ' + 'Must be between %s and %s', minRate, maxRate); -var maxNegativeRate = 0; -var minNegativeRate = -100; +const maxNegativeRate = 0; +const minNegativeRate = -100; function rateNegativeFormat(value) { if (typeof value !== 'number') { return true; @@ -97,8 +97,8 @@ function rateNegativeFormat(value) { rateNegativeFormat.doc = format('Must be a number with a maximum of two decimals after the decimal point. ' + 'Must be between %s and %s', minNegativeRate, maxNegativeRate); -var maxCurrencyRate = 999999999; -var minCurrencyRate = 0.000001; +const maxCurrencyRate = 999999999; +const minCurrencyRate = 0.000001; function currencyRateFormat(value) { if (typeof value !== 'number') { return true; diff --git a/generateExampleJson.js b/lib/generateExampleJson.js similarity index 83% rename from generateExampleJson.js rename to lib/generateExampleJson.js index cde458b..942bc49 100644 --- a/generateExampleJson.js +++ b/lib/generateExampleJson.js @@ -1,11 +1,11 @@ //Generate example JSONs for schemas //Examples are strings (so they can contain comments) -var wordWrap = require('word-wrap'); -var formats = require('./formats'); -var isProperty = require('is-property'); +const wordWrap = require('word-wrap'); +const formats = require('./formats'); +const isProperty = require('is-property'); /*** BEGIN output class that encapsulated indentation ***/ -var output = { +const output = { addLine: function (line) { if (this.value.length > 0) { this.value += '\n'; //add linechange @@ -13,8 +13,8 @@ var output = { this.addIndentedText(line); }, indent: function () { - var array = []; - for (var i = 0; i < this.indentation; i++) { + const array = []; + for (let i = 0; i < this.indentation; i++) { array.push(' '); } this.value += array.join(''); @@ -29,7 +29,7 @@ var output = { }; function createOutput(indentation) { - var newOutput = Object.create(output); + const newOutput = Object.create(output); newOutput.indentation = indentation; newOutput.value = ''; return newOutput; @@ -37,12 +37,12 @@ function createOutput(indentation) { /*** END output class that encapsulated indentation ***/ function generateExampleJson(schema, minimal, noReadOnly, output) { - var type = schema.type; + let type = schema.type; if (Array.isArray(type)) { if (type.length === 0) { throw new Error('type is array with length=0: ' + JSON.stringify(type)); } - for (var i = 0; i < type.length; i++) { + for (let i = 0; i < type.length; i++) { if (type[i] !== 'null') { type = type[i]; break; //exit for @@ -86,9 +86,9 @@ function generateExampleJson(schema, minimal, noReadOnly, output) { } function getAllowsNull(schema) { - var type = schema.type; + const type = schema.type; if (Array.isArray(type)) { - for (var i = 0; i < type.length; i++) { + for (let i = 0; i < type.length; i++) { if (type[i] === 'null') { return true; } @@ -98,12 +98,12 @@ function getAllowsNull(schema) { } function addIntro(schema, output) { - var allowNull = getAllowsNull(schema); + const allowNull = getAllowsNull(schema); if (schema.description) { - var lines = wordWrap(schema.description, {width: 80, indent: '//'}).split('\n'); + const lines = wordWrap(schema.description, {width: 80, indent: '//'}).split('\n'); lines.forEach(output.addLine.bind(output)); } - var doc; + let doc; if (schema.readonly) { output.addLine('//Read only. You do not need this on POST, PUT and PATCH. You can leave it in from what you GET, it will simply be ignored.'); } else { @@ -130,8 +130,8 @@ function generateObjectJson(schema, minimal, noReadOnly, output) { output.addText('{'); output.indentation++; - var comma = ''; - for (var property in schema.properties) { + let comma = ''; + for (let property in schema.properties) { if (minimal && !schema.properties[property].required && !schema.properties[property].minimal) { continue; } @@ -141,7 +141,7 @@ function generateObjectJson(schema, minimal, noReadOnly, output) { if (schema.properties[property].hidden) { continue; } - var propertySchema = schema.properties[property]; + const propertySchema = schema.properties[property]; output.addText(comma); addIntro(propertySchema, output); output.addLine(encodeProperty(property) + ':'); @@ -171,7 +171,7 @@ function generateArrayJson(schema, minimal, noReadOnly, output) { output.addText('['); output.indentation++; - var propertySchema = schema.items; + const propertySchema = schema.items; addIntro(propertySchema, output); output.addLine(''); generateExampleJson(propertySchema, minimal, noReadOnly, output); @@ -182,18 +182,18 @@ function generateArrayJson(schema, minimal, noReadOnly, output) { } module.exports = function (schema, options) { - var asArray = options && options.asArray; - var minimal = options && options.minimal; - var noReadOnly = options && options.noReadOnly; - var output = createOutput(0); + const asArray = options && options.asArray; + const minimal = options && options.minimal; + const noReadOnly = options && options.noReadOnly; + const output = createOutput(0); if (asArray) { output.addLine('//Array'); output.addLine('['); output.indentation++; } if (schema.description) { - var lines = wordWrap(schema.description, {width: 80, indent: '//'}).split('\n'); - var lastLine = lines.pop(); + const lines = wordWrap(schema.description, {width: 80, indent: '//'}).split('\n'); + const lastLine = lines.pop(); lines.forEach(output.addLine.bind(output)); output.addLine(lastLine + '\n'); //we need linebreak because object (top level in schema) does not insert linebreak output.indent(); diff --git a/getSchemaFromObject.js b/lib/getSchemaFromObject.js similarity index 78% rename from getSchemaFromObject.js rename to lib/getSchemaFromObject.js index 47f2c6c..542e6c6 100644 --- a/getSchemaFromObject.js +++ b/lib/getSchemaFromObject.js @@ -1,5 +1,5 @@ function getSchemaFromObject(o, optionalName) { - var schema = {}; + const schema = {}; schema.description = 'TODO: Add description'; if(optionalName){ schema.description += ' for: ' + optionalName; @@ -13,10 +13,10 @@ function getSchemaFromObject(o, optionalName) { } else { schema.type = typeof o; if (typeof o === 'object') { - var properties = {}; - var addedProperties; - for(var propertyName in o) { - var p = getSchemaFromObject(o[propertyName], propertyName); + const properties = {}; + let addedProperties; + for(let propertyName in o) { + const p = getSchemaFromObject(o[propertyName], propertyName); properties[propertyName] = p; addedProperties = true; } diff --git a/getSchemasDirectory.js b/lib/getSchemasDirectory.js similarity index 58% rename from getSchemasDirectory.js rename to lib/getSchemasDirectory.js index 2d5ce5b..b7b61be 100644 --- a/getSchemasDirectory.js +++ b/lib/getSchemasDirectory.js @@ -1,11 +1,11 @@ -var path = require('path'); -var fs = require('fs'); -var existsSync = fs.existsSync || path.existsSync; -var DIR_NAME = 'schemas'; +const path = require('path'); +const fs = require('fs'); +const existsSync = fs.existsSync || path.existsSync; +const DIR_NAME = 'schemas'; module.exports = function getSchemasDirectory(startDir) { - var dir = startDir; - var lastDir; + let dir = startDir; + let lastDir; while (lastDir !== dir) { if (existsSync(path.join(dir, DIR_NAME))) { return path.join(dir, DIR_NAME); diff --git a/index.js b/lib/index.js similarity index 71% rename from index.js rename to lib/index.js index 86eb9ed..7e7d758 100644 --- a/index.js +++ b/lib/index.js @@ -1,19 +1,19 @@ delete require.cache[__filename]; //do not cache in require cache -var getSchemasDirectory = require('./getSchemasDirectory'); -var readRawSchemas = require('./readRawSchemas'); -var schemaFactory = require('./schemaFactory'); -var getSchemaFromObject = require('./getSchemaFromObject'); -var parseExampleJson = require('./parseExampleJson'); -var cache = require('./cache'); //use requires caching to have a singleton -var path = require('path'); -var clone = require('clone'); -var traverse = require('traverse'); -var localizedSchemaNameMatcher = new RegExp('\.([A-Z]{2})$'); +const getSchemasDirectory = require('./getSchemasDirectory'); +const readRawSchemas = require('./readRawSchemas'); +const schemaFactory = require('./schemaFactory'); +const getSchemaFromObject = require('./getSchemaFromObject'); +const parseExampleJson = require('./parseExampleJson'); +const cache = require('./cache'); //use requires caching to have a singleton +const path = require('path'); +const clone = require('clone'); +const traverse = require('traverse'); +const localizedSchemaNameMatcher = new RegExp('\.([A-Z]{2})$'); function schemagicInit() { - var startDir = path.dirname(module.parent.filename); - var schemasDirectory; + const startDir = path.dirname(module.parent.filename); + let schemasDirectory; if (cache.schemaDirectories[startDir]) { schemasDirectory = cache.schemaDirectories[startDir]; } else { @@ -23,8 +23,8 @@ function schemagicInit() { if (cache.schemagics[schemasDirectory]) { return cache.schemagics[schemasDirectory]; } - var rawSchemas = readRawSchemas(schemasDirectory); - var schemagic = {}; + const rawSchemas = readRawSchemas(schemasDirectory); + const schemagic = {}; Object.defineProperty( schemagic, 'getSchemaFromObject', @@ -45,22 +45,22 @@ function schemagicInit() { value: parseExampleJson } ); //schemagic.parseExampleJson is not enumerable - var foreignKeys = {}; + let foreignKeys = {}; if (rawSchemas.foreignKeys) { foreignKeys = rawSchemas.foreignKeys; delete rawSchemas.foreignKeys; } Object.keys(rawSchemas).sort().forEach(function (schemaName) { - var schemaForeignKeys = getSchemaForeignKeys(schemaName, foreignKeys); + const schemaForeignKeys = getSchemaForeignKeys(schemaName, foreignKeys); schemagic[schemaName] = schemaFactory(rawSchemas[schemaName], schemaForeignKeys); - var rawPatchSchema = clone(rawSchemas[schemaName]); - var t = traverse(rawPatchSchema); + const rawPatchSchema = clone(rawSchemas[schemaName]); + const t = traverse(rawPatchSchema); t.forEach(function (value) { //make sure null is allowed for all non-required properties if (this.key === 'type' && this.path.length >= 3 && this.path[this.path.length - 3] === 'properties') { - var required = t.get([].concat(this.parent.path, ['required'])); + const required = t.get([].concat(this.parent.path, ['required'])); if (required === false) { - var type = value; + let type = value; if (!Array.isArray(type)) { type = [type]; } @@ -78,7 +78,7 @@ function schemagicInit() { } }); schemagic[schemaName].patch = schemaFactory(rawPatchSchema, schemaForeignKeys); - var rawArraySchema = { + const rawArraySchema = { "required": true, "type": 'array', "items": clone(rawSchemas[schemaName]) @@ -86,9 +86,9 @@ function schemagicInit() { schemagic[schemaName].array = schemaFactory(rawArraySchema, schemaForeignKeys); if (localizedSchemaNameMatcher.test(schemaName)) { - var localizationEdition = schemaName.slice(-2).toUpperCase(); - var globalSchemaName = schemaName.slice(0, -3); - var globalSchema = schemagic[globalSchemaName]; + const localizationEdition = schemaName.slice(-2).toUpperCase(); + const globalSchemaName = schemaName.slice(0, -3); + const globalSchema = schemagic[globalSchemaName]; if (globalSchema) { schemagic[schemaName].globalSchema = globalSchema; globalSchema.localizedSchemas = globalSchema.localizedSchemas || {}; @@ -110,11 +110,11 @@ function schemagicInit() { function getSchemaForeignKeys(schemaName, foreignKeys) { return Object.keys(foreignKeys).reduce(function (memo, key) { - var keyParts = key.split('.'); + const keyParts = key.split('.'); if (keyParts.length === 1) { memo[key] = foreignKeys[key]; } else { - var keySchemaName = keyParts.slice(0, keyParts.length - 1).join('.'); + const keySchemaName = keyParts.slice(0, keyParts.length - 1).join('.'); if (keySchemaName === schemaName) { memo[keyParts.pop()] = foreignKeys[key]; } diff --git a/parseExampleJson.js b/lib/parseExampleJson.js similarity index 75% rename from parseExampleJson.js rename to lib/parseExampleJson.js index a6b0a40..45902d5 100644 --- a/parseExampleJson.js +++ b/lib/parseExampleJson.js @@ -2,9 +2,9 @@ module.exports = function parseExampleJson(example){ if(typeof example !== 'string'){ throw new Error('Schemagic: Example JSON must be a string'); } - var noComments = example.replace(/^(?: )*\/\/.*$/gm, '').replace(/\n/g,' '); + const noComments = example.replace(/^(?: )*\/\/.*$/gm, '').replace(/\n/g,' '); try { - var o = new Function('return ' + noComments)(); + const o = new Function('return ' + noComments)(); if(['string', 'object', 'number'].indexOf(typeof o) === -1){ throw new Error('(wrong type returned from parsing)'); } diff --git a/readRawSchemas.js b/lib/readRawSchemas.js similarity index 59% rename from readRawSchemas.js rename to lib/readRawSchemas.js index 669a6d1..6861cc3 100644 --- a/readRawSchemas.js +++ b/lib/readRawSchemas.js @@ -1,29 +1,29 @@ "use strict"; -var path = require('path'); -var fs = require('fs'); -var deepEqual = require('deep-equal'); +const path = require('path'); +const fs = require('fs'); +const deepEqual = require('deep-equal'); function readRawSchemas(schemasDirectory) { - var schemaFileNames = fs.readdirSync(schemasDirectory); - var schemas = {}; + const schemaFileNames = fs.readdirSync(schemasDirectory); + const schemas = {}; schemaFileNames.forEach(function (schemaFileName) { if (/^\.\w+/.test(schemaFileName)) { return; } - var schemaName = schemaFileName.replace('.js', ''); - var schemaFilePath = path.join(schemasDirectory, schemaFileName); + const schemaName = schemaFileName.replace('.js', ''); + const schemaFilePath = path.join(schemasDirectory, schemaFileName); if (!fs.statSync(schemaFilePath).isFile()) { return; //only require files (in schema dir root) } - var schema = require(schemaFilePath); - var keys = Object.keys(schema); + let schema = require(schemaFilePath); + const keys = Object.keys(schema); if (keys.length === 1 && keys[0] === 'default') { schema = schema.default; } schemas[schemaName] = schema; if (schemaName !== 'foreignKeys') { - var copy = JSON.parse(JSON.stringify(schema)); + const copy = JSON.parse(JSON.stringify(schema)); if (!deepEqual(schema, copy)) { throw new Error(`The schema ${schemaFilePath} is not JSON stringify-able`); } diff --git a/schemaFactory.js b/lib/schemaFactory.js similarity index 56% rename from schemaFactory.js rename to lib/schemaFactory.js index fc2c38a..87f5974 100644 --- a/schemaFactory.js +++ b/lib/schemaFactory.js @@ -1,35 +1,35 @@ -var util = require('util'); -var foreignKeyValidationFactory = require('./foreignKeyValidationFactory'); -var generateExampleJson = require('./generateExampleJson'); -var imjv = require('is-my-json-valid'); -var clone = require('clone'); -var traverse = require('traverse'); -var formats = require('./formats'); -var parseExampleJson = require('./parseExampleJson'); +const util = require('util'); +const foreignKeyValidationFactory = require('./foreignKeyValidationFactory'); +const generateExampleJson = require('./generateExampleJson'); +const imjv = require('is-my-json-valid'); +const clone = require('clone'); +const traverse = require('traverse'); +const formats = require('./formats'); +const parseExampleJson = require('./parseExampleJson'); -var dataRegExp = /^data\./; +const dataRegExp = /^data\./; function schemaFactory(rawSchema, foreignKeys) { - var normalizedJSON; - - var foreignKeyValidation = foreignKeyValidationFactory(foreignKeys); - var schema = schemaWithAdditionalPropertiesNotAllowedAsDefault(rawSchema); - var schemaWitNoReadonlyProperties = schemaWitNoReadonly(schema); - - var validateSchema = getValidateSchemaInstance(); - var filterSchema = getFilterSchemaInstance(); - var validateSchemaNoReadonly = getValidateSchemaNoReadonlyInstance(); - - var exampleJson = generateExampleJson(schema); - var example = parseExampleJson(exampleJson); //make sure it can be parser, this will throw if not - var exampleJsonArray = generateExampleJson(schema, { asArray: true }); - var exampleArray = parseExampleJson(exampleJsonArray); //make sure it can be parser, this will throw if not - var exampleMinimalJson = generateExampleJson(schema, { minimal: true }); - var exampleMinimal = exampleMinimalJson && parseExampleJson(exampleMinimalJson); //make sure it can be parser, this will throw if not - var exampleNoReadOnlyJson = generateExampleJson(schema, { noReadOnly: true }); - var exampleNoReadOnly = exampleMinimalJson && parseExampleJson(exampleMinimalJson); //make sure it can be parser, this will throw if not - - var schemaContainer = { + let normalizedJSON; + + const foreignKeyValidation = foreignKeyValidationFactory(foreignKeys); + const schema = schemaWithAdditionalPropertiesNotAllowedAsDefault(rawSchema); + const schemaWitNoReadonlyProperties = schemaWitNoReadonly(schema); + + const validateSchema = getValidateSchemaInstance(); + const filterSchema = getFilterSchemaInstance(); + const validateSchemaNoReadonly = getValidateSchemaNoReadonlyInstance(); + + const exampleJson = generateExampleJson(schema); + const example = parseExampleJson(exampleJson); //make sure it can be parser, this will throw if not + const exampleJsonArray = generateExampleJson(schema, { asArray: true }); + const exampleArray = parseExampleJson(exampleJsonArray); //make sure it can be parser, this will throw if not + const exampleMinimalJson = generateExampleJson(schema, { minimal: true }); + const exampleMinimal = exampleMinimalJson && parseExampleJson(exampleMinimalJson); //make sure it can be parser, this will throw if not + const exampleNoReadOnlyJson = generateExampleJson(schema, { noReadOnly: true }); + const exampleNoReadOnly = exampleMinimalJson && parseExampleJson(exampleMinimalJson); //make sure it can be parser, this will throw if not + + const schemaContainer = { localize, validate, schema, @@ -73,8 +73,8 @@ function schemaFactory(rawSchema, foreignKeys) { options = options || {}; validateSchema(document); - var errors = validateSchema.errors || []; - var doForeignKeyValidation = options && options.foreignKey === true; + let errors = validateSchema.errors || []; + const doForeignKeyValidation = options && options.foreignKey === true; if (errors.length === 0 && doForeignKeyValidation && foreignKeys) { if (!optionalCallback) { throw new Error('Foreign key validation requires a callback'); @@ -84,11 +84,11 @@ function schemaFactory(rawSchema, foreignKeys) { return optionalCallback(err); } errors = errors.concat(foreignKeyErrors); - var result = transformErrorsAndHandleReadOnly(errors); + const result = transformErrorsAndHandleReadOnly(errors); return optionalCallback(null, result); }); } - var result = transformErrorsAndHandleReadOnly(errors); + const result = transformErrorsAndHandleReadOnly(errors); if (optionalCallback) { return optionalCallback(null, result); } @@ -109,7 +109,7 @@ function schemaFactory(rawSchema, foreignKeys) { if (!err.property || err.property === 'data') { err.property = 'root'; } - var index = parseInt(err.property, 10); + const index = parseInt(err.property, 10); if (!isNaN(index)) { err.index = index; } @@ -120,7 +120,7 @@ function schemaFactory(rawSchema, foreignKeys) { } else if (options.filter === true) { filterSchema(document); } - var result = { valid: !errors.length, errors: errors }; + const result = { valid: !errors.length, errors: errors }; return result; } } @@ -153,39 +153,39 @@ function schemaWitNoReadonly(schema) { } function schemaWithAdditionalPropertiesNotAllowedAsDefault(schema) { - var hasHiddenProperties = false; - var s = clone(schema); - var t = traverse(s); + let hasHiddenProperties = false; + const schemaCopy = clone(schema); + const schemaTraverse = traverse(schemaCopy); - t.forEach(function (value) { + schemaTraverse.forEach(function (value) { if (this.key === 'hidden' && value === true) { hasHiddenProperties = true; } if (this.key === 'type' && value === 'object') { - var getProp = getParentObjectProp.bind(this); - var additionalProperties = getProp('additionalProperties'); - var oneOfAllOfOrAnyOff = ['oneOf', 'anyOf', 'allOf'].some(getProp); + const getProp = getParentObjectProp.bind(this); + const additionalProperties = getProp('additionalProperties'); + const oneOfAllOfOrAnyOff = ['oneOf', 'anyOf', 'allOf'].some(getProp); if ((!oneOfAllOfOrAnyOff) && (additionalProperties !== true)) { - t.set([].concat(this.parent.path, ['additionalProperties']), false); + schemaTraverse.set([].concat(this.parent.path, ['additionalProperties']), false); } } }); if (hasHiddenProperties) { - const schemaWithoutHiddenProperties = traverse(clone(s)) + const schemaWithoutHiddenProperties = traverse(clone(schemaCopy)) .forEach(function (value) { (this.key === 'hidden') && (value === true) && this.parent.remove(); }); - s.toJSON = function () { + schemaCopy.toJSON = function () { return schemaWithoutHiddenProperties; }; } - return s; + return schemaCopy; function getParentObjectProp(prop) { - return !this.parent || t.get(this.parent.path.concat(prop)); + return !this.parent || schemaTraverse.get(this.parent.path.concat(prop)); } } diff --git a/package-lock.json b/package-lock.json index 4875ffd..21e82fe 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,8 +1,759 @@ { "name": "schemagic", - "version": "6.2.0", - "lockfileVersion": 1, + "version": "7.0.0", + "lockfileVersion": 2, "requires": true, + "packages": { + "": { + "name": "schemagic", + "version": "7.0.0", + "license": "MIT", + "dependencies": { + "async": "^2.0.1", + "clone": "^1.0.2", + "deep-equal": "^1.0.1", + "is-my-json-valid": "2.20.0", + "is-property": "^1.0.2", + "moment": "^2.14.1", + "traverse": "^0.6.6", + "valid-url": "^1.0.9", + "word-wrap": "^1.1.0" + }, + "devDependencies": { + "chai": "^3.5.0", + "chai-subset": "^1.3.0", + "deep-extend": "^0.6.0", + "injectr": "^0.5.1", + "mocha": "5.2.0", + "mocha-jshint": "^2.3.1", + "mocha-teamcity-reporter": "2.4.0", + "sinon": "^1.17.5", + "sinon-chai": "^2.8.0" + }, + "engines": { + "node": ">=16", + "npm": ">=8" + } + }, + "node_modules/assertion-error": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/assertion-error/-/assertion-error-1.1.0.tgz", + "integrity": "sha512-jgsaNduz+ndvGyFt3uSuWqvy4lCnIJiovtouQN5JZHOKCS2QuhEdbcQHFhVksz2N2U9hXJo8odG7ETyWlEeuDw==", + "dev": true, + "engines": { + "node": "*" + } + }, + "node_modules/async": { + "version": "2.6.0", + "resolved": "https://registry.npmjs.org/async/-/async-2.6.0.tgz", + "integrity": "sha512-xAfGg1/NTLBBKlHFmnd7PlmUW9KhVQIUuSrYem9xzFUZy13ScvtyGGejaae9iAVRiRq9+Cx7DPFaAAhCpyxyPw==", + "dependencies": { + "lodash": "^4.14.0" + } + }, + "node_modules/balanced-match": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.0.tgz", + "integrity": "sha1-ibTRmasr7kneFk6gK4nORi1xt2c=", + "dev": true + }, + "node_modules/brace-expansion": { + "version": "1.1.11", + "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz", + "integrity": "sha512-iCuPHDFgrHX7H2vEI/5xpz07zSHB00TpugqhmYtVmMO6518mCuRMoOYFldEBl0g187ufozdaHgWKcYFb61qGiA==", + "dev": true, + "dependencies": { + "balanced-match": "^1.0.0", + "concat-map": "0.0.1" + } + }, + "node_modules/browser-stdout": { + "version": "1.3.1", + "resolved": "https://registry.npmjs.org/browser-stdout/-/browser-stdout-1.3.1.tgz", + "integrity": "sha512-qhAVI1+Av2X7qelOfAIYwXONood6XlZE/fXaBSmW/T5SzLAmCgzi+eiWE7fUvbHaeNBQH13UftjpXxsfLkMpgw==", + "dev": true + }, + "node_modules/chai": { + "version": "3.5.0", + "resolved": "https://registry.npmjs.org/chai/-/chai-3.5.0.tgz", + "integrity": "sha1-TQJjewZ/6Vi9v906QOxW/vc3Mkc=", + "dev": true, + "dependencies": { + "assertion-error": "^1.0.1", + "deep-eql": "^0.1.3", + "type-detect": "^1.0.0" + }, + "engines": { + "node": ">= 0.4.0" + } + }, + "node_modules/chai-subset": { + "version": "1.6.0", + "resolved": "https://registry.npmjs.org/chai-subset/-/chai-subset-1.6.0.tgz", + "integrity": "sha1-pdDKFOMpp5WW7XAFi2ZGvWmIz+k=", + "dev": true, + "engines": { + "node": ">=4" + } + }, + "node_modules/cli": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/cli/-/cli-1.0.1.tgz", + "integrity": "sha1-IoF1NPJL+klQw01TLUjsvGIbjBQ=", + "dev": true, + "dependencies": { + "exit": "0.1.2", + "glob": "^7.1.1" + }, + "engines": { + "node": ">=0.2.5" + } + }, + "node_modules/clone": { + "version": "1.0.4", + "resolved": "https://registry.npmjs.org/clone/-/clone-1.0.4.tgz", + "integrity": "sha1-2jCcwmPfFZlMaIypAheco8fNfH4=", + "engines": { + "node": ">=0.8" + } + }, + "node_modules/commander": { + "version": "2.15.1", + "resolved": "https://registry.npmjs.org/commander/-/commander-2.15.1.tgz", + "integrity": "sha512-VlfT9F3V0v+jr4yxPc5gg9s62/fIVWsd2Bk2iD435um1NlGMYdVCq+MjcXnhYq2icNOizHr1kK+5TI6H0Hy0ag==", + "dev": true + }, + "node_modules/concat-map": { + "version": "0.0.1", + "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz", + "integrity": "sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=", + "dev": true + }, + "node_modules/console-browserify": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/console-browserify/-/console-browserify-1.1.0.tgz", + "integrity": "sha1-8CQcRXMKn8YyOyBtvzjtx0HQuxA=", + "dev": true, + "dependencies": { + "date-now": "^0.1.4" + } + }, + "node_modules/core-util-is": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.2.tgz", + "integrity": "sha1-tf1UIgqivFq1eqtxQMlAdUUDwac=", + "dev": true + }, + "node_modules/date-now": { + "version": "0.1.4", + "resolved": "https://registry.npmjs.org/date-now/-/date-now-0.1.4.tgz", + "integrity": "sha1-6vQ5/U1ISK105cx9vvIAZyueNFs=", + "dev": true + }, + "node_modules/debug": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/debug/-/debug-3.1.0.tgz", + "integrity": "sha512-OX8XqP7/1a9cqkxYw2yXss15f26NKWBpDXQd0/uK/KPqdQhxbPa994hnzjcE2VqQpDslf55723cKPUOGSmMY3g==", + "dev": true, + "dependencies": { + "ms": "2.0.0" + } + }, + "node_modules/deep-eql": { + "version": "0.1.3", + "resolved": "https://registry.npmjs.org/deep-eql/-/deep-eql-0.1.3.tgz", + "integrity": "sha1-71WKyrjeJSBs1xOQbXTlaTDrafI=", + "dev": true, + "dependencies": { + "type-detect": "0.1.1" + }, + "engines": { + "node": "*" + } + }, + "node_modules/deep-eql/node_modules/type-detect": { + "version": "0.1.1", + "resolved": "https://registry.npmjs.org/type-detect/-/type-detect-0.1.1.tgz", + "integrity": "sha1-C6XsKohWQORw6k6FBZcZANrFiCI=", + "dev": true, + "engines": { + "node": "*" + } + }, + "node_modules/deep-equal": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/deep-equal/-/deep-equal-1.0.1.tgz", + "integrity": "sha1-9dJgKStmDghO/0zbyfCK0yR0SLU=" + }, + "node_modules/deep-extend": { + "version": "0.6.0", + "resolved": "https://registry.npmjs.org/deep-extend/-/deep-extend-0.6.0.tgz", + "integrity": "sha512-LOHxIOaPYdHlJRtCQfDIVZtfw/ufM8+rVj649RIHzcm/vGwQRXFt6OPqIFWsm2XEMrNIEtWR64sY1LEKD2vAOA==", + "dev": true, + "engines": { + "node": ">=4.0.0" + } + }, + "node_modules/diff": { + "version": "3.5.0", + "resolved": "https://registry.npmjs.org/diff/-/diff-3.5.0.tgz", + "integrity": "sha512-A46qtFgd+g7pDZinpnwiRJtxbC1hpgf0uzP3iG89scHk0AUC7A1TGxf5OiiOUv/JMZR8GOt8hL900hV0bOy5xA==", + "dev": true, + "engines": { + "node": ">=0.3.1" + } + }, + "node_modules/dom-serializer": { + "version": "0.1.0", + "resolved": "https://registry.npmjs.org/dom-serializer/-/dom-serializer-0.1.0.tgz", + "integrity": "sha1-BzxpdUbOB4DOI75KKOKT5AvDDII=", + "dev": true, + "dependencies": { + "domelementtype": "~1.1.1", + "entities": "~1.1.1" + } + }, + "node_modules/dom-serializer/node_modules/domelementtype": { + "version": "1.1.3", + "resolved": "https://registry.npmjs.org/domelementtype/-/domelementtype-1.1.3.tgz", + "integrity": "sha1-vSh3PiZCiBrsUVRJJCmcXNgiGFs=", + "dev": true + }, + "node_modules/dom-serializer/node_modules/entities": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/entities/-/entities-1.1.1.tgz", + "integrity": "sha1-blwtClYhtdra7O+AuQ7ftc13cvA=", + "dev": true + }, + "node_modules/domelementtype": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/domelementtype/-/domelementtype-1.3.0.tgz", + "integrity": "sha1-sXrtguirWeUt2cGbF1bg/BhyBMI=", + "deprecated": "update to domelementtype@1.3.1", + "dev": true + }, + "node_modules/domhandler": { + "version": "2.3.0", + "resolved": "https://registry.npmjs.org/domhandler/-/domhandler-2.3.0.tgz", + "integrity": "sha1-LeWaCCLVAn+r/28DLCsloqir5zg=", + "dev": true, + "dependencies": { + "domelementtype": "1" + } + }, + "node_modules/domutils": { + "version": "1.5.1", + "resolved": "https://registry.npmjs.org/domutils/-/domutils-1.5.1.tgz", + "integrity": "sha1-3NhIiib1Y9YQeeSMn3t+Mjc2gs8=", + "dev": true, + "dependencies": { + "dom-serializer": "0", + "domelementtype": "1" + } + }, + "node_modules/entities": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/entities/-/entities-1.0.0.tgz", + "integrity": "sha1-sph6o4ITR/zeZCsk/fyeT7cSvyY=", + "dev": true + }, + "node_modules/escape-string-regexp": { + "version": "1.0.5", + "resolved": "https://registry.npmjs.org/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz", + "integrity": "sha1-G2HAViGQqN/2rjuyzwIAyhMLhtQ=", + "dev": true, + "engines": { + "node": ">=0.8.0" + } + }, + "node_modules/exit": { + "version": "0.1.2", + "resolved": "https://registry.npmjs.org/exit/-/exit-0.1.2.tgz", + "integrity": "sha1-BjJjj42HfMghB9MKD/8aF8uhzQw=", + "dev": true, + "engines": { + "node": ">= 0.8.0" + } + }, + "node_modules/formatio": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/formatio/-/formatio-1.1.1.tgz", + "integrity": "sha1-XtPM1jZVEJc4NGXZlhmRAOhhYek=", + "deprecated": "This package is unmaintained. Use @sinonjs/formatio instead", + "dev": true, + "dependencies": { + "samsam": "~1.1" + } + }, + "node_modules/fs.realpath": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz", + "integrity": "sha1-FQStJSMVjKpA20onh8sBQRmU6k8=", + "dev": true + }, + "node_modules/generate-function": { + "version": "2.3.1", + "resolved": "https://registry.npmjs.org/generate-function/-/generate-function-2.3.1.tgz", + "integrity": "sha512-eeB5GfMNeevm/GRYq20ShmsaGcmI81kIX2K9XQx5miC8KdHaC6Jm0qQ8ZNeGOi7wYB8OsdxKs+Y2oVuTFuVwKQ==", + "dependencies": { + "is-property": "^1.0.2" + } + }, + "node_modules/generate-object-property": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/generate-object-property/-/generate-object-property-1.2.0.tgz", + "integrity": "sha1-nA4cQDCM6AT0eDYYuTf6iPmdUNA=", + "dependencies": { + "is-property": "^1.0.0" + } + }, + "node_modules/glob": { + "version": "7.1.1", + "resolved": "https://registry.npmjs.org/glob/-/glob-7.1.1.tgz", + "integrity": "sha1-gFIR3wT6rxxjo2ADBs31reULLsg=", + "dev": true, + "dependencies": { + "fs.realpath": "^1.0.0", + "inflight": "^1.0.4", + "inherits": "2", + "minimatch": "^3.0.2", + "once": "^1.3.0", + "path-is-absolute": "^1.0.0" + }, + "engines": { + "node": "*" + } + }, + "node_modules/growl": { + "version": "1.10.5", + "resolved": "https://registry.npmjs.org/growl/-/growl-1.10.5.tgz", + "integrity": "sha512-qBr4OuELkhPenW6goKVXiv47US3clb3/IbuWF9KNKEijAy9oeHxU9IgzjvJhHkUzhaj7rOUD7+YGWqUjLp5oSA==", + "dev": true, + "engines": { + "node": ">=4.x" + } + }, + "node_modules/has-flag": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-3.0.0.tgz", + "integrity": "sha1-tdRU3CGZriJWmfNGfloH87lVuv0=", + "dev": true, + "engines": { + "node": ">=4" + } + }, + "node_modules/he": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/he/-/he-1.1.1.tgz", + "integrity": "sha1-k0EP0hsAlzUVH4howvJx80J+I/0=", + "dev": true, + "bin": { + "he": "bin/he" + } + }, + "node_modules/htmlparser2": { + "version": "3.8.3", + "resolved": "https://registry.npmjs.org/htmlparser2/-/htmlparser2-3.8.3.tgz", + "integrity": "sha1-mWwosZFRaovoZQGn15dX5ccMEGg=", + "dev": true, + "dependencies": { + "domelementtype": "1", + "domhandler": "2.3", + "domutils": "1.5", + "entities": "1.0", + "readable-stream": "1.1" + } + }, + "node_modules/inflight": { + "version": "1.0.6", + "resolved": "https://registry.npmjs.org/inflight/-/inflight-1.0.6.tgz", + "integrity": "sha1-Sb1jMdfQLQwJvJEKEHW6gWW1bfk=", + "dev": true, + "dependencies": { + "once": "^1.3.0", + "wrappy": "1" + } + }, + "node_modules/inherits": { + "version": "2.0.3", + "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.3.tgz", + "integrity": "sha1-Yzwsg+PaQqUC9SRmAiSA9CCCYd4=", + "dev": true + }, + "node_modules/injectr": { + "version": "0.5.1", + "resolved": "https://registry.npmjs.org/injectr/-/injectr-0.5.1.tgz", + "integrity": "sha1-QdYa/jx3kxYGobEjI9kZs88BO4k=", + "dev": true, + "engines": { + "node": ">=0.6.x" + } + }, + "node_modules/is-my-ip-valid": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/is-my-ip-valid/-/is-my-ip-valid-1.0.0.tgz", + "integrity": "sha512-gmh/eWXROncUzRnIa1Ubrt5b8ep/MGSnfAUI3aRp+sqTCs1tv1Isl8d8F6JmkN3dXKc3ehZMrtiPN9eL03NuaQ==" + }, + "node_modules/is-my-json-valid": { + "version": "2.20.0", + "resolved": "https://registry.npmjs.org/is-my-json-valid/-/is-my-json-valid-2.20.0.tgz", + "integrity": "sha512-XTHBZSIIxNsIsZXg7XB5l8z/OBFosl1Wao4tXLpeC7eKU4Vm/kdop2azkPqULwnfGQjmeDIyey9g7afMMtdWAA==", + "dependencies": { + "generate-function": "^2.0.0", + "generate-object-property": "^1.1.0", + "is-my-ip-valid": "^1.0.0", + "jsonpointer": "^4.0.0", + "xtend": "^4.0.0" + } + }, + "node_modules/is-property": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/is-property/-/is-property-1.0.2.tgz", + "integrity": "sha1-V/4cTkhHTt1lsJkR8msc1Ald2oQ=" + }, + "node_modules/isarray": { + "version": "0.0.1", + "resolved": "https://registry.npmjs.org/isarray/-/isarray-0.0.1.tgz", + "integrity": "sha1-ihis/Kmo9Bd+Cav8YDiTmwXR7t8=", + "dev": true + }, + "node_modules/jshint": { + "version": "2.9.5", + "resolved": "https://registry.npmjs.org/jshint/-/jshint-2.9.5.tgz", + "integrity": "sha1-HnJSkVzmgbQIJ+4UJIxG006apiw=", + "dev": true, + "dependencies": { + "cli": "~1.0.0", + "console-browserify": "1.1.x", + "exit": "0.1.x", + "htmlparser2": "3.8.x", + "lodash": "3.7.x", + "minimatch": "~3.0.2", + "shelljs": "0.3.x", + "strip-json-comments": "1.0.x" + }, + "bin": { + "jshint": "bin/jshint" + } + }, + "node_modules/jshint/node_modules/lodash": { + "version": "3.7.0", + "resolved": "https://registry.npmjs.org/lodash/-/lodash-3.7.0.tgz", + "integrity": "sha1-Nni9irmVBXwHreg27S7wh9qBHUU=", + "dev": true + }, + "node_modules/jshint/node_modules/shelljs": { + "version": "0.3.0", + "resolved": "https://registry.npmjs.org/shelljs/-/shelljs-0.3.0.tgz", + "integrity": "sha1-NZbmMHp4FUT1kfN9phg2DzHbV7E=", + "dev": true, + "bin": { + "shjs": "bin/shjs" + }, + "engines": { + "node": ">=0.8.0" + } + }, + "node_modules/jsonpointer": { + "version": "4.0.1", + "resolved": "https://registry.npmjs.org/jsonpointer/-/jsonpointer-4.0.1.tgz", + "integrity": "sha1-T9kss04OnbPInIYi7PUfm5eMbLk=", + "engines": { + "node": ">=0.10.0" + } + }, + "node_modules/lodash": { + "version": "4.17.5", + "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.5.tgz", + "integrity": "sha512-svL3uiZf1RwhH+cWrfZn3A4+U58wbP0tGVTLQPbjplZxZ8ROD9VLuNgsRniTlLe7OlSqR79RUehXgpBW/s0IQw==" + }, + "node_modules/lolex": { + "version": "1.3.2", + "resolved": "https://registry.npmjs.org/lolex/-/lolex-1.3.2.tgz", + "integrity": "sha1-fD2mL/yzDw9agKJWbKJORdigHzE=", + "dev": true + }, + "node_modules/minimatch": { + "version": "3.0.4", + "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.0.4.tgz", + "integrity": "sha512-yJHVQEhyqPLUTgt9B83PXu6W3rx4MvvHvSUvToogpwoGDOUQ+yDrR0HRot+yOCdCO7u4hX3pWft6kWBBcqh0UA==", + "dev": true, + "dependencies": { + "brace-expansion": "^1.1.7" + }, + "engines": { + "node": "*" + } + }, + "node_modules/minimist": { + "version": "0.0.8", + "resolved": "https://registry.npmjs.org/minimist/-/minimist-0.0.8.tgz", + "integrity": "sha1-hX/Kv8M5fSYluCKCYuhqp6ARsF0=", + "dev": true + }, + "node_modules/mkdirp": { + "version": "0.5.1", + "resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-0.5.1.tgz", + "integrity": "sha1-MAV0OOrGz3+MR2fzhkjWaX11yQM=", + "deprecated": "Legacy versions of mkdirp are no longer supported. Please update to mkdirp 1.x. (Note that the API surface has changed to use Promises in 1.x.)", + "dev": true, + "dependencies": { + "minimist": "0.0.8" + }, + "bin": { + "mkdirp": "bin/cmd.js" + } + }, + "node_modules/mocha": { + "version": "5.2.0", + "resolved": "https://registry.npmjs.org/mocha/-/mocha-5.2.0.tgz", + "integrity": "sha512-2IUgKDhc3J7Uug+FxMXuqIyYzH7gJjXECKe/w43IGgQHTSj3InJi+yAA7T24L9bQMRKiUEHxEX37G5JpVUGLcQ==", + "dev": true, + "dependencies": { + "browser-stdout": "1.3.1", + "commander": "2.15.1", + "debug": "3.1.0", + "diff": "3.5.0", + "escape-string-regexp": "1.0.5", + "glob": "7.1.2", + "growl": "1.10.5", + "he": "1.1.1", + "minimatch": "3.0.4", + "mkdirp": "0.5.1", + "supports-color": "5.4.0" + }, + "bin": { + "_mocha": "bin/_mocha", + "mocha": "bin/mocha" + }, + "engines": { + "node": ">= 4.0.0" + } + }, + "node_modules/mocha-jshint": { + "version": "2.3.1", + "resolved": "https://registry.npmjs.org/mocha-jshint/-/mocha-jshint-2.3.1.tgz", + "integrity": "sha1-MD8n5TOThVnSDyakEj1by1ZFpUY=", + "dev": true, + "dependencies": { + "jshint": "^2.8.0", + "minimatch": "^3.0.0", + "shelljs": "^0.4.0", + "uniq": "^1.0.1" + } + }, + "node_modules/mocha-teamcity-reporter": { + "version": "2.4.0", + "resolved": "https://registry.npmjs.org/mocha-teamcity-reporter/-/mocha-teamcity-reporter-2.4.0.tgz", + "integrity": "sha512-DSLAuh/0DiYUBbKKaCgRxoBmzedVmSZjw+nzB+ql9mg9l5MstmcR2xzTlGqrqC+NXkInWTZ4CQ1FjBmosev3vA==", + "dev": true, + "dependencies": { + "mocha": ">=3.5.0" + }, + "engines": { + "node": ">=4" + } + }, + "node_modules/mocha/node_modules/glob": { + "version": "7.1.2", + "resolved": "https://registry.npmjs.org/glob/-/glob-7.1.2.tgz", + "integrity": "sha512-MJTUg1kjuLeQCJ+ccE4Vpa6kKVXkPYJ2mOCQyUuKLcLQsdrMCpBPUi8qVE6+YuaJkozeA9NusTAw3hLr8Xe5EQ==", + "dev": true, + "dependencies": { + "fs.realpath": "^1.0.0", + "inflight": "^1.0.4", + "inherits": "2", + "minimatch": "^3.0.4", + "once": "^1.3.0", + "path-is-absolute": "^1.0.0" + }, + "engines": { + "node": "*" + } + }, + "node_modules/moment": { + "version": "2.21.0", + "resolved": "https://registry.npmjs.org/moment/-/moment-2.21.0.tgz", + "integrity": "sha512-TCZ36BjURTeFTM/CwRcViQlfkMvL1/vFISuNLO5GkcVm1+QHfbSiNqZuWeMFjj1/3+uAjXswgRk30j1kkLYJBQ==", + "engines": { + "node": "*" + } + }, + "node_modules/ms": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz", + "integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g=", + "dev": true + }, + "node_modules/once": { + "version": "1.4.0", + "resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz", + "integrity": "sha1-WDsap3WWHUsROsF9nFC6753Xa9E=", + "dev": true, + "dependencies": { + "wrappy": "1" + } + }, + "node_modules/path-is-absolute": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/path-is-absolute/-/path-is-absolute-1.0.1.tgz", + "integrity": "sha1-F0uSaHNVNP+8es5r9TpanhtcX18=", + "dev": true, + "engines": { + "node": ">=0.10.0" + } + }, + "node_modules/readable-stream": { + "version": "1.1.14", + "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-1.1.14.tgz", + "integrity": "sha1-fPTFTvZI44EwhMY23SB54WbAgdk=", + "dev": true, + "dependencies": { + "core-util-is": "~1.0.0", + "inherits": "~2.0.1", + "isarray": "0.0.1", + "string_decoder": "~0.10.x" + } + }, + "node_modules/samsam": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/samsam/-/samsam-1.1.2.tgz", + "integrity": "sha1-vsEf3IOp/aBjQBIQ5AF2wwJNFWc=", + "deprecated": "This package has been deprecated in favour of @sinonjs/samsam", + "dev": true + }, + "node_modules/shelljs": { + "version": "0.4.0", + "resolved": "https://registry.npmjs.org/shelljs/-/shelljs-0.4.0.tgz", + "integrity": "sha1-GZ/p4t43nv0D00X/FAYlJeSzHsI=", + "dev": true, + "bin": { + "shjs": "bin/shjs" + }, + "engines": { + "node": ">=0.8.0" + } + }, + "node_modules/sinon": { + "version": "1.17.7", + "resolved": "https://registry.npmjs.org/sinon/-/sinon-1.17.7.tgz", + "integrity": "sha1-RUKk9JugxFwF6y6d2dID4rjv4L8=", + "deprecated": "16.1.1", + "dev": true, + "dependencies": { + "formatio": "1.1.1", + "lolex": "1.3.2", + "samsam": "1.1.2", + "util": ">=0.10.3 <1" + }, + "engines": { + "node": ">=0.1.103" + } + }, + "node_modules/sinon-chai": { + "version": "2.14.0", + "resolved": "https://registry.npmjs.org/sinon-chai/-/sinon-chai-2.14.0.tgz", + "integrity": "sha512-9stIF1utB0ywNHNT7RgiXbdmen8QDCRsrTjw+G9TgKt1Yexjiv8TOWZ6WHsTPz57Yky3DIswZvEqX8fpuHNDtQ==", + "dev": true, + "peerDependencies": { + "chai": ">=1.9.2 <5", + "sinon": "^1.4.0 || ^2.1.0 || ^3.0.0 || ^4.0.0" + } + }, + "node_modules/string_decoder": { + "version": "0.10.31", + "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-0.10.31.tgz", + "integrity": "sha1-YuIDvEF2bGwoyfyEMB2rHFMQ+pQ=", + "dev": true + }, + "node_modules/strip-json-comments": { + "version": "1.0.4", + "resolved": "https://registry.npmjs.org/strip-json-comments/-/strip-json-comments-1.0.4.tgz", + "integrity": "sha1-HhX7ysl9Pumb8tc7TGVrCCu6+5E=", + "dev": true, + "bin": { + "strip-json-comments": "cli.js" + }, + "engines": { + "node": ">=0.8.0" + } + }, + "node_modules/supports-color": { + "version": "5.4.0", + "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-5.4.0.tgz", + "integrity": "sha512-zjaXglF5nnWpsq470jSv6P9DwPvgLkuapYmfDm3JWOm0vkNTVF2tI4UrN2r6jH1qM/uc/WtxYY1hYoA2dOKj5w==", + "dev": true, + "dependencies": { + "has-flag": "^3.0.0" + }, + "engines": { + "node": ">=4" + } + }, + "node_modules/traverse": { + "version": "0.6.6", + "resolved": "https://registry.npmjs.org/traverse/-/traverse-0.6.6.tgz", + "integrity": "sha1-y99WD9e5r2MlAv7UD5GMFX6pcTc=" + }, + "node_modules/type-detect": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/type-detect/-/type-detect-1.0.0.tgz", + "integrity": "sha1-diIXzAbbJY7EiQihKY6LlRIejqI=", + "dev": true, + "engines": { + "node": "*" + } + }, + "node_modules/uniq": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/uniq/-/uniq-1.0.1.tgz", + "integrity": "sha1-sxxa6CVIRKOoKBVBzisEuGWnNP8=", + "dev": true + }, + "node_modules/util": { + "version": "0.10.3", + "resolved": "https://registry.npmjs.org/util/-/util-0.10.3.tgz", + "integrity": "sha1-evsa/lCAUkZInj23/g7TeTNqwPk=", + "dev": true, + "dependencies": { + "inherits": "2.0.1" + } + }, + "node_modules/util/node_modules/inherits": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.1.tgz", + "integrity": "sha1-sX0I0ya0Qj5Wjv9xn5GwscvfafE=", + "dev": true + }, + "node_modules/valid-url": { + "version": "1.0.9", + "resolved": "https://registry.npmjs.org/valid-url/-/valid-url-1.0.9.tgz", + "integrity": "sha1-HBRHm0DxOXp1eC8RXkCGRHQzogA=" + }, + "node_modules/word-wrap": { + "version": "1.2.3", + "resolved": "https://registry.npmjs.org/word-wrap/-/word-wrap-1.2.3.tgz", + "integrity": "sha512-Hz/mrNwitNRh/HUAtM/VT/5VH+ygD6DV7mYKZAtHOrbs8U7lvPS6xf7EJKMF0uW1KJCl0H701g3ZGus+muE5vQ==", + "engines": { + "node": ">=0.10.0" + } + }, + "node_modules/wrappy": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz", + "integrity": "sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8=", + "dev": true + }, + "node_modules/xtend": { + "version": "4.0.2", + "resolved": "https://registry.npmjs.org/xtend/-/xtend-4.0.2.tgz", + "integrity": "sha512-LKYU1iAXJXUgAXn9URjiu+MWhyUXHsvfp7mcuYm9dSUKK0/CjtrUwFAxD82/mCWbtLsGjFIad0wIsod4zrTAEQ==", + "engines": { + "node": ">=0.4" + } + } + }, "dependencies": { "assertion-error": { "version": "1.1.0", @@ -533,7 +1284,8 @@ "version": "2.14.0", "resolved": "https://registry.npmjs.org/sinon-chai/-/sinon-chai-2.14.0.tgz", "integrity": "sha512-9stIF1utB0ywNHNT7RgiXbdmen8QDCRsrTjw+G9TgKt1Yexjiv8TOWZ6WHsTPz57Yky3DIswZvEqX8fpuHNDtQ==", - "dev": true + "dev": true, + "requires": {} }, "string_decoder": { "version": "0.10.31", diff --git a/package.json b/package.json index d9717c4..d2953c5 100644 --- a/package.json +++ b/package.json @@ -2,16 +2,16 @@ "author": "debitoor", "name": "schemagic", "description": "JSON validation with schemas, and schema tools", - "version": "6.3.0", + "version": "7.0.0", "repository": { "type": "git", "url": "git+https://github.com/debitoor/schemagic.git" }, "private": false, - "main": "./source/schemagic.js", + "main": "./lib/index.js", "engines": { - "node": ">=0.10.28", - "npm": ">=2.4.1" + "node": ">=16", + "npm": ">=8" }, "dependencies": { "async": "^2.0.1", diff --git a/test/common.js b/test/common.js index 2146cda..6f2040b 100644 --- a/test/common.js +++ b/test/common.js @@ -1,7 +1,7 @@ global.sinon = require('sinon'); -var chai = require('chai'); +const chai = require('chai'); chai.config.includeStack; global.chai = chai; global.expect = chai.expect; -var chaiSubset = require('chai-subset'); +const chaiSubset = require('chai-subset'); chai.use(chaiSubset); diff --git a/test/integration/dirCacheTest/test1/requireSchemagic.js b/test/integration/dirCacheTest/test1/requireSchemagic.js index a3494ef..e1a51ce 100644 --- a/test/integration/dirCacheTest/test1/requireSchemagic.js +++ b/test/integration/dirCacheTest/test1/requireSchemagic.js @@ -1,2 +1,2 @@ delete require.cache[__filename]; //do not cache in require cache -module.exports = require('../../../../'); \ No newline at end of file +module.exports = require('../../../../lib'); \ No newline at end of file diff --git a/test/integration/dirCacheTest/test2/requireSchemagic.js b/test/integration/dirCacheTest/test2/requireSchemagic.js index a3494ef..e1a51ce 100644 --- a/test/integration/dirCacheTest/test2/requireSchemagic.js +++ b/test/integration/dirCacheTest/test2/requireSchemagic.js @@ -1,2 +1,2 @@ delete require.cache[__filename]; //do not cache in require cache -module.exports = require('../../../../'); \ No newline at end of file +module.exports = require('../../../../lib'); \ No newline at end of file diff --git a/test/integration/dirCacheTest/test2/test2subdir/requireSchemagic.js b/test/integration/dirCacheTest/test2/test2subdir/requireSchemagic.js index e9e41e6..703c37d 100644 --- a/test/integration/dirCacheTest/test2/test2subdir/requireSchemagic.js +++ b/test/integration/dirCacheTest/test2/test2subdir/requireSchemagic.js @@ -1,2 +1,2 @@ delete require.cache[__filename]; //do not cache in require cache -module.exports = require('../../../../../'); \ No newline at end of file +module.exports = require('../../../../../lib'); \ No newline at end of file diff --git a/test/integration/getSchemasDirectory.spec.js b/test/integration/getSchemasDirectory.spec.js index 2b85d4c..9ebf9f6 100644 --- a/test/integration/getSchemasDirectory.spec.js +++ b/test/integration/getSchemasDirectory.spec.js @@ -1,9 +1,9 @@ -var path = require('path'); -var getSchemasDirectory = require('../../getSchemasDirectory'); +const path = require('path'); +const getSchemasDirectory = require('../../lib/getSchemasDirectory'); describe('/source/util/getSchemasDirectory', function(){ describe('when it thinks it is in /test/integration/schemas/tmp/tmp', function(){ - var dir; + let dir; before(function(){ dir = getSchemasDirectory(path.join(__dirname, 'schemas/tmp/tmp')); }); @@ -14,7 +14,7 @@ describe('/source/util/getSchemasDirectory', function(){ }); describe('when it thinks it is in /test/integration/schemas/tmp/', function(){ - var dir; + let dir; before(function(){ dir = getSchemasDirectory(path.join(__dirname, 'schemas/tmp')); }); diff --git a/test/integration/readRawSchemas.spec.js b/test/integration/readRawSchemas.spec.js index bcbaf59..f73e79c 100644 --- a/test/integration/readRawSchemas.spec.js +++ b/test/integration/readRawSchemas.spec.js @@ -1,9 +1,9 @@ -var path = require('path'); -var readRawSchemas = require('../../readRawSchemas'); +const path = require('path'); +const readRawSchemas = require('../../lib/readRawSchemas'); describe('/source/util/redRawSchemas', function() { describe('will read schema', function() { - var schemas; + let schemas; before(function() { schemas = readRawSchemas(path.join(__dirname, 'schemas')); }); diff --git a/test/integration/schemaFactory.spec.js b/test/integration/schemaFactory.spec.js index e5350cd..b8a465b 100644 --- a/test/integration/schemaFactory.spec.js +++ b/test/integration/schemaFactory.spec.js @@ -1,9 +1,9 @@ -var schemaFactory = require('../../schemaFactory'); -var rawSchemas = require('../exampleSchemas'); -var exampleJson = require('../../generateExampleJson'); +const schemaFactory = require('../../lib/schemaFactory'); +const rawSchemas = require('../exampleSchemas'); +const exampleJson = require('../../lib/generateExampleJson'); describe('/source/util/schemaFactory run on simpleSchema, the returned object', function () { - var schema; + let schema; before(function () { schema = schemaFactory(rawSchemas.simpleSchema); }); @@ -62,7 +62,7 @@ describe('/source/util/schemaFactory run on simpleSchema, the returned object', describe('loading schema with property that requires string', function () { - var schema2; + let schema2; before(function () { schema2 = schemaFactory(rawSchemas.schemaWithStringEscapePropertyName); }); @@ -74,13 +74,13 @@ describe('/source/util/schemaFactory run on simpleSchema, the returned object', }); describe('validating valid document with removeReadOnlyFields option not set', function () { - var document = { + const document = { a: 1, b: 'x', c: 'y', d: 'z' }; - var result; + let result; before(function () { result = schema.validate(document); @@ -101,13 +101,13 @@ describe('/source/util/schemaFactory run on simpleSchema, the returned object', }); describe('validating valid document with removeReadOnlyFields option set to true', function () { - var document = { + const document = { a: 1, b: 'x', c: 'y', d: 'z' }; - var result; + let result; before(function () { result = schema.validate(document, { removeReadOnlyFields: true }); @@ -126,13 +126,13 @@ describe('/source/util/schemaFactory run on simpleSchema, the returned object', }); describe('validating valid document with removeReadOnlyFields option set to false', function () { - var document = { + const document = { a: 1, b: 'x', c: 'y', d: 'z' }; - var result; + let result; before(function () { result = schema.validate(document, { removeReadOnlyFields: false }); @@ -153,13 +153,13 @@ describe('/source/util/schemaFactory run on simpleSchema, the returned object', }); describe('validating an invalid document', function () { - var document = { + const document = { a: 'I SHOULD BE A NUMBER', b: 'x', c: 'y', d: 'z' }; - var result; + let result; before(function () { result = schema.validate(document); @@ -181,8 +181,8 @@ describe('/source/util/schemaFactory run on simpleSchema, the returned object', }); describe('validating an Array in root, where schema requires an object', function () { - var document = ['test']; - var result; + const document = ['test']; + let result; before(function () { result = schema.validate(document, { emptyStringsToNull: false, removeEmptyFields: false }); @@ -194,7 +194,7 @@ describe('/source/util/schemaFactory run on simpleSchema, the returned object', }); describe('validating document with date and date-time', function () { - var document, result; + let document, result; before(function () { schema = schemaFactory(rawSchemas.dateTimeSchema); @@ -299,9 +299,9 @@ describe('/source/util/schemaFactory run on simpleSchema, the returned object', }); describe('test valid document', function () { - var result; + let result; - var document = { + const document = { a: { b: 'yes' } }; @@ -315,9 +315,9 @@ describe('/source/util/schemaFactory run on simpleSchema, the returned object', }); describe('test 1st invalid document', function () { - var result; + let result; - var document = { + const document = { a: {} }; @@ -331,9 +331,9 @@ describe('/source/util/schemaFactory run on simpleSchema, the returned object', }); describe('test 2nd invalid document', function () { - var result; + let result; - var document = { + const document = { a: { b: 'yess', c: 'de' } }; @@ -349,15 +349,15 @@ describe('/source/util/schemaFactory run on simpleSchema, the returned object', }); describe('test enum fields', function () { - var schema; + let schema; before(function () { schema = schemaFactory(rawSchemas.schemaWithEnum); }); describe('default positive case', function () { - var result; + let result; - var document = { + const document = { a: 'foo' }; @@ -371,9 +371,9 @@ describe('/source/util/schemaFactory run on simpleSchema, the returned object', }); describe('not required field', function () { - var result; + let result; - var document = {}; + const document = {}; before(function () { result = schema.validate(document); @@ -386,15 +386,15 @@ describe('/source/util/schemaFactory run on simpleSchema, the returned object', }); describe('test currency format', function () { - var schema; + let schema; before(function () { schema = schemaFactory(rawSchemas.currencySchema); }); describe('default positive case', function () { - var result; + let result; - var document = { + const document = { a: 100.01 }; @@ -408,9 +408,9 @@ describe('/source/util/schemaFactory run on simpleSchema, the returned object', }); describe('invalid format', function () { - var result; + let result; - var document = { + const document = { a: 100.001 }; @@ -424,9 +424,9 @@ describe('/source/util/schemaFactory run on simpleSchema, the returned object', }); describe('not required field', function () { - var result; + let result; - var document = {}; + const document = {}; before(function () { result = schema.validate(document); @@ -438,9 +438,9 @@ describe('/source/util/schemaFactory run on simpleSchema, the returned object', }); describe('allow null', function () { - var result; + let result; - var document = { + const document = { a: null }; @@ -455,15 +455,15 @@ describe('/source/util/schemaFactory run on simpleSchema, the returned object', }); describe('validate with filter', function () { - var schema; + let schema; before(function () { schema = schemaFactory(rawSchemas.simpleSchemaWithNoAdditionalProperties); }); describe('simple schema validation without filter: true', function () { - var result; - var document = { a: 1, b: 'd', notInSchemaField: 'foo' }; + let result; + const document = { a: 1, b: 'd', notInSchemaField: 'foo' }; before(function () { result = schema.validate(document); @@ -490,8 +490,8 @@ describe('/source/util/schemaFactory run on simpleSchema, the returned object', }); describe('simple schema validation with filter: true', function () { - var result; - var document = { a: 1, b: 'd', thisIsNotInSchemaField: 'foo' }; + let result; + const document = { a: 1, b: 'd', thisIsNotInSchemaField: 'foo' }; before(function () { result = schema.validate(document, { filter: true }); @@ -507,8 +507,8 @@ describe('/source/util/schemaFactory run on simpleSchema, the returned object', }); describe('nested objects schema validation with filter: true', function () { - var result; - var document = { + let result; + const document = { a: 1, b: 'd', thisIsNotInSchemaField: 'foo', diff --git a/test/integration/schemagic.spec.js b/test/integration/schemagic.spec.js index 94e5010..f999c12 100644 --- a/test/integration/schemagic.spec.js +++ b/test/integration/schemagic.spec.js @@ -1,9 +1,9 @@ describe('/source/schemagic with valid example schemas', function () { - var schemagic; + let schemagic; describe('requiring schemagic here', function () { before(function () { - schemagic = require('../../'); + schemagic = require('../../lib'); }); it('returns a schemagic object', function () { @@ -68,7 +68,7 @@ describe('/source/schemagic with valid example schemas', function () { }); describe('validate against test2 schema that has hidden property', function () { - var result1, result2, result3; + let result1, result2, result3; before(function () { result1 = schemagic.test2.validate({ hiddenProperty: null }); @@ -112,8 +112,8 @@ describe('/source/schemagic with valid example schemas', function () { }); describe('validating against test2 schema that has foreign key value, foreignKey:false - no callback', function () { - var result; - var doc; + let result; + let doc; before(function () { doc = { testForeignKey: 3 //this is invalid in foreign key check @@ -130,8 +130,8 @@ describe('/source/schemagic with valid example schemas', function () { }); describe('array.validate: validating against test2 array schema that has foreign key value, foreignKey:false - no callback', function () { - var result; - var doc; + let result; + let doc; before(function () { doc = [{ testForeignKey: 3 //this is invalid in foreign key check @@ -148,8 +148,8 @@ describe('/source/schemagic with valid example schemas', function () { }); describe('array.validate: validating against test2 array schema that has foreign key value, foreignKey:false - no callback, two items', function () { - var result; - var doc; + let result; + let doc; before(function () { doc = [{ testForeignKey: 3 //this is invalid in foreign key check @@ -169,7 +169,7 @@ describe('/source/schemagic with valid example schemas', function () { }); describe('validating against test2 schema that has foreign key value, foreignKey:true - no callback', function () { - var doc, options, validate; + let doc, options, validate; before(function () { doc = { testForeignKey: 3 //this is invalid in foreign key check @@ -186,7 +186,7 @@ describe('/source/schemagic with valid example schemas', function () { }); describe('array.validate: validating against test2 array schema that has foreign key value, foreignKey:true - no callback', function () { - var doc, options, validate; + let doc, options, validate; before(function () { doc = [{ testForeignKey: 3 //this is invalid in foreign key check @@ -203,9 +203,9 @@ describe('/source/schemagic with valid example schemas', function () { }); describe('validating against test2 schema that has invalid foreign key value, foreignKey:true - callback', function () { - var result; - var doc; - var options; + let result; + let doc; + let options; before(function (done) { doc = { testForeignKey: 3 //this is invalid in foreign key check @@ -235,9 +235,9 @@ describe('/source/schemagic with valid example schemas', function () { }); describe('array.validate: validating against test2 schema that has invalid foreign key value, foreignKey:true - callback', function () { - var result; - var doc; - var options; + let result; + let doc; + let options; before(function (done) { doc = [{ testForeignKey: 3 //this is invalid in foreign key check @@ -269,13 +269,13 @@ describe('/source/schemagic with valid example schemas', function () { describe('validating against test1 schema that has valid foreign key value, ' + 'foreignKey defined with dot notation and belongs to the schema, foreignKey:true - callback', function () { - var result; + let result; before(function (done) { - var doc = { + const doc = { a: 1, testForeignKey2: 12 //this is valid in 'test.testForeignKey2' foreign key check }; - var options = { foreignKey: true }; + const options = { foreignKey: true }; return schemagic.test.validate(doc, options, saveResult); function saveResult(err, res) { @@ -297,12 +297,12 @@ describe('/source/schemagic with valid example schemas', function () { describe('validating against test2 schema that has invalid foreign key value, ' + 'foreignKey defined with dot notation and belongs to the schema, foreignKey:true - callback', function () { - var result; + let result; before(function (done) { - var doc = { + const doc = { testForeignKey2: 12 //this is invalid in 'test2.testForeignKey2' foreign key check }; - var options = { foreignKey: true }; + const options = { foreignKey: true }; return schemagic.test2.validate(doc, options, saveResult); function saveResult(err, res) { @@ -329,12 +329,12 @@ describe('/source/schemagic with valid example schemas', function () { describe('validating against test3.with.dot schema that has invalid foreign key value, ' + 'foreignKey defined with dot notation and belongs to the schema, ' + 'schema has dot in its name, foreignKey:true - callback', function () { - var result; + let result; before(function (done) { - var doc = { + const doc = { testForeignKey2: 12 //this is invalid in 'test3.with.dot.testForeignKey2' foreign key check }; - var options = { foreignKey: true }; + const options = { foreignKey: true }; return schemagic['test3.with.dot'].validate(doc, options, saveResult); function saveResult(err, res) { @@ -359,9 +359,9 @@ describe('/source/schemagic with valid example schemas', function () { }); describe('validating against test2 schema with foreign key check that fails, foreignKey:true - callback', function () { - var error; - var doc; - var options; + let error; + let doc; + let options; before(function (done) { doc = { testForeignKeyError: 3 //this foreign key checker fails @@ -405,7 +405,7 @@ describe('/source/schemagic with valid example schemas', function () { }); describe('having required schemagic in test1 dir', function () { - var schemagic1; + let schemagic1; before(function () { schemagic1 = require('./dirCacheTest/test1/requireSchemagic'); }); @@ -419,7 +419,7 @@ describe('/source/schemagic with valid example schemas', function () { }); describe('requiring it again ()', function () { - var shcmemagic2; + let shcmemagic2; before(function () { //requireSchemagic is not cached in require cache shcmemagic2 = require('./dirCacheTest/test1/requireSchemagic'); @@ -432,7 +432,7 @@ describe('/source/schemagic with valid example schemas', function () { }); }); describe('having required schemagic in test2 dir', function () { - var schemagic1; + let schemagic1; before(function () { schemagic1 = require('./dirCacheTest/test2/requireSchemagic'); }); @@ -446,7 +446,7 @@ describe('/source/schemagic with valid example schemas', function () { }); describe('requiring it from subdir', function () { - var shcmemagic2; + let shcmemagic2; before(function () { shcmemagic2 = require('./dirCacheTest/test2/test2subdir/requireSchemagic'); }); diff --git a/test/unit/exampleJson.spec.js b/test/unit/exampleJson.spec.js index d91dded..d7691d2 100644 --- a/test/unit/exampleJson.spec.js +++ b/test/unit/exampleJson.spec.js @@ -1,5 +1,5 @@ -var schemas = require('../exampleSchemas'); -var exampleJson = require('../../generateExampleJson'); +const schemas = require('../exampleSchemas'); +const exampleJson = require('../../lib/generateExampleJson'); //This is just a helper function I used for adding tests retrospectively //Given a string output from exampleJson, it will console log @@ -11,7 +11,7 @@ function formatJsonExampleForExpect(exampleJsonGenerated) { describe('/source/util/exampleJson', function () { it('will generate correct exampleJsonString for noReadOnlySchema', function () { - var exampleJsonGenerated = exampleJson(schemas.noReadOnlySchema); + const exampleJsonGenerated = exampleJson(schemas.noReadOnlySchema); expect(exampleJsonGenerated).to.equal( '//Simple object\n' + '{\n' + @@ -28,7 +28,7 @@ describe('/source/util/exampleJson', function () { }); it('will generate correct exampleMinimalJsonString for schemaOneMinimalProperty', function () { - var exampleJsonGenerated = exampleJson(schemas.schemaOneMinimalProperty, {minimal: true}); + const exampleJsonGenerated = exampleJson(schemas.schemaOneMinimalProperty, {minimal: true}); expect(exampleJsonGenerated).to.equal( `//Simple object { @@ -39,7 +39,7 @@ describe('/source/util/exampleJson', function () { }); it('will generate correct minimal exampleJsonString for noReadOnlySchema', function () { - var exampleJsonGenerated = exampleJson(schemas.noReadOnlySchema, {minimal: true}); + const exampleJsonGenerated = exampleJson(schemas.noReadOnlySchema, {minimal: true}); expect(exampleJsonGenerated).to.equal( `//Simple object { @@ -55,7 +55,7 @@ describe('/source/util/exampleJson', function () { it('will generate correct exampleJsonString for schemaWithVeryLongDescription, ' + 'should word wrap the long description', function () { - var exampleJsonGenerated = exampleJson(schemas.schemaWithVeryLongDescription); + const exampleJsonGenerated = exampleJson(schemas.schemaWithVeryLongDescription); expect(exampleJsonGenerated).to.equal(`//long long long long long long long long long long long long long long long long //long long long long long long long long long long long long long long long long //long long long long long long long long long long long long long long long long @@ -72,7 +72,7 @@ describe('/source/util/exampleJson', function () { }); it('will generate correct exampleJsonString for schemaWithFormats', function () { - var exampleJsonGenerated = exampleJson(schemas.schemaWithFormats); + const exampleJsonGenerated = exampleJson(schemas.schemaWithFormats); expect(exampleJsonGenerated).to.equal( `//Formats { @@ -107,7 +107,7 @@ describe('/source/util/exampleJson', function () { }); it('will generate correct minimal exampleJsonString for schemaWithFormats', function () { - var exampleJsonGenerated = exampleJson(schemas.schemaWithFormats, {minimal: true}); + const exampleJsonGenerated = exampleJson(schemas.schemaWithFormats, {minimal: true}); expect(exampleJsonGenerated).to.equal( `//Formats { @@ -116,7 +116,7 @@ describe('/source/util/exampleJson', function () { }); it('will generate correct exampleJsonString (Array) for noReadOnlySchema', function () { - var exampleJsonGenerated = exampleJson(schemas.noReadOnlySchema, {asArray: true}); + const exampleJsonGenerated = exampleJson(schemas.noReadOnlySchema, {asArray: true}); expect(exampleJsonGenerated).to.equal( `//Array [ @@ -137,7 +137,7 @@ describe('/source/util/exampleJson', function () { ); }); it('will generate correct minimal exampleJsonString (Array) for noReadOnlySchema', function () { - var exampleJsonGenerated = exampleJson(schemas.noReadOnlySchema, {asArray: true, minimal: true}); + const exampleJsonGenerated = exampleJson(schemas.noReadOnlySchema, {asArray: true, minimal: true}); expect(exampleJsonGenerated).to.equal( `//Array [ @@ -157,7 +157,7 @@ describe('/source/util/exampleJson', function () { }); it('will generate correct exampleJsonString for simpleSchema', function () { - var exampleJsonGenerated = exampleJson(schemas.simpleSchema); + const exampleJsonGenerated = exampleJson(schemas.simpleSchema); expect(exampleJsonGenerated).to.equal(`//Simple object { //Required @@ -172,7 +172,7 @@ describe('/source/util/exampleJson', function () { }); it('will generate correct minimal exampleJsonString for simpleSchema', function () { - var exampleJsonGenerated = exampleJson(schemas.simpleSchema, {minimal: true}); + const exampleJsonGenerated = exampleJson(schemas.simpleSchema, {minimal: true}); expect(exampleJsonGenerated).to.equal( `//Simple object { @@ -185,7 +185,7 @@ describe('/source/util/exampleJson', function () { }); it('will generate correct noReadOnly exampleJsonString for simpleSchema', function () { - var exampleJsonGenerated = exampleJson(schemas.simpleSchema, {noReadOnly: true}); + const exampleJsonGenerated = exampleJson(schemas.simpleSchema, {noReadOnly: true}); expect(exampleJsonGenerated).to.equal( `//Simple object { @@ -198,7 +198,7 @@ describe('/source/util/exampleJson', function () { }); it('will generate correct exampleJsonString (Array) for simpleSchema', function () { - var exampleJsonGenerated = exampleJson(schemas.simpleSchema, {asArray: true}); + const exampleJsonGenerated = exampleJson(schemas.simpleSchema, {asArray: true}); expect(exampleJsonGenerated).to.equal( `//Array [ @@ -220,7 +220,7 @@ describe('/source/util/exampleJson', function () { }); it('will generate correct minimal exampleJsonString (Array) for simpleSchema', function () { - var exampleJsonGenerated = exampleJson(schemas.simpleSchema, {asArray: true, minimal: true}); + const exampleJsonGenerated = exampleJson(schemas.simpleSchema, {asArray: true, minimal: true}); expect(exampleJsonGenerated).to.equal( `//Array [ @@ -239,7 +239,7 @@ describe('/source/util/exampleJson', function () { it('will generate correct exampleJsonString for nestedSchema', function () { - var exampleJsonGenerated = exampleJson(schemas.nestedSchema); + const exampleJsonGenerated = exampleJson(schemas.nestedSchema); expect(exampleJsonGenerated).to.equal(`//Simple object { //Required @@ -257,7 +257,7 @@ describe('/source/util/exampleJson', function () { }); it('will generate correct minimal exampleJsonString for nestedSchema', function () { - var exampleJsonGenerated = exampleJson(schemas.nestedSchema, {minimal: true}); + const exampleJsonGenerated = exampleJson(schemas.nestedSchema, {minimal: true}); expect(exampleJsonGenerated).to.equal( `//Simple object { @@ -273,7 +273,7 @@ describe('/source/util/exampleJson', function () { }); it('will generate correct exampleJsonString (Array) for nestedSchema', function () { - var exampleJsonGenerated = exampleJson(schemas.nestedSchema, {asArray: true}); + const exampleJsonGenerated = exampleJson(schemas.nestedSchema, {asArray: true}); expect(exampleJsonGenerated).to.equal( `//Array [ @@ -298,7 +298,7 @@ describe('/source/util/exampleJson', function () { }); it('will generate correct minimal exampleJsonString (Array) for nestedSchema', function () { - var exampleJsonGenerated = exampleJson(schemas.nestedSchema, {asArray: true, minimal: true}); + const exampleJsonGenerated = exampleJson(schemas.nestedSchema, {asArray: true, minimal: true}); expect(exampleJsonGenerated).to.equal( `//Array [ @@ -319,7 +319,7 @@ describe('/source/util/exampleJson', function () { }); it('will generate correct exampleJsonString for arrayReadOnlyItemsSchema', function () { - var exampleJsonGenerated = exampleJson(schemas.arrayReadOnlyItemsSchema); + const exampleJsonGenerated = exampleJson(schemas.arrayReadOnlyItemsSchema); expect(exampleJsonGenerated).to.equal( `//Simple object { @@ -344,7 +344,7 @@ describe('/source/util/exampleJson', function () { }); it('will generate correct minimal exampleJsonString for arrayReadOnlyItemsSchema', function () { - var exampleJsonGenerated = exampleJson(schemas.arrayReadOnlyItemsSchema, {minimal: true}); + const exampleJsonGenerated = exampleJson(schemas.arrayReadOnlyItemsSchema, {minimal: true}); expect(exampleJsonGenerated).to.equal( `//Simple object { @@ -357,7 +357,7 @@ describe('/source/util/exampleJson', function () { }); it('will generate correct exampleJsonString (Array) for arrayReadOnlyItemsSchema', function () { - var exampleJsonGenerated = exampleJson(schemas.arrayReadOnlyItemsSchema, {asArray: true}); + const exampleJsonGenerated = exampleJson(schemas.arrayReadOnlyItemsSchema, {asArray: true}); expect(exampleJsonGenerated).to.equal( `//Array [ @@ -387,7 +387,7 @@ describe('/source/util/exampleJson', function () { }); it('will generate correct minimal exampleJsonString (Array) for arrayReadOnlyItemsSchema', function () { - var exampleJsonGenerated = exampleJson(schemas.arrayReadOnlyItemsSchema, {asArray: true, minimal: true}); + const exampleJsonGenerated = exampleJson(schemas.arrayReadOnlyItemsSchema, {asArray: true, minimal: true}); expect(exampleJsonGenerated).to.equal( `//Array [ @@ -405,7 +405,7 @@ describe('/source/util/exampleJson', function () { }); it('will generate correct exampleJsonString for arraySchema', function () { - var exampleJsonGenerated = exampleJson(schemas.arraySchema); + const exampleJsonGenerated = exampleJson(schemas.arraySchema); expect(exampleJsonGenerated).to.equal( `//Simple object { @@ -430,7 +430,7 @@ describe('/source/util/exampleJson', function () { }); it('will generate correct minimal exampleJsonString for arraySchema', function () { - var exampleJsonGenerated = exampleJson(schemas.arraySchema, {minimal: true}); + const exampleJsonGenerated = exampleJson(schemas.arraySchema, {minimal: true}); expect(exampleJsonGenerated).to.equal( `//Simple object { @@ -451,7 +451,7 @@ describe('/source/util/exampleJson', function () { }); it('will generate correct exampleJsonString (Array) for arraySchema', function () { - var exampleJsonGenerated = exampleJson(schemas.arraySchema, {asArray: true}); + const exampleJsonGenerated = exampleJson(schemas.arraySchema, {asArray: true}); expect(exampleJsonGenerated).to.equal( `//Array [ @@ -481,7 +481,7 @@ describe('/source/util/exampleJson', function () { }); it('will generate correct minimal exampleJsonString (Array) for arraySchema', function () { - var exampleJsonGenerated = exampleJson(schemas.arraySchema, {asArray: true, minimal: true}); + const exampleJsonGenerated = exampleJson(schemas.arraySchema, {asArray: true, minimal: true}); expect(exampleJsonGenerated).to.equal( `//Array [ @@ -507,7 +507,7 @@ describe('/source/util/exampleJson', function () { }); it('will generate correct exampleJsonString for arrayNestedSchema', function () { - var exampleJsonGenerated = exampleJson(schemas.arrayNestedSchema); + const exampleJsonGenerated = exampleJson(schemas.arrayNestedSchema); expect(exampleJsonGenerated).to.equal( `//Simple object { @@ -531,7 +531,7 @@ describe('/source/util/exampleJson', function () { }); it('will generate correct minimal exampleJsonString for arrayNestedSchema', function () { - var exampleJsonGenerated = exampleJson(schemas.arrayNestedSchema, {minimal: true}); + const exampleJsonGenerated = exampleJson(schemas.arrayNestedSchema, {minimal: true}); expect(exampleJsonGenerated).to.equal( `//Simple object { @@ -553,7 +553,7 @@ describe('/source/util/exampleJson', function () { }); it('will generate correct exampleJsonString (Array) for arrayNestedSchema', function () { - var exampleJsonGenerated = exampleJson(schemas.arrayNestedSchema, {asArray: true}); + const exampleJsonGenerated = exampleJson(schemas.arrayNestedSchema, {asArray: true}); expect(exampleJsonGenerated).to.equal( `//Array [ @@ -582,7 +582,7 @@ describe('/source/util/exampleJson', function () { }); it('will generate correct minimal exampleJsonString (Array) for arrayNestedSchema', function () { - var exampleJsonGenerated = exampleJson(schemas.arrayNestedSchema, {asArray: true, minimal: true}); + const exampleJsonGenerated = exampleJson(schemas.arrayNestedSchema, {asArray: true, minimal: true}); expect(exampleJsonGenerated).to.equal( `//Array [ @@ -609,7 +609,7 @@ describe('/source/util/exampleJson', function () { }); it('will generate correct exampleJsonString for arrayAtRootSchema', function () { - var exampleJsonGenerated = exampleJson(schemas.arrayAtRootSchema); + const exampleJsonGenerated = exampleJson(schemas.arrayAtRootSchema); expect(exampleJsonGenerated).to.equal( `//Schema that contains an array at root level [ @@ -627,7 +627,7 @@ describe('/source/util/exampleJson', function () { }); it('will generate correct minimal exampleJsonString for arrayAtRootSchema', function () { - var exampleJsonGenerated = exampleJson(schemas.arrayAtRootSchema, {minimal: true}); + const exampleJsonGenerated = exampleJson(schemas.arrayAtRootSchema, {minimal: true}); expect(exampleJsonGenerated).to.equal( `//Schema that contains an array at root level [ @@ -642,7 +642,7 @@ describe('/source/util/exampleJson', function () { }); it('will generate correct exampleJsonString (Array) for arrayAtRootSchema', function () { - var exampleJsonGenerated = exampleJson(schemas.arrayAtRootSchema, {asArray: true}); + const exampleJsonGenerated = exampleJson(schemas.arrayAtRootSchema, {asArray: true}); expect(exampleJsonGenerated).to.equal( `//Array [ @@ -665,7 +665,7 @@ describe('/source/util/exampleJson', function () { }); it('will generate correct minimal exampleJsonString (Array) for arrayAtRootSchema', function () { - var exampleJsonGenerated = exampleJson(schemas.arrayAtRootSchema, {asArray: true, minimal: true}); + const exampleJsonGenerated = exampleJson(schemas.arrayAtRootSchema, {asArray: true, minimal: true}); expect(exampleJsonGenerated).to.equal( `//Array [ diff --git a/test/unit/foreignKeyValidationFactory.spec.js b/test/unit/foreignKeyValidationFactory.spec.js index 15098f0..2c43f4a 100644 --- a/test/unit/foreignKeyValidationFactory.spec.js +++ b/test/unit/foreignKeyValidationFactory.spec.js @@ -1,9 +1,9 @@ -var foreignKeyValidationFactory = require('../../foreignKeyValidationFactory'); +const foreignKeyValidationFactory = require('../../lib/foreignKeyValidationFactory'); describe('creating foreignKeyValidation with mock foreign keys definition', function () { - var foreignKeyValidation; - var foreignKeys; + let foreignKeyValidation; + let foreignKeys; beforeEach(function () { foreignKeys = { @@ -21,8 +21,8 @@ describe('creating foreignKeyValidation with mock foreign keys definition', func describe('validating foreign keys, with document that has no properties that should be checked', function () { - var result; - var doc, options; + let result; + let doc, options; beforeEach(function (done) { foreignKeys.test = sinon.spy(foreignKeys.test); @@ -58,8 +58,8 @@ describe('creating foreignKeyValidation with mock foreign keys definition', func describe('validating foreign keys, with document that has 4 properties that should be checked, all values are valid', function () { - var result; - var doc, options; + let result; + let doc, options; beforeEach(function (done) { foreignKeys.test = sinon.spy(foreignKeys.test); @@ -96,8 +96,8 @@ describe('creating foreignKeyValidation with mock foreign keys definition', func describe('validating foreign keys, with document that has 4 properties that should be checked, 2 values are invalid', function () { - var result; - var doc, options; + let result; + let doc, options; beforeEach(function (done) { foreignKeys.test = sinon.spy(foreignKeys.test); @@ -145,8 +145,8 @@ describe('creating foreignKeyValidation with mock foreign keys definition', func describe('validating foreign keys, with document that has a property where the foreign key check fails', function () { - var error; - var doc, options; + let error; + let doc, options; beforeEach(function (done) { foreignKeys.test = sinon.spy(foreignKeys.test); @@ -176,8 +176,8 @@ describe('creating foreignKeyValidation with mock foreign keys definition', func describe('validating foreign keys, with document that has three properties where the foreign key check fails', function () { - var error; - var doc, options; + let error; + let doc, options; beforeEach(function (done) { foreignKeys.test = sinon.spy(foreignKeys.test);