You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: docs/standalone.md
+190-33
Original file line number
Diff line number
Diff line change
@@ -2,83 +2,241 @@
2
2
3
3
[[toc]]
4
4
5
-
Ajv supports generating standalone modules with exported validation function(s), with one default export or multiple named exports, that are pre-compiled and can be used without Ajv. It is useful for several reasons:
5
+
Ajv supports generating standalone validation functions from JSON Schemas at compile/build time. These functions can then be used during runtime to do validation without initialising Ajv. It is useful for several reasons:
6
6
7
7
- to reduce the browser bundle size - Ajv is not included in the bundle (although if you have a large number of schemas the bundle can become bigger - when the total size of generated validation code is bigger than Ajv code).
8
8
- to reduce the start-up time - the validation and compilation of schemas will happen during build time.
9
9
- to avoid dynamic code evaluation with Function constructor (used for schema compilation) - when it is prohibited by the browser page [Content Security Policy](./security.md#content-security-policy).
10
10
11
-
This functionality in Ajv v7 supersedes deprecated package ajv-pack that can be used with Ajv v6. All schemas, including those with recursive references, formats and user-defined keywords are supported, with few [limitations](#configuration-and-limitations).
11
+
This functionality in Ajv v7 supersedes the deprecated package ajv-pack that can be used with Ajv v6. All schemas, including those with recursive references, formats and user-defined keywords are supported, with few [limitations](#configuration-and-limitations).
12
12
13
-
## Usage with CLI
13
+
## Two-step process
14
14
15
-
In most cases you would use this functionality via [ajv-cli](https://github.com/ajv-validator/ajv-cli) (>= 4.0.0) to generate module that exports validation function.
15
+
The **first step** is to **generate** the standalone validation function code. This is done at compile/build time of your project and the output is a generated JS file. The **second step** is to **use** the generated JS validation function.
16
16
17
+
There are two methods to generate the code, using either the Ajv CLI or the Ajv JS library. There are also a few different options that can be passed when generating code. Below is just a highlight of a few options:
18
+
19
+
- Set the `code.source` (JS) value to true or use the `compile` (CLI) command to generate standalone code.
20
+
- The standalone code can be generated in either ES5 or ES6, it defaults to ES6. Set the `code.es5` (JS) value to true or pass the `--code-es5` (CLI) flag to true if you want ES5 code.
21
+
- The standalone code can be generated in either CJS (module.export & require) or ESM (exports & import), it defaults to CJS. Set the `code.esm` (JS) value to true or pass the `--code-esm` (CLI) flag if you want ESM exported code.
22
+
23
+
Note that the way the function is exported, differs if you are exporting a single or multiple schemas. See examples below.
24
+
25
+
### Generating function(s) using CLI
26
+
27
+
In most cases you would use this functionality via [ajv-cli](https://github.com/ajv-validator/ajv-cli) (>= 4.0.0) to generate the standalone code that exports the validation function. See [ajv-cli](https://github.com/ajv-validator/ajv-cli#compile-schemas) docs and the [cli options](https://github.com/ajv-validator/ajv-cli#ajv-options) for additional information.
28
+
29
+
#### Using the defaults - ES6 and CJS exports
17
30
```sh
18
31
npm install -g ajv-cli
19
32
ajv compile -s schema.json -o validate_schema.js
20
33
```
21
34
22
-
`validate_schema.js` will contain the module exporting validation function that can be bundled into your application.
23
-
24
-
See [ajv-cli](https://github.com/ajv-validator/ajv-cli) docs for additional information.
25
-
26
-
## Usage from code
35
+
### Generating using the JS library
27
36
37
+
Install the package, version >= v7.0.0:
28
38
```sh
29
39
npm install ajv
30
40
```
31
41
42
+
#### Generating functions(s) for a single schema using the JS library - ES6 and CJS exports
43
+
32
44
```javascript
33
-
constAjv=require("ajv") // version >= v7.0.0
34
-
constajv=newAjv({code: {source:true}}) // this option is required to generate standalone code
To run the standalone generated functions, the Ajv package should still be a run-time dependency for most schemas, but generated modules can only depend on code in [runtime](https://github.com/ajv-validator/ajv/tree/master/lib/runtime) folder, so the whole Ajv will not be included in the bundle (or executed) if you require the modules with standalone validation code from your application code.
229
+
One of the main reason for using the standalone mode is to start applications faster to avoid runtime schema compilation.
230
+
231
+
The standalone generated functions still has a dependency on the Ajv. Specifically on the code in the [runtime](https://github.com/ajv-validator/ajv/tree/master/lib/runtime) folder of the package.
232
+
233
+
Completely isolated validation functions can be generated if desired (won't be for most use cases). Run the generated code through a bundler like ES Build to create completely isolated validation functions that can be imported/required without any dependency on Ajv. This is also not needed if your project is already using a bundler.
76
234
77
235
## Configuration and limitations
78
236
79
237
To support standalone code generation:
80
238
81
-
- Ajv option `source.code` must be set to `true`
239
+
- Ajv option `code.source` must be set to `true`
82
240
- only `code` and `macro` user-defined keywords are supported (see [User defined keywords](./keywords.md)).
83
241
- when `code` keywords define variables in shared scope using `gen.scopeValue`, they must provide `code` property with the code snippet. See source code of pre-defined keywords for examples in [vocabularies folder](https://github.com/ajv-validator/ajv/blob/master/lib/vocabularies).
84
242
- if formats are used in standalone code, ajv option `code.formats` should contain the code snippet that will evaluate to an object with all used format definitions - it can be a call to `require("...")` with the correct path (relative to the location of saved module):
@@ -93,6 +251,5 @@ const ajv = new Ajv({
93
251
formats: _`require("./my-formats")`,
94
252
},
95
253
})
96
-
```
97
254
98
255
If you only use formats from [ajv-formats](https://github.com/ajv-validator/ajv-formats) this option will be set by this package automatically.
0 commit comments