这怎么可能是JSON模式

这怎么可能是JSON模式

本文介绍了这怎么可能是JSON模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试验证JSON架构实际上是JSON架构,而不是实例(如我所读),该资源针对元模式进行了验证,例如:

I'm trying to validate that a JSON Schema is actually a JSON Schema, and not an instance, as I have read, resource for that is validate against meta-schema e.g:

  • Core validation meta-schema (http://json-schema.org/draft/2019-09/schema)
  • Older versions meta-schema (https://json-schema.org/draft-04/schema)

我尝试使用不同的验证库, json-schema-validator 对于Java,以及 jsonschema 对于Python来说,它具有更多的保证,但我一直在寻找有趣的东西断言这是一个有效的JSON Schema实例.

I have tried with different validation libraries, json-schema-validator for Java, and jsonschema for Python to have more assurance, but I keep on obtaining the funny assertion that this is a valid JSON Schema instance.

{
    "hey" : {
        "you" : {
            "how" : {
                "dyd" : "Very well, ty"
            }
        }
    }
}

我之所以来到这里,是因为我似乎有很大的误解或误解,因为我无法理解如何将一个清晰的JSON实例(它声明没有数据类型)验证为JSON Schema实例.

I'm coming here because it seems obvious I have some big misconception or misunderstanding, as I cannot understand how a clear JSON instance (it declares no data types) can be validated as a JSON Schema instance.

正如我在开始时所述,我想解决的最初问题是如何验证JSON模式,但是如果任何JSON有效实例太有效(当结果抛出时),如何断言呢? /p>

Initial problem I wanted to solve, as I stated on the beginning, is how to validate a JSON Schema, but if any JSON valid instance is too a valid JSON schema (as results are throwing), how to assert this?

推荐答案

简短的答案是:JSON Schema是为可扩展性设计的.这意味着只要不与已知/预期关键字冲突,它就可以添加任何种类的附加属性.

The short answer is: JSON Schema is designed for extensibility. That means it allows any kind of additional properties to be added as long as they are not conflicting with the known/expected keywords.

在您的情况下,hey属性当然不是已知的关键字,即在验证过程中只是被忽略.剩下的就是有效的JSON模式{},它允许任何类型.

In your case, the hey property is certainly not a known keyword, i.e. it is simply being ignored during validation. That leaves you with the valid JSON Schema {} which allows any type.

那么如何确保某些东西实际上是JSON模式?这取决于您要定义的范围缩小多少.

How to ensure something is actually a JSON Schema then?That depends on how much narrower you want to define the term.

  1. 您可以强制要求顶层需要定义特定的$schema版本.
  2. 您可以强制至少在顶层使用有效的type属性.
  3. 如果您知道这些JSON模式来自何处并且没有利用此可扩展性,则可以在触发验证之前操作目标Meta JSON模式并包括additionalProperties: false作为顶级属性.
  1. You could enforce that the top-level needs to define a particular $schema version.
  2. You could enforce that at least on the top-level there is a valid type attribute.
  3. If you know where those JSON Schemas are coming from and that they are not making use of this extensibility, you could manipulate your target Meta JSON Schema and include "additionalProperties": false as top-level property before triggering the validation.

这篇关于这怎么可能是JSON模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 16:59