{
  "id": "http://www.jsonscript.org/schema/macro.json#",
  "$schema": "https://raw.githubusercontent.com/epoberezkin/ajv/master/lib/refs/json-schema-v5.json#",
  "description": "schema for macro definition",
  "type": "object",
  "required": [ "name", "description", "rules" ],
  "additionalProperties": false,
  "properties": {
    "name": {
      "decription": "macro name",
      "$ref": "#nonEmptyString"
    },
    "description": {
      "$ref": "#nonEmptyString"
    },
    "rules": {
      "description": "macro rules",
      "type": "array",
      "minItems": 1,
      "items": {
        "type": "object",
        "required": [ "description", "pattern", "script" ],
        "additionalProperties": false,
        "properties": {
          "description": { "$ref": "#nonEmptyString" },
          "pattern": {
            "type": "object",
            "minProperties": 1,
            "maxProperties": 1,
            "additionalProperties": {
              "description": "property itself should be a valid regular expression",
              "type": "integer"
            }
          },
          "script": {
            "type": "object",
            "additionalProperties": false,
            "patternProperties": {
              "^\\$[a-z]+$": {
                "anyOf": [
                  {
                    "description": "this string referes to the partial match in macro pattern",
                    "type": "string",
                    "pattern": "^\\$[1-9]$"
                  },
                  {
                    "description": "object with a single property that refers to the match; the value of the property is a substitution map",
                    "type": "object",
                    "minProperties": 1,
                    "maxProperties": 1,
                    "additionalProperties": false,
                    "patternProperties": {
                      "^\\$[1-9]$": {
                        "type": "object",
                        "additionalProperties": {
                          "type": "string"
                        }
                      }
                    }
                  },
                  {
                    "description": "this number referes to the value in the macro",
                    "type": "integer"
                  },
                  {
                    "description": "any valid JSONScript"
                  }
                ]
              }
            }
          }
        }
      }
    }
  },
  "definitions": {
    "nonEmptyString": {
      "id": "#nonEmptyString",
      "type": "string",
      "minLength": 1
    }
  }
}
