docs/guide/environments.md
[[toc]]
The main consideration for using Ajv server-side is to manage compiled schemas correctly, ensuring that the same schema is not compiled more than once.
Depending on the life-time of the environments, the benefits from "compile once - validate many times" model can be limited - you can consider using standalone validation code.
If you have a pre-defined set of schemas, you can:
Please see gajus/table package that pre-compiles schemas in this way.
Even if your schemas need to be stored in the database, you can still compile schemas once and store your validation functions alongside schemas in the database as well, loading them on demand.
See Content Security Policy to decide how best to use Ajv in the browser for your use case.
Whether you compile schemas in the browser or use standalone validation code, it is recommended that you bundle them together with your application code.
If you need to use Ajv in several application bundles you can create a separate UMD bundles of Ajv using npm run bundle script.
In this case you need to load Ajv using the correct bundle, depending on which schema language and which version you need to use:
<code-group> <code-block title="JSON Schema (draft-07)"> ```html <script src="bundle/ajv7.min.js"></script> <script> ;(function () { const Ajv = window.ajv7 const ajv = new Ajv() })() </script> ``` </code-block> <code-block title="JSON Schema (draft-2019-09)"> ```html <script src="bundle/ajv2019.min.js"></script> <script> ;(function () { const Ajv = window.ajv2019 const ajv = new Ajv() })() </script> ``` </code-block> <code-block title="JSON Type Definition"> ```html <script src="bundle/ajvJTD.min.js"></script> <script> ;(function () { const Ajv = window.ajvJTD const ajv = new Ajv() })() </script> ``` </code-block> </code-group>This bundle can be used with different module systems; it creates global ajv/ajv2019/ajvJTD if no module system is found.
The browser bundles are available on cdnjs.
::: warning Some frameworks re-define require
Some frameworks, e.g. Dojo, may redefine global require in a way that is not compatible with CommonJS module format. In this case Ajv bundle has to be loaded before the framework and then you can use global ajv (see issue #234).
:::
::: warning Internet Explorer 11
Ajv v8 in IE 11 will not work straight out of the box. To use it either recompile it, or set the options unicodeRegExp to false and code: { es5: true }, and transpile the Ajv node module (see issue #1585).
:::
You need to:
const ajv = new Ajv({code: {es5: true}})
See Advanced options.
The default configuration of AJV is to generate code in ES6 with Common JS (CJS) exports. This can be changed by setting the ES Modules(ESM) flag.
const ajv = new Ajv({code: {esm: true}})
Ajv is used in other JavaScript environments, including Electron apps, WeChat mini-apps and many others, where the same considerations apply as above:
If any of this is important, you may have better results with pre-compiled standalone validation code.
Ajv can be used from the terminal in any operating system supported by Node.js
CLI is available as a separate npm package ajv-cli.
It supports: