api-schema-builder
Advanced tools
Comparing version 1.0.0 to 1.0.1
{ | ||
"name": "api-schema-builder", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "build schema with validators for each endpoint", | ||
@@ -5,0 +5,0 @@ "main": "src/index.js", |
# api-schema-builder | ||
[![Build Status](https://travis-ci.org/Zooz/api-schema-builder.svg?branch=master)](https://travis-ci.org/Zooz/api-schema-builder) | ||
[![Coverage Status](https://coveralls.io/repos/github/Zooz/api-schema-builder/badge.svg?branch=master)](https://coveralls.io/github/Zooz/api-schema-builder?branch=master) | ||
This package is used to build schema for input validation base on openapi doc [Swagger (Open API)](https://swagger.io/specification/) definition and [ajv](https://www.npmjs.com/package/ajv) | ||
@@ -9,2 +12,4 @@ | ||
- [Install](#install) | ||
@@ -11,0 +16,0 @@ - [API](#api) |
@@ -27,4 +27,2 @@ 'use strict'; | ||
const dereferenced = swaggers[0]; | ||
const referenced = swaggers[1]; | ||
Object.keys(dereferenced.paths).forEach(function (currentPath) { | ||
@@ -40,3 +38,3 @@ let pathParameters = dereferenced.paths[currentPath].parameters || []; | ||
if (isOpenApi3){ | ||
schemas[parsedPath][currentMethod].body = oas3.buildBodyValidation(dereferenced, referenced, currentPath, currentMethod, middlewareOptions); | ||
schemas[parsedPath][currentMethod].body = oas3.buildBodyValidation(dereferenced, swaggers[1], currentPath, currentMethod, middlewareOptions); | ||
} else { | ||
@@ -51,27 +49,6 @@ let bodySchema = middlewareOptions.expectFormFieldsInBody | ||
const validatedBodySchema = oas2.getValidatedBodySchema(bodySchema); | ||
let bodySchemaReference = referenced.paths[currentPath][currentMethod].parameters.filter(function (parameter) { return parameter.in === 'body' })[0]; | ||
let schemaReference = bodySchemaReference.schema['$ref']; | ||
schemas[parsedPath][currentMethod].body = oas2.buildBodyValidation(validatedBodySchema, dereferenced.definitions, referenced, currentPath, currentMethod, parsedPath, middlewareOptions, schemaReference); | ||
schemas[parsedPath][currentMethod].body = oas2.buildBodyValidation(validatedBodySchema, dereferenced.definitions, swaggers[1], currentPath, currentMethod, parsedPath, middlewareOptions); | ||
} | ||
} | ||
//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['$ref']) : undefined; | ||
if(headersValidator || bodyValidator){ | ||
schemas[parsedPath][currentMethod].responses[statusCode] = new Validators.ResponseValidator({body:bodyValidator,headers:headersValidator}); | ||
} | ||
} | ||
}); | ||
let localParameters = parameters.filter(function (parameter) { | ||
@@ -180,31 +157,4 @@ return parameter.in !== 'body'; | ||
//split to diff parsers if needed | ||
function buildHeadersValidation(headers, middlewareOptions) { | ||
let ajv = new Ajv( {allErrors: true, coerceTypes: 'array', ...middlewareOptions.ajvConfigParams}); | ||
ajvUtils.addCustomKeyword(ajv, middlewareOptions.formats); | ||
var ajvHeadersSchema = { | ||
title: 'HTTP headers', | ||
type: 'object', | ||
properties: {}, | ||
required: [], | ||
additionalProperties: true | ||
}; | ||
Object.keys(headers).forEach(key => { | ||
let headerObj = headers[key]; | ||
const headerName = key.toLowerCase(); | ||
ajvHeadersSchema.properties[headerName] = {description:headerObj.description, type: headerObj.type}; | ||
headerObj.required && ajvHeadersSchema.required.push(key); | ||
}, this); | ||
//todo - should i need it? | ||
// ajvHeadersSchema.content = createContentTypeHeaders(middlewareOptions.contentTypeValidation, contentTypes); | ||
return new Validators.SimpleValidator(ajv.compile(ajvHeadersSchema)); | ||
} | ||
module.exports = { | ||
buildSchema: buildSchema | ||
}; |
@@ -36,8 +36,14 @@ | ||
function buildBodyValidation(schema, swaggerDefinitions, originalSwagger, currentPath, currentMethod, parsedPath, middlewareOptions = {}, schemaReference) { | ||
let ajv = new Ajv({allErrors: true, ...middlewareOptions.ajvConfigBody}); | ||
function buildBodyValidation(schema, swaggerDefinitions, originalSwagger, currentPath, currentMethod, parsedPath, middlewareOptions = {}) { | ||
const defaultAjvOptions = { | ||
allErrors: true | ||
// unknownFormats: 'ignore' | ||
}; | ||
const options = Object.assign({}, defaultAjvOptions, middlewareOptions.ajvConfigBody); | ||
let ajv = new Ajv(options); | ||
ajvUtils.addCustomKeyword(ajv, middlewareOptions.formats, middlewareOptions.keywords); | ||
if (schema.discriminator) { | ||
return buildInheritance(schema.discriminator, swaggerDefinitions, originalSwagger, currentPath, currentMethod, parsedPath, ajv, schemaReference); | ||
return buildInheritance(schema.discriminator, swaggerDefinitions, originalSwagger, currentPath, currentMethod, parsedPath, ajv); | ||
} else { | ||
@@ -47,3 +53,5 @@ return new Validators.SimpleValidator(ajv.compile(schema)); | ||
} | ||
function buildInheritance(discriminator, dereferencedDefinitions, swagger, currentPath, currentMethod, parsedPath, ajv, schemaReference) { | ||
function buildInheritance(discriminator, dereferencedDefinitions, swagger, currentPath, currentMethod, parsedPath, ajv) { | ||
let bodySchema = swagger.paths[currentPath][currentMethod].parameters.filter(function (parameter) { return parameter.in === 'body' })[0]; | ||
var inheritsObject = { | ||
@@ -57,3 +65,3 @@ inheritance: [] | ||
swagger.definitions[key].allOf.forEach(element => { | ||
if (element['$ref'] && element['$ref'] === schemaReference) { | ||
if (element['$ref'] && element['$ref'] === bodySchema.schema['$ref']) { | ||
inheritsObject[key] = ajv.compile(dereferencedDefinitions[key]); | ||
@@ -60,0 +68,0 @@ inheritsObject.inheritance.push(key); |
@@ -6,12 +6,9 @@ 'use strict'; | ||
SimpleValidator = require('./SimpleValidator'), | ||
DiscriminatorValidator = require('./DiscriminatorValidator'), | ||
ResponseValidator = require('./ResponseValidator'); | ||
DiscriminatorValidator = require('./DiscriminatorValidator'); | ||
module.exports = { | ||
Validator: Validator, | ||
OneOfValidator: OneOfValidator, | ||
ResponseValidator:ResponseValidator, | ||
SimpleValidator: SimpleValidator, | ||
DiscriminatorValidator | ||
}; | ||
}; |
@@ -14,3 +14,3 @@ 'use strict'; | ||
before(function () { | ||
const swaggerPath = path.join(__dirname, 'pets-request.yaml'); | ||
const swaggerPath = path.join(__dirname, 'pets.yaml'); | ||
return schemaValidatorGenerator.buildSchema(swaggerPath, { | ||
@@ -17,0 +17,0 @@ ajvConfigBody: { |
@@ -14,3 +14,3 @@ 'use strict'; | ||
before(function () { | ||
const swaggerPath = path.join(__dirname, 'pets-request.yaml'); | ||
const swaggerPath = path.join(__dirname, 'pets.yaml'); | ||
return schemaValidatorGenerator.buildSchema(swaggerPath, {}).then(receivedSchema => { | ||
@@ -17,0 +17,0 @@ schema = receivedSchema; |
Sorry, the diff of this file is not supported yet
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
132
345494
57
1302