我正在尝试使用 JSON Schema 规范中的 meta-schema 示例。
我将新的主元模式放入一个名为 new-main-meta-schema.json
的文件中{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://example.com/meta/general-use-example",
"$dynamicAnchor": "meta",
"$vocabulary": {
"https://json-schema.org/draft/2020-12/vocab/core": true,
"https://json-schema.org/draft/2020-12/vocab/applicator": true,
"https://json-schema.org/draft/2020-12/vocab/validation": true,
"https://example.com/vocab/example-vocab": true
},
"allOf": [
{"$ref": "https://json-schema.org/draft/2020-12/meta/core"},
{"$ref": "https://json-schema.org/draft/2020-12/meta/applicator"},
{"$ref": "https://json-schema.org/draft/2020-12/meta/validation"},
{"$ref": "https://example.com/meta/example-vocab"}
],
"patternProperties": {
"^unevaluated": false
},
"properties": {
"localKeyword": {
"$comment": "Not in vocabulary, but validated if used",
"type": "string"
}
}
}
该元模式引用了新的扩展模式。我将其放入名为 extension-schema.json
的文件中{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://example.com/meta/example-vocab",
"$dynamicAnchor": "meta",
"$vocabulary": {
"https://example.com/vocab/example-vocab": true
},
"type": ["object", "boolean"],
"properties": {
"minDate": {
"type": "string",
"pattern": "[0-9]{4}-[0-9]{2}-[0-9]{2}",
"format": "date"
}
}
}
现在我创建了一个符合新元架构的架构。我将架构放入名为 schema.json
的文件中{
"$schema": "https://example.com/meta/general-use-example",
"type": "object",
"properties": {
"departingGate": {"type": "string"},
"minDate": "2023-08-07"
}
}
然后我创建了该架构的一个实例。我将实例放入名为 instance.json
的文件中{
"departingGate": "Gate 26"
}
所有这些看起来都正确吗?
接下来,我想根据 schema.json 验证 instance.json,因此我打开浏览器到在线 JSON 模式验证器工具 Hyperjump。我将 new-main-meta-schema.json 的内容复制到在线工具的架构部分。该工具立即响应此错误消息:
Error: Unrecognized vocabulary: https://example.com/vocab/example-vocab. You can define this vocabulary with the 'defineVocabulary' function.
有什么方法可以在 Hyperjump 的在线模式验证器中运行这个示例吗?如果没有,是否有可以运行此示例的在线模式验证器?
有什么方法可以在 Hyperjump 的在线模式验证器中运行这个示例吗?
经过一些调整,是的。定义自定义方言的某些部分需要代码。任何需要代码的事情都无法在在线工具中完成。
您的示例创建了一个名为
minDate
的自定义关键字。为了让验证器知道如何评估该关键字,您需要在 JavaScript(或 TypeScript)中提供关键字实现。您无法在线提供代码,但如果您的关键字只是一个注释(始终验证true
),您可以将关键字所在的词汇表设置为可选,从而避免编写代码。
以下是如何使词汇成为可选的。
"$vocabulary": {
"https://json-schema.org/draft/2020-12/vocab/core": true,
"https://json-schema.org/draft/2020-12/vocab/applicator": true,
"https://json-schema.org/draft/2020-12/vocab/validation": true,
"https://example.com/vocab/example-vocab": false // <-- This changed to false
},
它也在词汇元模式中。实际上没有理由在这里使用
$vocabulary
,所以删除它即可。
// Delete these lines
"$vocabulary": {
"https://example.com/vocab/example-vocab": true
},
现在它将在在线验证器中运行。如果您还没有弄清楚,您可以使用
[+]
按钮向架构面板添加其他选项卡。第一个选项卡始终是入口点。这就是您创建的架构所在的位置。方言元架构和词汇元架构将位于您打开的“架构 2”和“架构 3”选项卡中。警告:为避免错误,请先添加元架构,然后再将架构粘贴到主架构选项卡中。
此时您将收到无效架构错误,因为您的架构无效。我想你的意思是这样的,
"departureDate": { "minDate": "2023-08-07" } // <-- The `minDate` keyword needed to be inside a schema