compile method
Validator.compile(schema, cacheKey?) will compile the JSON Schema into an AJV validation function and reuse the cache when passing in cacheKey.
method signature
Parameters
schema- JSON Schema objectcacheKey- Optional cache key; after passing it in, the compiled results can be reused within the sameValidatorinstance
return value
Returns the AJV validation function, which can be called directly like a normal function; the execution result is true / false, and the error details are hung on validate.errors.
Applicable scenarios
- When the same schema needs to be validated multiple times,
compile()first and then reuse the compilation results - You need to control the cache keys yourself to avoid repeated compilation overhead.
- Want to separate compilation and execution and connect to custom process
Things to note
- The cache scope of
cacheKeyis the currentValidatorinstance and is not shared across instances. - If you just do batch validation on the same schema, you can also use
validator.validateBatch()directly.
Corresponding sample file
Example entry: compile.ts
Description: Covers the reuse of compilation results of compile(), hits of cacheKey, and reading error details from the validation function in failure scenarios.