-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support in OpenApi 3.0
- Loading branch information
Showing
23 changed files
with
1,622 additions
and
227 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
|
||
'use strict'; | ||
/** Class representing a node in a tree structure, which each node has value and children(nodes) saving by key. */ | ||
class Node { | ||
/** | ||
* Create a node. | ||
* @param value - The value of the node. | ||
*/ | ||
constructor(value){ | ||
this.value = value; | ||
this.childrenAsKeyValue = {}; | ||
} | ||
/** | ||
* Add child to the node. | ||
* @param node - The node which going to be the child. | ||
* @param key - The key which is the identifier of the child. | ||
*/ | ||
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. | ||
*/ | ||
setData(node){ | ||
if (node instanceof Node){ | ||
this.value = node.value; | ||
this.childrenAsKeyValue = node.childrenAsKeyValue; | ||
} | ||
}; | ||
/** | ||
* Get node value. | ||
* @return The value of the node. | ||
*/ | ||
getValue(){ | ||
return this.value; | ||
} | ||
} | ||
|
||
module.exports = {Node}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
|
||
const Validators = require('../validators'), | ||
Ajv = require('ajv'), | ||
ajvUtils = require('../utils/ajv-utils'); | ||
|
||
module.exports = { | ||
getValidatedBodySchema, | ||
buildBodyValidation | ||
}; | ||
|
||
function getValidatedBodySchema(bodySchema) { | ||
let validatedBodySchema; | ||
if (bodySchema[0].in === 'body') { | ||
// if we are processing schema for a simple JSON payload, no additional processing needed | ||
validatedBodySchema = bodySchema[0].schema; | ||
} else if (bodySchema[0].in === 'formData') { | ||
// if we are processing multipart form, assemble body schema from form field schemas | ||
validatedBodySchema = { | ||
required: [], | ||
properties: {} | ||
}; | ||
bodySchema.forEach((formField) => { | ||
if (formField.type !== 'file') { | ||
validatedBodySchema.properties[formField.name] = { | ||
type: formField.type | ||
}; | ||
if (formField.required) { | ||
validatedBodySchema.required.push(formField.name); | ||
} | ||
} | ||
}); | ||
} | ||
return validatedBodySchema; | ||
} | ||
|
||
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); | ||
|
||
if (schema.discriminator) { | ||
return buildInheritance(schema.discriminator, swaggerDefinitions, originalSwagger, currentPath, currentMethod, parsedPath, ajv); | ||
} else { | ||
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]; | ||
var inheritsObject = { | ||
inheritance: [] | ||
}; | ||
inheritsObject.discriminator = discriminator; | ||
|
||
Object.keys(swagger.definitions).forEach(key => { | ||
if (swagger.definitions[key].allOf) { | ||
swagger.definitions[key].allOf.forEach(element => { | ||
if (element['$ref'] && element['$ref'] === bodySchema.schema['$ref']) { | ||
inheritsObject[key] = ajv.compile(dereferencedDefinitions[key]); | ||
inheritsObject.inheritance.push(key); | ||
} | ||
}); | ||
} | ||
}, this); | ||
|
||
return new Validators.OneOfValidator(inheritsObject); | ||
} |
Oops, something went wrong.