openapi-backend icon indicating copy to clipboard operation
openapi-backend copied to clipboard

Add option to use AJV Standalone

Open rehanvdm opened this issue 2 years ago • 3 comments

I saw this issue https://github.com/anttiviljami/openapi-backend/issues/114 and the resulting PR, which lazy loads schemas, which is great for environments like AWS Lambda.

There is now a new method in AJV 8 to generate the schemas to JS functions at compile time so that they can be used at runtime, no more compiling schemas at runtime which saves a lot of time. You can read more about the process here: https://ajv.js.org/standalone.html and I also wrote a blog about it here: https://www.rehanvdm.com/blog/typescript-type-safety-with-ajv-standalone. . Fastify also added the option recently, might be worth seeing their implementation.

One of the main reasons I chose this package is because it uses AJV for validation instead of the other validation libraries that can be slow.

rehanvdm avatar Jan 22 '22 08:01 rehanvdm

Amazing! Thanks for bringing this to my attention @rehanvdm 💪

anttiviljami avatar Jan 22 '22 09:01 anttiviljami

So I have a 3000 lines Open API spec that takes about 7 seconds to load on a AWS Lambda @1024MB when quick is set to false 😲. With quick true it takes about 80ms, and then the input (path, query, body) validation about 100ms and the response about 10ms. That only happens the first time so the quick option is doing its job. I still feel that it can be improved by using the standalone mode.

Do you have any plans to maybe tackle this within the next few weeks/months/roadmap? Do you think it would be possible to somehow use the customizeAjv function when creating the OpenAPIBackend object to load the schemas in there then?

rehanvdm avatar Feb 02 '22 13:02 rehanvdm

@rehanvdm tbh no clue when I'd get to this. PRs would be highly appreciated <3

anttiviljami avatar Feb 20 '22 14:02 anttiviljami