Greco
Greco

Reputation: 172

How can this be a JSON Schema

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:

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?

Upvotes: 1

Views: 420

Answers (1)

Carsten
Carsten

Reputation: 2147

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.

Upvotes: 1

Related Questions