Skip to content
Open
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ If the element is an object, it must include `name` and `definition`. If the ele
- `makeOptionalAttributesNullable` - Boolean that forces preprocessing of Swagger schema to include 'null' as possible type for all non-required properties. Main use-case for this is to ensure correct handling of null values when Ajv type coercion is enabled
- `ajvConfigBody` - Object that will be passed as config to new Ajv instance which will be used for validating request body. Can be useful to e. g. enable type coercion (to automatically convert strings to numbers etc). See Ajv documentation for supported values.
- `ajvConfigParams` - Object that will be passed as config to new Ajv instance which will be used for validating request headers and parameters. See Ajv documentation for supported values.
- `contentTypeValidation` - Boolean that indicates if to perform content type validation in case `consume` field is specified and the request body is not empty.
- `contentTypeValidation` - Boolean that indicates if to perform content type validation in case `consumes` (OAS2) or `content` (OAS3) field is specified and the request body is not empty.
- `expectFormFieldsInBody` - Boolean that indicates whether form fields of non-file type that are specified in the schema should be validated against request body (e. g. Multer is copying text form fields to body)
- `buildRequests` - Boolean that indicates whether if create validators for requests, default is true.
- `buildResponses` - Boolean that indicates whether if create validators for responses, default is false.
Expand Down
8 changes: 5 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ function buildValidations(referenced, dereferenced, receivedOptions) {
const schemas = {};

const basePaths = dereferenced.servers && dereferenced.servers.length
? dereferenced.servers.map(({ url }) => new URL(url).pathname)
? dereferenced.servers.map(({ url }) => new URL(url, 'http://foo').pathname)
Copy link
Collaborator

Choose a reason for hiding this comment

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

why 'http://foo'?

Copy link
Author

Choose a reason for hiding this comment

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

there has to be an origin, but any origin works (we just want to extract the pathname)

: [dereferenced.basePath || '/'];

Object.keys(dereferenced.paths).forEach(currentPath => {
Expand Down Expand Up @@ -123,8 +123,10 @@ function buildRequestValidator(referenced, dereferenced, currentPath, currentMet
}

if (localParameters.length > 0 || options.contentTypeValidation) {
requestSchema.parameters = buildParametersValidation(localParameters,
dereferenced.paths[currentPath][currentMethod].consumes || dereferenced.paths[currentPath].consumes || dereferenced.consumes, options);
const contentTypes = isOpenApi3 ?
Copy link
Collaborator

Choose a reason for hiding this comment

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

i know the code base isn't pretty to say least, but i suggest that will try not to make it even harder to read. what do you say about extracting this to a function?

Copy link
Collaborator

Choose a reason for hiding this comment

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

i'd also suggest adding a test for covering both cases when content type exists and missing from the request body

Object.keys( get(dereferenced.paths[currentPath][currentMethod], 'requestBody.content') || {} ) :
( dereferenced.paths[currentPath][currentMethod].consumes || dereferenced.paths[currentPath].consumes || dereferenced.consumes );
requestSchema.parameters = buildParametersValidation(localParameters, contentTypes, options);
}

return requestSchema;
Expand Down