# 执行环境
¥Execution environments
# 服务器端 Node.js
¥Server-side Node.js
使用 Ajv 服务器端的主要考虑因素是正确 管理编译结构,确保相同的结构不会被编译多次。
¥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.
# 短暂的环境
¥Short-lived environments
根据环境的生命周期,“编译一次”的好处 - 校验多次”模型可能受到限制 - 你可以考虑使用 独立校验代码。
¥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:
编译构建步骤中的所有模式 - 你可以编写自己的脚本或使用 ajv-cli (opens new window)。
¥compile all schemas in the build step - you can either write your own script or use ajv-cli (opens new window).
生成并美化独立校验代码 - 你可以从一个文件导出所有模式。
¥generate and beautify standalone validation code - you can have all your schemas exported from one file.
此外,你可以使用任何打包工具内联对 Ajv 或 ajv 格式的所有依赖。
¥additionally, you can inline all dependencies on Ajv or ajv-formats using any bundling tools.
将已编译的结构部署为应用或库的一部分(依赖或不依赖 Ajv,取决于你是否执行了步骤 3 以及结构中使用了哪些校验关键字)
¥deploy compiled schemas as part of your application or library (with or without dependency on Ajv, depending on whether you did step 3 and which validation keywords are used in the schemas)
请参阅 gajus/table (opens new window) 包,它以这种方式预编译结构。
¥Please see gajus/table (opens new window) 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.
# 浏览器
¥Browsers
请参阅 内容安全政策 来决定如何根据你的用例在浏览器中最好地使用 Ajv。
¥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.
如果你需要在多个应用包中使用 Ajv,你可以使用 npm run bundle
脚本创建单独的 Ajv UMD 包。
¥If you need to use Ajv in several application bundles you can create a separate UMD bundles of Ajv using npm run bundle
script.
在这种情况下,你需要使用正确的包加载 Ajv,具体取决于你需要使用的结构语言和版本:
¥In this case you need to load Ajv using the correct bundle, depending on which schema language and which version you need to use:
该打包包可与不同的模块系统一起使用;如果没有找到模块系统,它会创建全局 ajv
/ajv2019
/ajvJTD
。
¥This bundle can be used with different module systems; it creates global ajv
/ajv2019
/ajvJTD
if no module system is found.
浏览器打包包已在 cdnjs (opens new window) 上提供。
¥The browser bundles are available on cdnjs (opens new window).
一些框架重新定义了 require
一些框架,例如 Dojo 可能会以与 CommonJS 模块格式不兼容的方式重新定义全局 require。在这种情况下,必须在框架之前加载 Ajv 包,然后你可以使用全局 ajv
(请参阅问题 #234 (opens new window))。
¥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 (opens new window)).
互联网浏览器 11
IE 11 中的 Ajv v8 不能直接开箱即用。要使用它 重新编译它,或将选项 unicodeRegExp 设置为 false
和 code: { es5: true }
,并转译 Ajv 节点模块(请参阅问题 #1585 (opens new window))。
¥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 (opens new window)).
# ES5 环境
¥ES5 environments
你需要:
¥You need to:
将 Typescript 重新编译为 ES5 目标 - 在打包的编译代码中它被设置为 2018。
¥recompile Typescript to ES5 target - it is set to 2018 in the bundled compiled code.
生成 ES5 校验码:
¥generate ES5 validation code:
const ajv = new Ajv({code: {es5: true}})
¥See Advanced options (opens new window).
# CJS 与 ESM 导出
¥CJS vs ESM exports
AJV 的默认配置是使用 Common JS (CJS) 导出生成 ES6 代码。这可以通过设置 ES 模块 (ESM) 标志来更改。
¥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}})
# 其他 JavaScript 环境
¥Other JavaScript environments
Ajv 用于其他 JavaScript 环境,包括 Electron 应用、微信小应用和许多其他环境,其中的考虑因素与上述相同:
¥Ajv is used in other JavaScript environments, including Electron apps, WeChat mini-apps and many others, where the same considerations apply as above:
编译性能
¥compilation performance
限制性内容安全策略
¥restrictive content security policy
打包尺寸
¥bundle size
如果其中任何一个很重要,那么使用预编译的 独立校验代码 可能会获得更好的结果。
¥If any of this is important, you may have better results with pre-compiled standalone validation code.
# 命令行接口
¥Command line interface
Ajv 可以在 Node.js 支持的任何操作系统中从终端使用
¥Ajv can be used from the terminal in any operating system supported by Node.js
CLI 作为单独的 npm 包 ajv-cli (opens new window) 提供。
¥CLI is available as a separate npm package ajv-cli (opens new window).
它支持:
¥It supports:
编译 JSON 结构以测试其有效性
¥compiling JSON Schemas to test their validity
生成导出校验函数的 独立校验代码
¥generating standalone validation code that exports validation function(s)
将结构迁移到 Draft-07 和 Draft-2019-09(使用 json-schema-migrate (opens new window))
¥migrating schemas to draft-07 and draft-2019-09 (using json-schema-migrate (opens new window))
根据 JSON 模式校验数据文件
¥validating data file(s) against JSON Schema
根据 JSON 结构测试数据的预期有效性
¥testing expected validity of data against JSON Schema
引用结构
¥referenced schemas
用户定义的元结构、校验关键字和格式
¥user-defined meta-schemas, validation keywords and formats
JSON、JSON5、YAML 和 JavaScript 格式的文件
¥files in JSON, JSON5, YAML, and JavaScript format
所有 Ajv 选项
¥all Ajv options
以 JSON-patch (opens new window) 格式报告校验后数据的变化
¥reporting changes in data after validation in JSON-patch (opens new window) format