Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Drop Node 6 support. #45

Merged
merged 1 commit into from
Apr 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ script: "npm run test:coverage"

jobs:
include:
- language: node_js
node_js: 6
script: "npm run test"
- language: node_js
node_js: node
script: "npm run test:coverage"
Expand Down
997 changes: 453 additions & 544 deletions package-lock.json

Large diffs are not rendered by default.

15 changes: 9 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
"test": "mocha",
"test:coverage": "nyc npm test",
"coveralls": "cat ./coverage/lcov.info |./node_modules/.bin/coveralls",
"lint": "./node_modules/.bin/eslint src"
"lint": "eslint src",
"lint:fix": "eslint src --fix"
},
"repository": {
"type": "git",
Expand Down Expand Up @@ -54,8 +55,7 @@
"decimal.js": "^10.2.0",
"js-yaml": "^3.13.1",
"json-schema-deref-sync": "^0.13.0",
"object.values": "^1.1.1",
"swagger-parser": "^6.0.5"
"swagger-parser": "^9.0.1"
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an important benefit of bumping Node version, up-to-date swagger-parser no longer brings in core-js as a transitive dependency, which should help solve PayU/openapi-validator-middleware#116

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kibertoad , if we're going to release a breaking change, i'd like to also handle this together #39

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I'll work on that, but probably they can be merged separately.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kobik It has been done: #47

},
"devDependencies": {
"ajv-keywords": "^3.4.1",
Expand All @@ -70,11 +70,14 @@
"eslint-plugin-promise": "^4.2.1",
"eslint-plugin-standard": "^4.0.1",
"lodash.get": "^4.4.2",
"mocha": "^6.2.2",
"nyc": "^15.0.0",
"uuid": "^3.3.3"
"mocha": "^6.2.3",
"nyc": "^15.0.1",
"uuid": "^3.4.0"
},
"publishConfig": {
"registry": "https://registry.npmjs.org/"
},
"engines": {
"node": ">=8"
}
}
8 changes: 4 additions & 4 deletions src/customKeywords/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ var Ajv = require('ajv');
const fileNameField = 'fieldname';

function missingElements(superSet, subset) {
let missingElements = [];
const missingElements = [];
subset.every(function (subsetElement) {
if (!superSet.includes(subsetElement)) {
missingElements.push(subsetElement);
Expand All @@ -17,7 +17,7 @@ module.exports = {
validate: function filesValidation(schema, data) {
filesValidation.errors = [];
const dataFileName = data.map((element) => { return element[fileNameField] });
let missingFiles = missingElements(dataFileName, schema.required);
const missingFiles = missingElements(dataFileName, schema.required);
if (missingFiles.length > 0) {
filesValidation.errors.push(new Ajv.ValidationError({
keyword: 'files',
Expand All @@ -28,8 +28,8 @@ module.exports = {
}

// Check that only the optional files exists
let allFiles = schema.required.concat(schema.optional);
let extraFiles = missingElements(allFiles, dataFileName);
const allFiles = schema.required.concat(schema.optional);
const extraFiles = missingElements(allFiles, dataFileName);
if (extraFiles.length > 0) {
filesValidation.errors.push(new Ajv.ValidationError({
keyword: 'files',
Expand Down
3 changes: 3 additions & 0 deletions src/data_structures/tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class Node {
this.value = value;
this.childrenAsKeyValue = {};
}

/**
* Add child to the node.
* @param node - The node which going to be the child.
Expand All @@ -18,6 +19,7 @@ class Node {
addChild(node, key){
this.childrenAsKeyValue[key] = node;
}

/**
* Override node data by other node by reference.
* @param node - The node which going to use to take his data.
Expand All @@ -28,6 +30,7 @@ class Node {
this.childrenAsKeyValue = node.childrenAsKeyValue;
}
};

/**
* Get node value.
* @return The value of the node.
Expand Down
22 changes: 11 additions & 11 deletions src/parsers/open-api2.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ module.exports = {
};

function buildPathParameters(parameters, pathParameters) {
let localParameters = parameters.filter(function (parameter) {
const localParameters = parameters.filter(function (parameter) {
return parameter.in !== 'body';
}).concat(pathParameters);
return localParameters;
Expand Down Expand Up @@ -46,15 +46,15 @@ function getValidatedBodySchema(bodySchema) {
}

function buildHeadersValidation(responses, contentTypes, statusCode, options) {
let headers = get(responses, `[${statusCode}].headers`);
const headers = get(responses, `[${statusCode}].headers`);
if (!headers && !contentTypes) return;

const defaultAjvOptions = {
allErrors: true,
coerceTypes: 'array'
};
const ajvOptions = Object.assign({}, defaultAjvOptions, options.ajvConfigParams);
let ajv = new Ajv(ajvOptions);
const ajv = new Ajv(ajvOptions);

ajvUtils.addCustomKeyword(ajv, options.formats, options.keywords);

Expand All @@ -67,7 +67,7 @@ function buildHeadersValidation(responses, contentTypes, statusCode, options) {

if (headers) {
Object.keys(headers).forEach(key => {
let headerObj = Object.assign({}, headers[key]);
const headerObj = Object.assign({}, headers[key]);
const headerName = key.toLowerCase();
delete headerObj.name;
ajvHeadersSchema.properties[headerName] = headerObj;
Expand All @@ -84,31 +84,31 @@ function buildAjvValidator(ajvConfigBody, formats, keywords){
allErrors: true
};
const ajvOptions = Object.assign({}, defaultAjvOptions, ajvConfigBody);
let ajv = new Ajv(ajvOptions);
const ajv = new Ajv(ajvOptions);

ajvUtils.addCustomKeyword(ajv, formats, keywords);
return ajv;
}

function buildResponseBodyValidation(responses, swaggerDefinitions, originalSwagger, currentPath, currentMethod, statusCode, options) {
let schema = get(responses, `[${statusCode}].schema`);
const schema = get(responses, `[${statusCode}].schema`);
if (!schema) return;

let ajv = buildAjvValidator(options.ajvConfigBody, options.formats, options.keywords);
const ajv = buildAjvValidator(options.ajvConfigBody, options.formats, options.keywords);

if (schema.discriminator) {
let referenceName = originalSwagger.paths[currentPath][currentMethod].responses[statusCode].schema['$ref'];
const referenceName = originalSwagger.paths[currentPath][currentMethod].responses[statusCode].schema.$ref;
return buildInheritance(schema.discriminator, swaggerDefinitions, originalSwagger, ajv, referenceName);
} else {
return new Validators.SimpleValidator(ajv.compile(schema));
}
}

function buildRequestBodyValidation(schema, swaggerDefinitions, originalSwagger, currentPath, currentMethod, options) {
let ajv = buildAjvValidator(options.ajvConfigBody, options.formats, options.keywords);
const ajv = buildAjvValidator(options.ajvConfigBody, options.formats, options.keywords);

if (schema.discriminator) {
let referenceName = originalSwagger.paths[currentPath][currentMethod].parameters.filter(function (parameter) { return parameter.in === 'body' })[0].schema['$ref'];
const referenceName = originalSwagger.paths[currentPath][currentMethod].parameters.filter(function (parameter) { return parameter.in === 'body' })[0].schema.$ref;
return buildInheritance(schema.discriminator, swaggerDefinitions, originalSwagger, ajv, referenceName);
} else {
return new Validators.SimpleValidator(ajv.compile(schema));
Expand All @@ -124,7 +124,7 @@ function buildInheritance(discriminator, dereferencedDefinitions, swagger, ajv,
Object.keys(swagger.definitions).forEach(key => {
if (swagger.definitions[key].allOf) {
swagger.definitions[key].allOf.forEach(element => {
if (element['$ref'] && element['$ref'] === referenceName) {
if (element.$ref && element.$ref === referenceName) {
inheritsObject[key] = ajv.compile(dereferencedDefinitions[key]);
inheritsObject.inheritance.push(key);
}
Expand Down
33 changes: 16 additions & 17 deletions src/parsers/open-api3.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,28 +86,27 @@ function handleBodyValidation(
validationType,
{ ajvConfigBody, formats, keywords }
) {

if (!dereferencedBodySchema) {
return
return;
}

if (dereferencedBodySchema.discriminator && !referencedBodySchema) {
return
return;
}

const defaultAjvOptions = {
allErrors: true
};

const ajvOptions = Object.assign({}, defaultAjvOptions, ajvConfigBody);
let ajv = new Ajv(ajvOptions);
const ajv = new Ajv(ajvOptions);

ajvUtils.addCustomKeyword(ajv, formats, keywords);

if (dereferencedBodySchema.discriminator) {
let referencedSchemas = referenced.components.schemas;
let dereferencedSchemas = dereferenced.components.schemas;
let referenceName = referencedBodySchema['$ref'];
const referencedSchemas = referenced.components.schemas;
const dereferencedSchemas = dereferenced.components.schemas;
const referenceName = referencedBodySchema.$ref;

return buildV3Inheritance(referencedSchemas, dereferencedSchemas, ajv, referenceName);
} else {
Expand All @@ -119,31 +118,31 @@ function handleBodyValidation(
}

function buildPathParameters(parameters, pathParameters) {
let allParameters = [].concat(parameters, pathParameters);
let localParameters = allParameters.map(handleSchema);
const allParameters = [].concat(parameters, pathParameters);
const localParameters = allParameters.map(handleSchema);
return localParameters;
}

function handleSchema(data) {
let clonedData = cloneDeep(data);
let schema = data.schema;
const clonedData = cloneDeep(data);
const schema = data.schema;
if (schema) {
delete clonedData['schema'];
delete clonedData.schema;
Object.assign(clonedData, schema);
}
return clonedData;
}

function buildHeadersValidation(responses, statusCode, { ajvConfigParams, formats, keywords, contentTypeValidation }) {
let headers = get(responses, `[${statusCode}].headers`);
const headers = get(responses, `[${statusCode}].headers`);
if (!headers) return;

const defaultAjvOptions = {
allErrors: true,
coerceTypes: 'array'
};
const ajvOptions = Object.assign({}, defaultAjvOptions, ajvConfigParams);
let ajv = new Ajv(ajvOptions);
const ajv = new Ajv(ajvOptions);

ajvUtils.addCustomKeyword(ajv, formats, keywords);

Expand All @@ -156,7 +155,7 @@ function buildHeadersValidation(responses, statusCode, { ajvConfigParams, format
};

Object.keys(headers).forEach(key => {
let headerObj = Object.assign({}, headers[key].schema);
const headerObj = Object.assign({}, headers[key].schema);
const headerName = key.toLowerCase();
delete headerObj.name;
delete headerObj.required;
Expand Down Expand Up @@ -214,8 +213,8 @@ function buildV3Inheritance(referencedSchemas, dereferencedSchemas, ajv, referen
}

const options = currentSchema.oneOf.map((refObject) => {
let option = findKey(currentSchema.discriminator.mapping, (value) => (value === refObject['$ref']));
const ref = getKeyFromRef(refObject['$ref']);
const option = findKey(currentSchema.discriminator.mapping, (value) => (value === refObject.$ref));
const ref = getKeyFromRef(refObject.$ref);
return { option: option || ref, ref };
});
discriminatorObject.allowedValues = options.map((option) => option.option);
Expand Down
4 changes: 2 additions & 2 deletions src/utils/schema-preprocessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ function makeOptionalAttributesNullable(bodySchema) {
return;
}

for (let schemaEntry of bodySchema) {
for (const schemaEntry of bodySchema) {
if (schemaEntry && schemaEntry.schema) {
_processSchemaEntry(schemaEntry.schema);
}
Expand All @@ -26,7 +26,7 @@ function _processSchemaEntry(schema) {

const properties = schema.properties;
for (const property in properties) {
if (properties.hasOwnProperty(property)) {
if (Object.prototype.hasOwnProperty.call(properties, property)) {
if (!schema.required || schema.required.indexOf(property) === -1) {
if (!Array.isArray(properties[property].type)) {
properties[property].type = [properties[property].type, 'null'];
Expand Down
5 changes: 0 additions & 5 deletions src/utils/schemaUtils.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const values = require('object.values');
const fs = require('fs');
const yaml = require('js-yaml');
const path = require('path');
Expand All @@ -8,10 +7,6 @@ const schemaLoaders = require('./schemaLoaders');

const { readOnly, writeOnly, validationTypes, allDataTypes } = require('./common');

if (!Object.values) {
values.shim();
}

const DEFAULT_REQUEST_CONTENT_TYPE = 'application/json';
const DEFAULT_RESPONSE_CONTENT_TYPE = 'application/json';

Expand Down
2 changes: 1 addition & 1 deletion src/validators/DiscriminatorValidator.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function findSchemaValidation(tree, data) {
}

function discriminator(schemas, data) {
let schema = findSchemaValidation.call(this, schemas, data);
const schema = findSchemaValidation.call(this, schemas, data);
let result = false;
if (schema) {
result = schema(data);
Expand Down
2 changes: 1 addition & 1 deletion src/validators/OneOfValidator.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class OneOfValidator extends Validator {
}
}
function oneOf(schemas, data) {
let schema = schemas[data[schemas.discriminator]];
const schema = schemas[data[schemas.discriminator]];
let result = false;
if (schema) {
result = schema(data);
Expand Down
6 changes: 3 additions & 3 deletions src/validators/ResponseValidator.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ class ResponseValidator extends Validator {
}
}
function responseValidator(response, data) {
let bodySchema = response.body;
let headersSchema = response.headers;
const bodySchema = response.body;
const headersSchema = response.headers;
let bodyValidationResult = true, bodyValidationErrors = [],
headersValidationResult = true, headersValidationErrors = [];
if (bodySchema) {
Expand All @@ -35,7 +35,7 @@ function responseValidator(response, data) {
headersValidationErrors = headersSchema.errors ? addErrorPrefix(headersSchema.errors, 'headers') : [];
}

let errors = bodyValidationErrors.concat(headersValidationErrors);
const errors = bodyValidationErrors.concat(headersValidationErrors);
this.errors = errors.length === 0 ? null : errors;

return bodyValidationResult && headersValidationResult;
Expand Down
2 changes: 1 addition & 1 deletion src/validators/SimpleValidator.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class SimpleValidator extends Validator {
}

function simple(ajvValidate, data) {
let result = ajvValidate(data);
const result = ajvValidate(data);
this.errors = ajvValidate.errors;

return result;
Expand Down
2 changes: 1 addition & 1 deletion src/validators/validator-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module.exports = {
};

function allowedValuesError(discriminator, allowedValues) {
let error = new Error('should be equal to one of the allowed values');
const error = new Error('should be equal to one of the allowed values');
error.dataPath = '.' + discriminator;
error.keyword = 'enum';
error.params = {
Expand Down