Skip to content

Instantly share code, notes, and snippets.

@jdesrosiers
Created June 26, 2022 21:52
Show Gist options
  • Save jdesrosiers/0ab0715d56fc3695e786f1c43555c207 to your computer and use it in GitHub Desktop.
Save jdesrosiers/0ab0715d56fc3695e786f1c43555c207 to your computer and use it in GitHub Desktop.
Customizable refactor of draft-07 meta-schema
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "http://example.com/my-custom-draft-07/schema#",
"title": "Core schema meta-schema",
"allOf": [{ "$ref": "#/definitions/schema" }],
"$comment": "Add constraints here that should only apply to root schemas and not sub-schemas",
"definitions": {
"schema": {
"$comment": "Add constainsts here that should apply to any schema including sub-schemas",
"type": ["object", "boolean"],
"properties": {
"$id": {
"type": "string",
"format": "uri-reference"
},
"$schema": {
"type": "string",
"format": "uri"
},
"$ref": {
"type": "string",
"format": "uri-reference"
},
"$comment": {
"type": "string"
},
"title": {
"type": "string"
},
"description": {
"type": "string"
},
"default": true,
"readOnly": {
"type": "boolean",
"default": false
},
"writeOnly": {
"type": "boolean",
"default": false
},
"examples": {
"type": "array",
"items": true
},
"multipleOf": {
"type": "number",
"exclusiveMinimum": 0
},
"maximum": {
"type": "number"
},
"exclusiveMaximum": {
"type": "number"
},
"minimum": {
"type": "number"
},
"exclusiveMinimum": {
"type": "number"
},
"maxLength": { "$ref": "#/definitions/nonNegativeInteger" },
"minLength": { "$ref": "#/definitions/nonNegativeIntegerDefault0" },
"pattern": {
"type": "string",
"format": "regex"
},
"additionalItems": { "$ref": "#/definitions/schema" },
"items": {
"anyOf": [
{ "$ref": "#/definitions/schema" },
{ "$ref": "#/definitions/schemaArray" }
],
"default": true
},
"maxItems": { "$ref": "#/definitions/nonNegativeInteger" },
"minItems": { "$ref": "#/definitions/nonNegativeIntegerDefault0" },
"uniqueItems": {
"type": "boolean",
"default": false
},
"contains": { "$ref": "#/definitions/schema" },
"maxProperties": { "$ref": "#/definitions/nonNegativeInteger" },
"minProperties": { "$ref": "#/definitions/nonNegativeIntegerDefault0" },
"required": { "$ref": "#/definitions/stringArray" },
"additionalProperties": { "$ref": "#/definitions/property-schema" },
"definitions": {
"type": "object",
"additionalProperties": { "$ref": "#/definitions/schema" },
"default": {}
},
"properties": {
"type": "object",
"additionalProperties": { "$ref": "#/definitions/property-schema" },
"default": {}
},
"patternProperties": {
"type": "object",
"additionalProperties": { "$ref": "#/definitions/property-schema" },
"propertyNames": { "format": "regex" },
"default": {}
},
"dependencies": {
"type": "object",
"additionalProperties": {
"anyOf": [
{ "$ref": "#/definitions/schema" },
{ "$ref": "#/definitions/stringArray" }
]
}
},
"propertyNames": { "$ref": "#/definitions/schema" },
"const": true,
"enum": {
"type": "array",
"items": true,
"minItems": 1,
"uniqueItems": true
},
"type": {
"anyOf": [
{ "$ref": "#/definitions/simpleTypes" },
{
"type": "array",
"items": { "$ref": "#/definitions/simpleTypes" },
"minItems": 1,
"uniqueItems": true
}
]
},
"format": { "type": "string" },
"contentMediaType": { "type": "string" },
"contentEncoding": { "type": "string" },
"if": { "$ref": "#/definitions/schema" },
"then": { "$ref": "#/definitions/schema" },
"else": { "$ref": "#/definitions/schema" },
"allOf": { "$ref": "#/definitions/schemaArray" },
"anyOf": { "$ref": "#/definitions/schemaArray" },
"oneOf": { "$ref": "#/definitions/schemaArray" },
"not": { "$ref": "#/definitions/schema" }
},
"default": true
},
"property-schema": {
"allOf": [{ "$ref": "#/definitions/schema" }],
"$comment": "Add constraints here that should only apply to property sub-schemas"
},
"schemaArray": {
"type": "array",
"minItems": 1,
"items": { "$ref": "#/definitions/schema" }
},
"nonNegativeInteger": {
"type": "integer",
"minimum": 0
},
"nonNegativeIntegerDefault0": {
"allOf": [
{ "$ref": "#/definitions/nonNegativeInteger" },
{ "default": 0 }
]
},
"simpleTypes": {
"enum": [
"array",
"boolean",
"integer",
"null",
"number",
"object",
"string"
]
},
"stringArray": {
"type": "array",
"items": { "type": "string" },
"uniqueItems": true,
"default": []
}
}
}
@jdesrosiers
Copy link
Author

This is a refactored version of the Draft-07 meta-schema. It doesn't do anything more or less that the official meta-schemas, but it's structured in a way that that makes it easier to add constraints for different parts of the schema. There is a space for contraints that should apply globally, constraints that should only apply to the root schema, and constraints that should only apply to property sub-schemas. If you are taking on the task of writing a custom draft-07 meta-schema, this is a good place to start.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment