如何让 AJV (v5.0.0) 使用 JSON Schema 0.7?

How do I get AJV (v5.0.0) to useJSON Schema 0.7?

我似乎找不到如何让 AJV 使用 0.7(或 0.6 或 0.4)。我认为是“$schema”关键字,但我找不到不会导致错误的字符串(URI)。

如果我是对的,我应该为有效的关键字“$schema”分配什么值?

如果我说错了,还有其他表示版本的方法,请告诉我。

在此先感谢您的帮助。

在 Ajv 网站上,他们建议您告诉 Ajv v6 使用非默认架构:

Please note: To use Ajv with draft-06 schemas you need to explicitly add the meta-schema to the validator instance: [1]

ajv.addMetaSchema(require('ajv/lib/refs/json-schema-draft-06.json'));

如果您安装 Ajv v5 并查看 lib/refs 文件夹,则 draft-07 架构不可用:

$data.json
json-schema-draft-04.json
json-schema-draft-06.json
json-schema-v5.json

所以我认为您不能应用类似的技术来处理 draft-07 和 Ajv v5,这可能就是您为此苦苦挣扎的原因。

我的建议是使用 Ajv v6,因为它默认适用于 draft-07:

Ajv version 6.0.0 that supports draft-07 is released. [1]

参考资料

  1. https://ajv.js.org/