Skip to content

Commit

Permalink
annotate types
Browse files Browse the repository at this point in the history
  • Loading branch information
Carmine DiMascio committed Dec 23, 2019
1 parent 847c8d9 commit 93693b7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
15 changes: 12 additions & 3 deletions src/middlewares/openapi.request.validator.ts
Expand Up @@ -16,9 +16,18 @@ import {
OpenApiRequestMetadata,
} from '../framework/types';

import { ParametersParser } from './parameters/parameters.parse';
import {
ParametersParser,
ParametersSchema,
} from './parameters/parameters.parse';
import { ParametersTransform } from './parameters/parameters.transform';

type BodySchema = OpenAPIV3.ReferenceObject | OpenAPIV3.SchemaObject | {};
export interface DraftSchema {
required: string[];
properties: ParametersSchema & BodySchema;
}

export class RequestValidator {
private _middlewareCache: { [key: string]: RequestHandler } = {};
private _apiDocs: OpenAPIV3.Document;
Expand Down Expand Up @@ -166,7 +175,7 @@ class RequestBody {
path: string,
contentType: ContentType,
requestBody: OpenAPIV3.RequestBodyObject,
): object {
): BodySchema {
if (requestBody.content) {
let content = null;
for (const type of contentType.equivalents()) {
Expand Down Expand Up @@ -196,7 +205,7 @@ class RequestBody {
ajv: Ajv,
contentType: ContentType,
requestBody: OpenAPIV3.RequestBodyObject,
): object {
): BodySchema {
const bodyContentSchema =
requestBody.content[contentType.contentType] &&
requestBody.content[contentType.contentType].schema;
Expand Down
10 changes: 5 additions & 5 deletions src/middlewares/parameters/parameters.transform.ts
Expand Up @@ -14,7 +14,7 @@ export class ParametersTransform {
this.schema = schema;
}

applyExplodedJsonTransform(req: OpenApiRequest) {
public applyExplodedJsonTransform(req: OpenApiRequest): void {
// forcing convert to object if scheme describes param as object + explode
// for easy validation, keep the schema but update whereabouts of its sub components
this.parameters.parseObjectExplode.forEach(item => {
Expand Down Expand Up @@ -42,7 +42,7 @@ export class ParametersTransform {
});
}

applyJsonTransform(req: OpenApiRequest) {
public applyJsonTransform(req: OpenApiRequest): void {
/**
* support json in request params, query, headers and cookies
* like this filter={"type":"t-shirt","color":"blue"}
Expand All @@ -63,7 +63,7 @@ export class ParametersTransform {
});
}

applyJsonArrayTransform(req: OpenApiRequest) {
public applyJsonArrayTransform(req: OpenApiRequest): void {
/**
* array deserialization
* filter=foo,bar,baz
Expand All @@ -79,7 +79,7 @@ export class ParametersTransform {
});
}

applyExplodedJsonArrayTransform(req: OpenApiRequest) {
public applyExplodedJsonArrayTransform(req: OpenApiRequest): void {
/**
* forcing convert to array if scheme describes param as array + explode
*/
Expand All @@ -93,7 +93,7 @@ export class ParametersTransform {
});
}

applyPathTransform(req: OpenApiRequest) {
public applyPathTransform(req: OpenApiRequest): void {
const openapi = <OpenApiRequestMetadata>req.openapi;
const shouldUpdatePathParams = Object.keys(openapi.pathParams).length > 0;

Expand Down

0 comments on commit 93693b7

Please sign in to comment.