Skip to content

Commit

Permalink
Add validateApiDoc props in OpenApiValidatorOpts (#525)
Browse files Browse the repository at this point in the history
* Add validateApiDoc props in OpenApiValidatorOpts

* rename validateApiSpec to validateApiDoc

Co-authored-by: Carmine DiMascio <[email protected]>
  • Loading branch information
ZhouJian26 and cdimascio authored Feb 3, 2021
1 parent d09f1a2 commit cfb9fb7
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 4 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,7 @@ OpenApiValidator.middleware({
apiSpec: './openapi.yaml',
validateRequests: true,
validateResponses: true,
validateApiSpec: true,
validateSecurity: {
handlers: {
ApiKeyAuth: (req, scopes, schema) => {
Expand Down Expand Up @@ -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.
Expand Down
6 changes: 3 additions & 3 deletions src/framework/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ export class OpenAPIFramework {
return acc;
}, new Set<string>()),
);
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) {
Expand Down
3 changes: 2 additions & 1 deletion src/framework/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -412,7 +413,7 @@ export interface OpenAPIFrameworkPathObject {

interface OpenAPIFrameworkArgs {
apiDoc: OpenAPIV3.Document | string;
validateApiDoc?: boolean;
validateApiSpec?: boolean;
$refParser?: {
mode: 'bundle' | 'dereference';
};
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ function openapiValidator(options: OpenApiValidatorOpts) {
return oav.installMiddleware(
new OpenApiSpecLoader({
apiDoc: options.apiSpec,
validateApiSpec: options.validateApiSpec,
$refParser: options.$refParser,
}).load(),
);
Expand Down

0 comments on commit cfb9fb7

Please sign in to comment.