Search code examples
jsonjsonschemajson-schema-validator

A type for JSON schema in a JSON schema?


I would like to have a schema, which describes a value, which is a JSON schema itself, for example

{
  "title": "Schema modification event",
  "description": "Describes and event of modification of another (arbitrary) JSON schema",
  "type": "object",
  "properties": {
    "field_name": {
      "type": "string",
      "format": "json-pointer",
      "description": "Location where change occurred"
    },
    "field_schema": {
      "$ref": "https://json-schema.org/draft-07/schema",
      "description": "New schema of the location"
    }
  },
  "required": [
    "field_name",
    "field_schema"
  ]
}

Solution

  • There isn't a type for a schema, but what you've done with field_schema should work to ensure that field_schema is a JSON Schema.

    You wouldn't be able to process field_name using the schema in field_schema within the same operation, though. You'd have to do it in several steps:

    1. Validate the payload (what the schema above should validate)
    2. Extract your new data from wherever field_name is pointing and the schema at field_schema.
    3. Validate the new data with the field_schema schema.