[RFC] Improve the JSON schema validation performance #4706
chenjiahan
started this conversation in
RFC Discussions
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Background
Ajv is one of the most popular JSON schema validator, but it is slow in Modern.js.
In Modern.js Framework and Modern.js Module, running Ajv causes every CLI command to take an extra 50~150ms of time. This is because Ajv needs to compile schemas into functions, which is quite slow.
According to the Ajv document:
And some of the Modern.js config schemas are dynamically provided by different plugins, so we can not pre-compile them to improve performance.
Altenatives
Meanwhile, Rspack and Modern.js Builder use Zod, were shown to be 20x faster (but currently Zod's error logs are not as friendly as Ajv).
And some new tools choose not to use schema validation, such as Vite and Remix. Since most users use
.ts
config files, TypeScript will help users to find invalid configurations.Considering the complexity of Modern.js configuration, we still need a schema validator, so Zod can be the best option at the moment. We can migrate all JSON schemas to Zod, and provide a new plugin hook to extend the Zod schemas. This change will make the schema validation faster and easier to maintain (because Zod is TypeScript friendly).
Drawbacks
The
validateSchema
plugin hook of Modern.js won't work anymore, but I think it's rarely used by 3rd party plugins, and this does not affect the functionality of the plugin.Beta Was this translation helpful? Give feedback.
All reactions