How can this be a JSON Schema

浪尽此生 提交于 2021-01-05 12:57:24

问题


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)

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"
            }
        }
    }
}

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.

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?


回答1:


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.

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.

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

  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.


来源:https://stackoverflow.com/questions/60152271/how-can-this-be-a-json-schema

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!