api-schema-builder
Advanced tools
Comparing version 1.0.1 to 1.0.2
{ | ||
"name": "api-schema-builder", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "build schema with validators for each endpoint", | ||
"main": "src/index.js", | ||
"files" :[ | ||
"src" | ||
], | ||
"nyc": { | ||
@@ -12,5 +15,2 @@ "report-dir": "./coverage", | ||
"branches": 90, | ||
"include": [ | ||
"src/*.js" | ||
], | ||
"reporter": [ | ||
@@ -25,4 +25,4 @@ "lcov", | ||
"scripts": { | ||
"test": "node_modules/mocha/bin/_mocha ./test/*/*/*-test.js ./test/*/*-test.js ./test/*-test.js --recursive", | ||
"test:coverage": "nyc node_modules/mocha/bin/_mocha --recursive ./test/*/*/*-test.js ./test/*/*-test.js ./test/*-test.js", | ||
"test": "node_modules/mocha/bin/_mocha ./test/*/*-test.js --recursive", | ||
"test:coverage": "nyc node_modules/mocha/bin/_mocha --recursive ./test/*/*-test.js ", | ||
"coveralls": "cat ./coverage/lcov.info | ./node_modules/.bin/coveralls", | ||
@@ -54,6 +54,5 @@ "lint": "./node_modules/.bin/eslint src" | ||
"devDependencies": { | ||
"body-parser": "^1.18.3", | ||
"ajv-keywords": "^3.4.0", | ||
"chai": "^4.2.0", | ||
"chai-sinon": "^2.8.1", | ||
"clear-require": "^2.0.0", | ||
"coveralls": "^3.0.2", | ||
@@ -67,5 +66,5 @@ "eslint": "^5.15.1", | ||
"eslint-plugin-standard": "^4.0.0", | ||
"express": "^4.16.4", | ||
"form-data": "^2.3.3", | ||
"lodash": "^4.17.11", | ||
"mocha": "^6.0.2", | ||
"multer": "^1.4.1", | ||
"nyc": "^13.3.0", | ||
@@ -72,0 +71,0 @@ "request": "^2.88.0", |
@@ -56,4 +56,8 @@ | ||
* `method`: the relevant method it written in the doc, for example `get`. | ||
* `parameters.validate`: ajv validator that check: paths, files, queries, headers. | ||
* `body.validate`: ajv validator that check: body only. | ||
* `parameters`: | ||
* `validate`: ajv validator that check: paths, files, queries and headers. | ||
* `errors`: in case of fail validation it return array of errors, otherwise return null | ||
* `body`: | ||
* `validate`: ajv validator that check: body only. | ||
* `errors`: in case of fail validation it return array of errors, otherwise return null | ||
@@ -60,0 +64,0 @@ |
@@ -27,2 +27,4 @@ 'use strict'; | ||
const dereferenced = swaggers[0]; | ||
const referenced = swaggers[1]; | ||
Object.keys(dereferenced.paths).forEach(function (currentPath) { | ||
@@ -38,3 +40,3 @@ let pathParameters = dereferenced.paths[currentPath].parameters || []; | ||
if (isOpenApi3){ | ||
schemas[parsedPath][currentMethod].body = oas3.buildBodyValidation(dereferenced, swaggers[1], currentPath, currentMethod, middlewareOptions); | ||
schemas[parsedPath][currentMethod].body = oas3.buildBodyValidation(dereferenced, referenced, currentPath, currentMethod, middlewareOptions); | ||
} else { | ||
@@ -49,6 +51,26 @@ let bodySchema = middlewareOptions.expectFormFieldsInBody | ||
const validatedBodySchema = oas2.getValidatedBodySchema(bodySchema); | ||
schemas[parsedPath][currentMethod].body = oas2.buildBodyValidation(validatedBodySchema, dereferenced.definitions, swaggers[1], currentPath, currentMethod, parsedPath, middlewareOptions); | ||
let bodySchemaReference = referenced.paths[currentPath][currentMethod].parameters.filter(function (parameter) { return parameter.in === 'body' })[0] || {}; | ||
let schemaReference = bodySchemaReference.schema; | ||
schemas[parsedPath][currentMethod].body = oas2.buildBodyValidation(validatedBodySchema, dereferenced.definitions, referenced, currentPath, currentMethod, parsedPath, middlewareOptions, schemaReference); | ||
} | ||
} | ||
// response validation | ||
schemas[parsedPath][currentMethod].responses = {}; | ||
let responses = dereferenced.paths[currentPath][currentMethod].responses || []; | ||
Object.keys(responses).forEach(statusCode => { | ||
if (statusCode !== 'default'){ | ||
let responseDereferenceSchema = responses[statusCode].schema; | ||
let responseDereferenceHeaders = responses[statusCode].headers; | ||
let headersValidator = responseDereferenceHeaders ? buildHeadersValidation(responseDereferenceHeaders, middlewareOptions) : undefined; | ||
let responseSchema = referenced.paths[currentPath][currentMethod].responses[statusCode].schema; | ||
let bodyValidator = responseSchema ? oas2.buildBodyValidation(responseDereferenceSchema, dereferenced.definitions, referenced, currentPath, currentMethod, parsedPath, middlewareOptions, responseSchema) : undefined; | ||
if (headersValidator || bodyValidator){ | ||
schemas[parsedPath][currentMethod].responses[statusCode] = new Validators.ResponseValidator({ body: bodyValidator, headers: headersValidator }); | ||
} | ||
} | ||
}); | ||
let localParameters = parameters.filter(function (parameter) { | ||
@@ -65,6 +87,3 @@ return parameter.in !== 'body'; | ||
return schemas; | ||
}) | ||
.catch(function (error) { | ||
return Promise.reject(error); | ||
}); | ||
}); | ||
} | ||
@@ -89,3 +108,3 @@ | ||
ajvUtils.addCustomKeyword(ajv, middlewareOptions.formats); | ||
ajvUtils.addCustomKeyword(ajv, middlewareOptions.formats, middlewareOptions.keywords); | ||
@@ -159,4 +178,39 @@ var ajvParametersSchema = { | ||
// split to diff parsers if needed | ||
function buildHeadersValidation(headers, middlewareOptions) { | ||
const defaultAjvOptions = { | ||
allErrors: true, | ||
coerceTypes: 'array' | ||
}; | ||
const options = Object.assign({}, defaultAjvOptions, middlewareOptions.ajvConfigParams); | ||
let ajv = new Ajv(options); | ||
ajvUtils.addCustomKeyword(ajv, middlewareOptions.formats); | ||
var ajvHeadersSchema = { | ||
title: 'HTTP headers', | ||
type: 'object', | ||
properties: {}, | ||
required: [], | ||
additionalProperties: true | ||
}; | ||
Object.keys(headers).forEach(key => { | ||
let headerObj = Object.assign({}, headers[key]); | ||
const headerName = key.toLowerCase(); | ||
const headerRequired = headerObj.required; | ||
if (headerRequired) ajvHeadersSchema.required.push(key); | ||
delete headerObj.name; | ||
delete headerObj.required; | ||
ajvHeadersSchema.properties[headerName] = headerObj; | ||
}, this); | ||
// todo - should i need it? | ||
// ajvHeadersSchema.content = createContentTypeHeaders(middlewareOptions.contentTypeValidation, contentTypes); | ||
return new Validators.SimpleValidator(ajv.compile(ajvHeadersSchema)); | ||
} | ||
module.exports = { | ||
buildSchema: buildSchema | ||
}; |
@@ -36,6 +36,5 @@ | ||
function buildBodyValidation(schema, swaggerDefinitions, originalSwagger, currentPath, currentMethod, parsedPath, middlewareOptions = {}) { | ||
function buildBodyValidation(schema, swaggerDefinitions, originalSwagger, currentPath, currentMethod, parsedPath, middlewareOptions = {}, schemaReference) { | ||
const defaultAjvOptions = { | ||
allErrors: true | ||
// unknownFormats: 'ignore' | ||
}; | ||
@@ -48,3 +47,3 @@ const options = Object.assign({}, defaultAjvOptions, middlewareOptions.ajvConfigBody); | ||
if (schema.discriminator) { | ||
return buildInheritance(schema.discriminator, swaggerDefinitions, originalSwagger, currentPath, currentMethod, parsedPath, ajv); | ||
return buildInheritance(schema.discriminator, swaggerDefinitions, originalSwagger, currentPath, currentMethod, parsedPath, ajv, schemaReference); | ||
} else { | ||
@@ -54,5 +53,3 @@ return new Validators.SimpleValidator(ajv.compile(schema)); | ||
} | ||
function buildInheritance(discriminator, dereferencedDefinitions, swagger, currentPath, currentMethod, parsedPath, ajv) { | ||
let bodySchema = swagger.paths[currentPath][currentMethod].parameters.filter(function (parameter) { return parameter.in === 'body' })[0]; | ||
function buildInheritance(discriminator, dereferencedDefinitions, swagger, currentPath, currentMethod, parsedPath, ajv, schemaReference = {}) { | ||
var inheritsObject = { | ||
@@ -66,3 +63,3 @@ inheritance: [] | ||
swagger.definitions[key].allOf.forEach(element => { | ||
if (element['$ref'] && element['$ref'] === bodySchema.schema['$ref']) { | ||
if (element['$ref'] && element['$ref'] === schemaReference['$ref']) { | ||
inheritsObject[key] = ajv.compile(dereferencedDefinitions[key]); | ||
@@ -69,0 +66,0 @@ inheritsObject.inheritance.push(key); |
@@ -6,3 +6,4 @@ 'use strict'; | ||
SimpleValidator = require('./SimpleValidator'), | ||
DiscriminatorValidator = require('./DiscriminatorValidator'); | ||
DiscriminatorValidator = require('./DiscriminatorValidator'), | ||
ResponseValidator = require('./ResponseValidator'); | ||
@@ -12,4 +13,5 @@ module.exports = { | ||
OneOfValidator: OneOfValidator, | ||
ResponseValidator: ResponseValidator, | ||
SimpleValidator: SimpleValidator, | ||
DiscriminatorValidator | ||
}; | ||
}; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
17
136
47459
19
653