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

fix: observe validateApiSpec and avoid schema re-checks (performance) #544

Merged
merged 16 commits into from Feb 28, 2021

Conversation

cdimascio
Copy link
Owner

@cdimascio cdimascio commented Feb 28, 2021

related to @aaronluman's #543

fixes:

behavior:

  • ensure validateApiSpec is observed. currently, setting it to false is ignored
  • disables schema validation on subsequent validations since validation will have occurred on startup if validateApiSpec is true.
  • if validateApiSpec is false schema validation is skipped entirely which was the initial intended behavior

}

return {
validateSchema: false, // only needed on startup
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean by "only needed on startup"?
With this set to false it blocks ajv validations from running regardless of the value of validateApiSpec. (the ajv check catches duplicate enum values, for example).

adding validateApiSpec to the const { ... } = this.options on line 48 and changing this line to validateSchema: validateApiSpec allows for full validation suppression when validateApiSpec is set to false but still allows for full schema validation otherwise

Copy link
Owner Author

@cdimascio cdimascio Feb 28, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch...

Schema validation occurs here on startup https://github.com/cdimascio/express-openapi-validator/blob/master/src/framework/openapi.schema.validator.ts#L10

that said, ajv isn’t set up the same way there as it is for request and response validation. Perhaps it should be. It may make sense for the startup code above to create an ajv instance by calling the code below (but setting validateSchema true and skipping the req/res specific logic) https://github.com/cdimascio/express-openapi-validator/blob/master/src/framework/ajv/index.ts#L21.
And, in addition, the startup ajv instance will still need the draft schema and open api schema, etc setup.

Doing something like this may make it possible to do a complete validate on startup, which should make it okay to set validateSchema to false in all other cases

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

interesting. running the tests this morning everything appears to be working as expected. The startup check is catching the schema errors when I set validateApiSpec: true.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fantastic! i'll work to get this merged in. thanks for your help

@cdimascio
Copy link
Owner Author

@all-contributors add @aaronluman for code and test

@allcontributors
Copy link
Contributor

@cdimascio

I've put up a pull request to add @aaronluman! 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants