我可以将 JSON Schema 中的字段声明为“JSON Schema”吗?
Can I declare a field in JSON Schema as “JSON Schema”?
我的一个项目接受“插件”。这些插件需要提供一个特殊的 JSON 文件,其中包含一些元信息,还用 JSON Schema 描述事件对象。例如:
{
"name": "My component",
"description": "My super awesome component",
"documentation": "docs/main.md",
"maintainer": "john.doe@example.com",
"events": [{
"name": "click",
"description": "Occurs when the element is clicked.",
"data": [{
"name": "xPos",
"description": "The horizontal position of the click.",
"schema": {
"type": "integer",
"minimum": 0
}
}
]
}]
}
此元文件将根据 JSON 架构进行验证。现在我的问题是,如何验证 events[0].data[0].schema
条目的内容?在这种情况下,预期字段是一个整数,但它也可以是任何其他类型。 JSON 模式中是否定义了 "type":"schema"
或类似的东西?
(为了它的价值,我使用 ajv 作为验证器。)
您将寻找元模式! JSON 描述 JSON 模式的模式。
您可以在 http://json-schema.org/specification.html#meta-schemas
找到它们
您想像这样通过 $id 引用元模式。
{
"$ref": "http://json-schema.org/draft-07/schema#"
}
使用上面的作为你的架构,下面作为你想要失败的测试数据,你可以看到它使用 https://www.jsonschemavalidator.net
{
"type": "integer",
"minimum": 0,
"properties": ["a"],
}
但是您应该考虑,验证仅断言为 false,因为 properties
必须是一个对象。 允许未知关键字并且有效,空对象,或true
或false
,因此如果您要将properties
更改为_properties
在我的示例中,根据元模式,您的模式仍然有效。
也相关:
如果您想手动验证一个模式是否有效,ajv 可以让您轻松做到这一点!记录在 https://ajv.js.org/#api-validateschema
.validateSchema(Object schema) -> Boolean
Validates schema. This method should be used to validate schemas
rather than validate due to the inconsistency of uri format in JSON
Schema standard.
By default this method is called automatically when the schema is
added, so you rarely need to use it directly.
If schema doesn’t have $schema property, it is validated against draft
6 meta-schema (option meta should not be false).
If schema has $schema property, then the schema with this id (that
should be previously added) is used to validate passed schema.
Errors will be available at ajv.errors.
我的一个项目接受“插件”。这些插件需要提供一个特殊的 JSON 文件,其中包含一些元信息,还用 JSON Schema 描述事件对象。例如:
{
"name": "My component",
"description": "My super awesome component",
"documentation": "docs/main.md",
"maintainer": "john.doe@example.com",
"events": [{
"name": "click",
"description": "Occurs when the element is clicked.",
"data": [{
"name": "xPos",
"description": "The horizontal position of the click.",
"schema": {
"type": "integer",
"minimum": 0
}
}
]
}]
}
此元文件将根据 JSON 架构进行验证。现在我的问题是,如何验证 events[0].data[0].schema
条目的内容?在这种情况下,预期字段是一个整数,但它也可以是任何其他类型。 JSON 模式中是否定义了 "type":"schema"
或类似的东西?
(为了它的价值,我使用 ajv 作为验证器。)
您将寻找元模式! JSON 描述 JSON 模式的模式。
您可以在 http://json-schema.org/specification.html#meta-schemas
找到它们您想像这样通过 $id 引用元模式。
{
"$ref": "http://json-schema.org/draft-07/schema#"
}
使用上面的作为你的架构,下面作为你想要失败的测试数据,你可以看到它使用 https://www.jsonschemavalidator.net
{
"type": "integer",
"minimum": 0,
"properties": ["a"],
}
但是您应该考虑,验证仅断言为 false,因为 properties
必须是一个对象。 允许未知关键字并且有效,空对象,或true
或false
,因此如果您要将properties
更改为_properties
在我的示例中,根据元模式,您的模式仍然有效。
也相关:
如果您想手动验证一个模式是否有效,ajv 可以让您轻松做到这一点!记录在 https://ajv.js.org/#api-validateschema
.validateSchema(Object schema) -> Boolean
Validates schema. This method should be used to validate schemas rather than validate due to the inconsistency of uri format in JSON Schema standard.
By default this method is called automatically when the schema is added, so you rarely need to use it directly.
If schema doesn’t have $schema property, it is validated against draft 6 meta-schema (option meta should not be false).
If schema has $schema property, then the schema with this id (that should be previously added) is used to validate passed schema.
Errors will be available at ajv.errors.