# 格式校验

¥Format validation

# 字符串格式

¥String formats

从版本 7 开始,Ajv 不包含 JSON Schema 规范定义的格式 - 这些和其他几种格式由 ajv-formats (opens new window) 插件提供。

¥From version 7 Ajv does not include formats defined by JSON Schema specification - these and several other formats are provided by ajv-formats (opens new window) plugin.

要从此插件添加所有格式:

¥To add all formats from this plugin:

有关更多详细信息,请参阅 ajv-formats (opens new window) 文档。

¥See ajv-formats (opens new window) documentation for further details.

建议不要对不受信任的数据使用 "format" 关键字实现,因为它们可能使用潜在不安全的正则表达式(即使已知问题已修复) - see 重拒绝攻击

¥It is recommended NOT to use "format" keyword implementations with untrusted data, as they may use potentially unsafe regular expressions (even though known issues are fixed) - see ReDoS attack.

不可信数据的格式校验

如果你需要使用 "format" 关键字来校验不受信任的数据,你必须评估它们对你的校验场景的适用性和安全性。

¥If you need to use "format" keyword to validate untrusted data, you MUST assess their suitability and safety for your validation scenarios.

ajv-formats (opens new window) 中定义了以下格式,用于使用 "format" 关键字进行字符串校验:

¥The following formats are defined in ajv-formats (opens new window) for string validation with "format" keyword:

ajv-formats-draft2019 中的其他格式

JSON 结构草案 07 还为具有国际字符的 URL、主机名和电子邮件定义了格式 iriiri-referenceidn-hostnameidn-email。这些格式在 ajv-formats-draft2019 (opens new window) 插件中可用。

¥JSON Schema draft-07 also defines formats iri, iri-reference, idn-hostname and idn-email for URLs, hostnames and emails with international characters. These formats are available in ajv-formats-draft2019 (opens new window) plugin.

# 用户定义的格式

¥User-defined formats

你可以使用 addFormat 方法添加和替换任何格式:

¥You can add and replace any formats using addFormat method:

ajv.addFormat("identifier", /^a-z\$_[a-zA-Z$_0-9]*$/)

Ajv 还允许定义仅应用于数字的格式:

¥Ajv also allows defining the formats that would be applied to numbers only:

ajv.addFormat("byte", {
  type: "number",
  validate: (x) => x >= 0 && x <= 255 && x % 1 == 0,
})

# 格式和独立校验代码

¥Formats and standalone validation code

如果你使用 ajv-formats (opens new window) 包中的格式,则将立即支持 独立校验代码

¥If you use formats from ajv-formats (opens new window) package, standalone validation code will be supported out of the box.

独立代码和 Ajv 版本

你需要确保 ajv-formats 导入与你在应用中使用的 ajv 版本和代码相同的版本和代码,以便独立校验代码正常工作(因为当前使用的是 instanceof 检查)。

¥You need to make sure that ajv-formats imports the same version and the same code of ajv as the one you use in your application for standalone validation code to work (because of instanceof check that is currently used).

当你在应用中更新 ajv 版本时,npm 和其他包管理器可能不会更新 ajv 格式的 ajv 依赖的版本 - 解决方法是使用干净的 npm 安装。

¥npm and other package managers may not update the version of ajv dependency of ajv-formats when you update version of ajv in your application - the workaround is to use clean npm installation.

如果你定义自己的格式,为了使独立代码生成正常工作,你需要将计算结果为具有所有定义格式的对象的代码片段传递给选项 code.formats

¥If you define your own formats, for standalone code generation to work you need to pass the code snippet that evaluates to an object with all defined formats to the option code.formats: