Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add validateApiDoc props in OpenApiValidatorOpts #525

Merged
merged 2 commits into from
Feb 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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