@trayio/connector-utils
Advanced tools
Comparing version 0.3.3 to 0.3.4
@@ -22,3 +22,3 @@ const _ = require('lodash'); | ||
result, | ||
iteratee(key), | ||
iteratee(key, value), | ||
_.isObjectLike(value) ? deepMapKeys(value, iteratee) : value, | ||
@@ -25,0 +25,0 @@ ); |
/* eslint-disable no-console */ | ||
const _ = require('lodash'); | ||
require('deepdash/addOmitDeep')(_); | ||
const deepmerge = require('deepmerge'); | ||
const deepMapKeys = require('./deepMapKeys'); | ||
const logger = require('./internal/logger'); | ||
const MISSING_KEYS_MESSAGE = | ||
'There are missing schema keys that should be provided:'; | ||
const INPUT_SCHEMA_PROBLEMS_MESSAGE = | ||
'There are problems with the generated input schema:'; | ||
@@ -11,14 +14,14 @@ const flattenAndCompact = ({ array }) => _.flattenDeep(_.compact(array)); | ||
const logIssuesToConsole = ({ issues }) => { | ||
if (issues.some(error => error.missing === 'type')) { | ||
logger.log('error', MISSING_KEYS_MESSAGE); | ||
if (issues.some((error) => error.missing === 'type')) { | ||
logger.log('error', INPUT_SCHEMA_PROBLEMS_MESSAGE); | ||
} else { | ||
logger.log('warn', MISSING_KEYS_MESSAGE); | ||
logger.log('warn', INPUT_SCHEMA_PROBLEMS_MESSAGE); | ||
} | ||
logger.log( | ||
'table', | ||
issues.map(error => ({ | ||
issues.map((error) => ({ | ||
key: error.key, | ||
[error.missing]: 'missing', | ||
})), | ||
['key', 'type', 'description'], | ||
['key', 'type', 'description', 'full stop'], | ||
); | ||
@@ -41,3 +44,3 @@ }; | ||
return Array.isArray(col) | ||
? col.map(element => | ||
? col.map((element) => | ||
deepValidateSchema({ | ||
@@ -98,2 +101,4 @@ collection: element, | ||
incompleteSchemaElements.push({ key, missing: 'description' }); | ||
} else if (!_.get(element, 'description', '.').endsWith('.')) { | ||
incompleteSchemaElements.push({ key, missing: 'full stop' }); | ||
} | ||
@@ -104,2 +109,36 @@ } | ||
// target is the original array that you wish to merge into | ||
// source is the array that you wish to merge into the target array | ||
const combineByIndex = (target, source, options, overwrite = false) => { | ||
const concatenate = !overwrite; | ||
const destination = target.slice(); | ||
source.forEach((item, index) => { | ||
if (typeof destination[index] === 'undefined') { | ||
destination[index] = options.cloneUnlessOtherwiseSpecified( | ||
item, | ||
options, | ||
); | ||
} else if (options.isMergeableObject(item)) { | ||
destination[index] = deepmerge(target[index], item, options); | ||
} else if (concatenate && target.indexOf(item) === -1) { | ||
destination.push(item); | ||
} else if (overwrite) { | ||
destination[index] = item; | ||
} | ||
}); | ||
return destination; | ||
}; | ||
const arrayMergeFunctions = { | ||
combineByIndex: (target, source, options) => | ||
combineByIndex(target, source, options), | ||
overwriteByIndex: (target, source, options) => | ||
combineByIndex(target, source, options, true), | ||
overwrite: (target, source) => source, | ||
}; | ||
/** | ||
@@ -115,15 +154,45 @@ * Generates an operation input schema. | ||
* This will be used as the root of the object path when logging validation issues. | ||
* @param {String} arrayMergeType The type of merging algorithm to be used for arrays. | ||
* Possible algorithms are concatenate (default), combineByIndex, overwriteByIndex or overwrite. | ||
* concatenate - merge arrays by concatenating values (default) | ||
* combineByIndex - merge/combine arrays by index value (arrays of objects will merge by index if possible, other types will concatenate) | ||
* overwriteByIndex - merge/overwrite arrays by index value (arrays of objects will merge by index value if possible, other types will overwrite by index value) | ||
* overwrite - overwrite original array with specified array | ||
* @return {object} A copy of the requested schema elements. | ||
*/ | ||
const generateInputSchema = ({ schema, keys, operation = 'schema' }) => { | ||
const generateInputSchema = ({ | ||
schema = {}, | ||
keys = { | ||
key_name: { | ||
type: 'string', | ||
description: 'Description.', | ||
title: '', | ||
default: '', | ||
}, | ||
}, | ||
operation = 'schema', | ||
arrayMergeType = 'concatenate', | ||
}) => { | ||
// map the required input parameters to their individual schemas | ||
// and override with any additionally provided values | ||
const mappedSchema = _.map(keys, (value, key) => ({ | ||
[key]: { ...schema[key], ...value }, | ||
})); | ||
const mappedSchema = _.map(keys, (value, key) => { | ||
return { | ||
[key]: deepmerge(schema[key], value, { | ||
arrayMerge: arrayMergeFunctions[arrayMergeType], | ||
}), | ||
}; | ||
}); | ||
// deep map change keys to aliases | ||
const aliasedSchema = deepMapKeys(mappedSchema, (key, value) => { | ||
const { alias } = value; | ||
return alias || key; | ||
}); | ||
// deep remove original alias keys | ||
const transformedSchema = _.omitDeep(aliasedSchema, 'alias'); | ||
// find incomplete schema definitions | ||
const incompleteSchemaErrors = deepValidateSchema({ | ||
collection: mappedSchema, | ||
collection: transformedSchema, | ||
iteratee: checkForIncompleteSchemaElements, | ||
@@ -139,3 +208,3 @@ path: operation, | ||
// combine the individual schemas to a single operation schema | ||
const combinedSchema = mappedSchema.reduce( | ||
const combinedSchema = transformedSchema.reduce( | ||
(acc, curr) => ({ ...acc, ...curr }), | ||
@@ -142,0 +211,0 @@ {}, |
{ | ||
"name": "@trayio/connector-utils", | ||
"version": "0.3.3", | ||
"version": "0.3.4", | ||
"description": "Common utility functions used in connectors.", | ||
@@ -10,3 +10,4 @@ "main": "lib/index.js", | ||
"test": "jest tests", | ||
"test:watch": "jest tests --watch --notify" | ||
"test:watch": "jest tests --watch --notify", | ||
"update": "npx npm-check-updates -u && npm install" | ||
}, | ||
@@ -32,19 +33,21 @@ "repository": { | ||
"devDependencies": { | ||
"eslint": "^6.8.0", | ||
"eslint-config-airbnb-base": "^14.1.0", | ||
"eslint-config-prettier": "^6.10.0", | ||
"eslint-plugin-import": "^2.20.1", | ||
"eslint-plugin-jest": "^23.8.2", | ||
"eslint-plugin-jsx-a11y": "^6.2.3", | ||
"eslint-plugin-prettier": "^3.1.2", | ||
"eslint": "^7.11.0", | ||
"eslint-config-airbnb-base": "^14.2.0", | ||
"eslint-config-prettier": "^6.12.0", | ||
"eslint-plugin-import": "^2.22.1", | ||
"eslint-plugin-jest": "^24.1.0", | ||
"eslint-plugin-jsx-a11y": "^6.3.1", | ||
"eslint-plugin-prettier": "^3.1.4", | ||
"eslint-plugin-promise": "^4.2.1", | ||
"jest": "^25.1.0", | ||
"jest": "^26.5.3", | ||
"jest-json-schema": "^2.1.0", | ||
"jsdoc-to-markdown": "^5.0.3", | ||
"prettier": "^1.19.1" | ||
"jsdoc-to-markdown": "^6.0.1", | ||
"prettier": "^2.1.2" | ||
}, | ||
"dependencies": { | ||
"lodash": "~4.17.15", | ||
"deepdash": "^5.3.0", | ||
"deepmerge": "^4.2.2", | ||
"lodash": "~4.17.20", | ||
"mustache": "^4.0.1" | ||
} | ||
} |
@@ -107,2 +107,5 @@ # Connector-utils | ||
</dd> | ||
<dt><a href="#removeAuthKeys">removeAuthKeys(collection, additionalKeys)</a></dt> | ||
<dd><p>Removes top levels '#' keys and additional top level keys if supplied.</p> | ||
</dd> | ||
<dt><a href="#validatePaginationRange">validatePaginationRange(value, validation)</a></dt> | ||
@@ -401,28 +404,35 @@ <dd><p>Helper for validating user pagination input for a given range.</p> | ||
## generateInputSchema({ schema, keys, operation = 'schema' }) | ||
## generateInputSchema({ schema, keys, operation = 'schema', arrayMergeType = 'concatenate' }) | ||
Helper for generating an operation input schema. | ||
Will log to the console if: | ||
**Kind**: global function | ||
- a requested key does not exist, or | ||
- `type` or `description` keys are missing | ||
| Param | Type | Description | | ||
| -------------- | ------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | ||
| schema | <code>Object</code> | The full connector schema definition. | | ||
| keys | <code>Object</code> | The keys that you wish to extract from the schema with any override values. | | ||
| operation | <code>String</code> | The name of the connector operation that you are generating the schema for. This will be used as the root of the object path when logging validation issues. Optional. | | ||
| arrayMergeType | <code>String</code> | The type of merging algorithm to be used for arrays. Possible algorithms are `concatenate` (default), `combineByIndex`, `overwriteByIndex` or `overwrite`. Optional. | | ||
| returns | <code>Object</code> | A copy of the requested schema elements. | | ||
Will not log to the console if requested key does not exist, but is overridden with at least a type and description. | ||
**Array merge types:** | ||
- @param {Object} schema The full connector schema definition. | ||
- @param {Object} keys The keys that you wish to extract from the schema with any override values. | ||
- @param {String} operation The name of the connector operation that you are generating the schema for. | ||
- This will be used as the root of the object path when logging validation issues. | ||
- @return {object} A copy of the requested schema elements. | ||
\*/ | ||
- `concatenate` - merge arrays by concatenating values (default) | ||
- `combineByIndex` - merge/combine arrays by index value | ||
- arrays of objects will merge by index (if possible) | ||
- other types will concatenate (ignoring value duplication overlap) | ||
- `overwriteByIndex` - merge/overwrite arrays by index value | ||
- arrays of objects will merge by index (if possible) | ||
- other types will overwrite by index value | ||
- `overwrite` - overwrite original array with specified array | ||
**Kind**: global function | ||
Will log to the console if: | ||
| Param | Type | Description | | ||
| --------- | ------------------- | --------------------------------------------------------------------------- | | ||
| schema | <code>Object</code> | The full connector schema definition. | | ||
| keys | <code>Object</code> | The keys that you wish to extract from the schema with any override values. | | ||
| operation | <code>String</code> | The name of the connector operation that you are generating the schema for. | | ||
- a requested key does not exist, or | ||
- `type` or `description` keys are missing, or | ||
- a `description` does not end in a full stop | ||
Will not log to the console if requested key does not exist, but is overridden with at least a type and description with a full stop. | ||
For more information on how to use the schema generator, please see [schema-generation.md](./schema-generation.md). | ||
@@ -441,2 +451,3 @@ | ||
}, | ||
arrayMergeType: 'concatenate', | ||
}); | ||
@@ -459,2 +470,3 @@ /** | ||
default: 'value', | ||
alias: 'key_2', | ||
}, | ||
@@ -472,3 +484,3 @@ new_key: { | ||
* `full_schema_key_1` is extracted from the full schema without modification | ||
* `full_schema_key_2` is extracted from the full schema and extended/overridden with extra keys and values | ||
* `full_schema_key_2` is extracted from the full schema and extended/overridden with extra keys and values. The key name will be changed to `key_2' by use of an alias. | ||
* `new_key` is not in the full schema but it's full keys and values are supplied | ||
@@ -475,0 +487,0 @@ */ |
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
55280
841
519
4
1
+ Addeddeepdash@^5.3.0
+ Addeddeepmerge@^4.2.2
+ Addeddeepdash@5.3.9(transitive)
+ Addeddeepmerge@4.3.1(transitive)
+ Addedlodash-es@4.17.21(transitive)
Updatedlodash@~4.17.20