From cfb9fb7f105bb04e8a71045d8a8b4c11b636e56f Mon Sep 17 00:00:00 2001 From: Zhou Jian Date: Wed, 3 Feb 2021 03:47:53 +0100 Subject: [PATCH] Add validateApiDoc props in OpenApiValidatorOpts (#525) * Add validateApiDoc props in OpenApiValidatorOpts * rename validateApiSpec to validateApiDoc Co-authored-by: Carmine DiMascio --- README.md | 9 +++++++++ src/framework/index.ts | 6 +++--- src/framework/types.ts | 3 ++- src/index.ts | 1 + 4 files changed, 15 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 03237ea5..37d56b4c 100644 --- a/README.md +++ b/README.md @@ -478,6 +478,7 @@ OpenApiValidator.middleware({ apiSpec: './openapi.yaml', validateRequests: true, validateResponses: true, + validateApiSpec: true, validateSecurity: { handlers: { ApiKeyAuth: (req, scopes, schema) => { @@ -647,6 +648,14 @@ Determines whether the validator should validate securities e.g. apikey, basic, } ``` +### ▪️ validateApiSpec (optional) + +Determines whether the validator should validate the OpenAPI specification. Useful if you are certain that the api spec is syntactically correct and want to bypass this check. + +- `true` (**default**) - validate the OpenAPI specification. +- `false` - do not validate the OpenAPI specification. + + ### ▪️ formats (optional) Defines a list of custome formats. diff --git a/src/framework/index.ts b/src/framework/index.ts index 2e3fe0de..d5bdf4e8 100644 --- a/src/framework/index.ts +++ b/src/framework/index.ts @@ -31,14 +31,14 @@ export class OpenAPIFramework { return acc; }, new Set()), ); - const validateApiDoc = - 'validateApiDoc' in args ? !!args.validateApiDoc : true; + const validateApiSpec = + 'validateApiSpec' in args ? !!args.validateApiSpec : true; const validator = new OpenAPISchemaValidator({ version: apiDoc.openapi, // extensions: this.apiDoc[`x-${args.name}-schema-extension`], }); - if (validateApiDoc) { + if (validateApiSpec) { const apiDocValidation = validator.validate(apiDoc); if (apiDocValidation.errors.length) { diff --git a/src/framework/types.ts b/src/framework/types.ts index 8f20d01b..9f8a2498 100644 --- a/src/framework/types.ts +++ b/src/framework/types.ts @@ -76,6 +76,7 @@ export type Serializer = { export interface OpenApiValidatorOpts { apiSpec: OpenAPIV3.Document | string; + validateApiSpec?: boolean; validateResponses?: boolean | ValidateResponseOpts; validateRequests?: boolean | ValidateRequestOpts; validateSecurity?: boolean | ValidateSecurityOpts; @@ -412,7 +413,7 @@ export interface OpenAPIFrameworkPathObject { interface OpenAPIFrameworkArgs { apiDoc: OpenAPIV3.Document | string; - validateApiDoc?: boolean; + validateApiSpec?: boolean; $refParser?: { mode: 'bundle' | 'dereference'; }; diff --git a/src/index.ts b/src/index.ts index 9d0ce703..e839b59a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -35,6 +35,7 @@ function openapiValidator(options: OpenApiValidatorOpts) { return oav.installMiddleware( new OpenApiSpecLoader({ apiDoc: options.apiSpec, + validateApiSpec: options.validateApiSpec, $refParser: options.$refParser, }).load(), );