Skip to content

Commit

Permalink
fix: multipart middlware ajv options
Browse files Browse the repository at this point in the history
  • Loading branch information
LEI committed Nov 8, 2020
1 parent 46ed8fb commit 72253ce
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
5 changes: 3 additions & 2 deletions src/framework/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ export type SecurityHandlers = {
) => boolean | Promise<boolean>;
};

export interface MultipartOpts extends ajv.Options {
multerOpts: any;
export interface MultipartOpts {
multerOpts: boolean | multer.Options;
ajvOpts: ajv.Options;
}

export interface RequestValidatorOptions
Expand Down
4 changes: 1 addition & 3 deletions src/middlewares/openapi.multipart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ export function multipart(
options: MultipartOpts,
): OpenApiRequestHandler {
const mult = multer(options.multerOpts);
const Ajv = createRequestAjv(apiDoc, {
unknownFormats: options.unknownFormats,
});
const Ajv = createRequestAjv(apiDoc, { ...options.ajvOpts });
return (req, res, next) => {
// TODO check that format: binary (for upload) else do not use multer.any()
// use multer.none() if no binary parameters exist
Expand Down
15 changes: 12 additions & 3 deletions src/openapi.validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ export class OpenApiValidator {
}

installMiddleware(spec: Promise<Spec>): OpenApiRequestHandler[] {
const { formats } = this.options;
const middlewares: OpenApiRequestHandler[] = [];
const pContext = spec.then((spec) => {
const responseApiDoc = this.options.validateResponses
Expand All @@ -98,7 +97,7 @@ export class OpenApiValidator {
useDefaults: true,
unknownFormats: this.options.unknownFormats,
format: this.options.validateFormats,
formats: formats.reduce((acc, f) => {
formats: this.options.formats.reduce((acc, f) => {
acc[f.name] = {
type: f.type,
validate: f.validate,
Expand Down Expand Up @@ -257,7 +256,17 @@ export class OpenApiValidator {
private multipartMiddleware(apiDoc: OpenAPIV3.Document) {
return middlewares.multipart(apiDoc, {
multerOpts: this.options.fileUploader,
unknownFormats: this.options.unknownFormats,
ajvOpts: {
unknownFormats: this.options.unknownFormats,
format: this.options.validateFormats,
formats: this.options.formats.reduce((acc, f) => {
acc[f.name] = {
type: f.type,
validate: f.validate,
};
return acc;
}, {}),
},
});
}

Expand Down

0 comments on commit 72253ce

Please sign in to comment.