# 为什么使用 AJV
¥Why use AJV
# 编写更多代码
¥Write less code
确保你的数据一接收到就是有效的
¥Ensure your data is valid as soon as it's received
你无需将数据校验和清理逻辑编写为冗长的代码,而是可以使用简洁、易于阅读和跨平台的 JSON 结构 (opens new window) 或 JSON 类型定义 (opens new window) 规范来声明数据要求,并在数据到达应用后立即对其进行校验。
¥Instead of having your data validation and sanitization logic written as lengthy code, you can declare the requirements to your data with concise, easy to read and cross-platform JSON Schema (opens new window) or JSON Type Definition (opens new window) specifications and validate the data as soon as it arrives to your application.
TypeScript 用户可以使用校验函数作为类型保护,具有类型级别保证,如果你的数据经过校验 - it is correct.
¥TypeScript users can use validation functions as type guards, having type level guarantee that if your data is validated - it is correct.
阅读 入门 和 与 TypeScript 一起使用 的更多内容
¥Read more in Getting started and Using with TypeScript
# 超级快速且安全
¥Super fast & secure
将你的结构编译为优化的 JavaScript 代码
¥Compiles your schemas to optimized JavaScript code
Ajv 生成代码将 JSON 结构转换为超快速的校验函数,对 v8 优化非常有效。
¥Ajv generates code to turn JSON Schemas into super-fast validation functions that are efficient for v8 optimization.
根据这些基准,目前 Ajv 是最快、最符合标准的校验器:
¥Currently Ajv is the fastest and the most standard compliant validator according to these benchmarks:
json-schema-benchmark (opens new window) - 比第二名快 50%
¥json-schema-benchmark (opens new window) - 50% faster than the second place
jsck 基准 (opens new window) - 速度提高 20-190%
¥jsck benchmark (opens new window) - 20-190% faster
Ajv 是在没有完全符合 JSON Schema 规范的校验器的情况下设计的,旨在通过将 JSON Schema 即时编译为代码来实现最佳的校验性能。Ajv 实现了速度和严谨性,但最初安全性是事后才想到的 - 根据用户的报告,许多安全漏洞已得到修复。
¥Ajv was designed at the time when there were no validators fully complying with JSON Schema specification, aiming to achieve the best possibly validation performance via just-in-time compilation of JSON schemas to code. Ajv achieved both speed and rigour, but initially security was an afterthought - many security flaws have been fixed thanks to the reports from its users.
Ajv 版本 7 进行了重建,将安全代码生成嵌入其设计中作为主要目标 - 即使你使用不受信任的模式(仍然不推荐),也有针对远程代码执行的类型级保证。
¥Ajv version 7 was rebuilt to have secure code generation embedded in its design as the primary objective - even if you use untrusted schemas (which is still not recommended) there are type-level guarantees against remote code execution.
阅读 代码生成设计 的更多内容
¥Read more in Code generation design
# 多种标准
¥Multi-standard
使用 JSON 类型定义或 JSON 结构
¥Use JSON Type Definition or JSON Schema
除了多个 JSON 结构 草案(包括最新的 2020-12 草案)之外,Ajv 还支持 JSON 类型定义 - 新的 RFC8927 (opens new window) 提供了一个更简单的 JSON 模式替代方案。JTD 旨在与类型系统保持良好一致,拥有用于多种语言的校验和类型代码生成的工具。
¥In addition to the multiple JSON Schema drafts, including the latest draft 2020-12, Ajv has support for JSON Type Definition - a new RFC8927 (opens new window) that offers a much simpler alternative to JSON Schema. Designed to be well-aligned with type systems, JTD has tools for both validation and type code generation for multiple languages.
阅读 选择结构语言 的更多内容
¥Read more in Choosing schema language
入门 →