1

A project of mine accepts “plugins”. These plugins need to provide a special JSON file which contains some meta information and also describes event objects with JSON Schema. For example:

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

This meta file will be validated against a JSON Schema. Now my question is, how could I validate the contents of the events[0].data[0].schema entry? In this case, the expected field is an integer, but it could be any other type, too. Is there a "type":"schema" or something similar defined in JSON Schema?

(For what it’s worth, I’m using ajv as validator.)

lxg
  • 12,375
  • 12
  • 51
  • 73

1 Answers1

1

You'll be looking for the meta schema! The JSON Schema that describes a JSON Schema.

You can find them at http://json-schema.org/specification.html#meta-schemas

You want to reference the meta schema by it's $id like so.

{
  "$ref": "http://json-schema.org/draft-07/schema#"
}

Using the above as your schema, and the below as your test data you want to fail, you can see it working using https://www.jsonschemavalidator.net

{
  "type": "integer",
  "minimum": 0,
  "properties": ["a"],
}

You should consider however, that validation only asserts false because properties must be an object. Unknown keywords are allowed and valid, as is an empty object, or true or false, so if you were to change properties to _properties in my example, your schema would still be valid according to the meta schema.


Also relevant:

If you wanted to manually validate a schema is a valid schema, ajv allows you to do this easily! Documented at 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.

Relequestual
  • 11,631
  • 6
  • 47
  • 83
  • Thanks, but this means I cannot validate the `schema` nodes as part of the main schema … I must validate them as `"type":"object"` and later validate all known `schema` nodes separetely. Right? – lxg Apr 24 '19 at 07:48
  • If you're creating an overall schema for your structure (which includes a schema), you can reference the meta schema using $ref for where you would expect to see a schema. Am I understanding your situation correctly? You want to create a schema where part of it IS a JSON Schema? – Relequestual Apr 24 '19 at 08:24
  • Yes, a part of my schema is a schema. See my example above. – lxg Apr 24 '19 at 09:45
  • Ah, yes … `$ref` is the solution! – lxg Apr 24 '19 at 18:37