{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://82excel.com/schema/rules-1.json",
  "title": "82excel rules (format 1)",
  "description": "Language-neutral validation rules for spreadsheet uploads. Pure JSON: regular expressions are strings. See https://82excel.com.",
  "type": "object",
  "required": [
    "columns"
  ],
  "additionalProperties": false,
  "properties": {
    "$schema": {
      "type": "string"
    },
    "id": {
      "type": "string"
    },
    "version": {
      "type": "string"
    },
    "columns": {
      "type": "array",
      "minItems": 1,
      "items": {
        "$ref": "#/$defs/column"
      }
    },
    "columnsOrder": {
      "enum": [
        "any",
        "strict"
      ],
      "default": "any"
    },
    "extraColumns": {
      "enum": [
        "ignore",
        "warn",
        "error"
      ],
      "default": "ignore"
    },
    "rows": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "min": {
          "type": "integer",
          "minimum": 0
        },
        "max": {
          "type": "integer",
          "minimum": 0
        },
        "blank": {
          "enum": [
            "skip",
            "warn",
            "error",
            "stop"
          ],
          "default": "skip"
        },
        "totals": {
          "enum": [
            "warn",
            "ignore",
            "error"
          ],
          "default": "warn"
        }
      }
    },
    "rowRules": {
      "type": "array",
      "items": {
        "$ref": "#/$defs/rowRule"
      }
    },
    "formulaInjection": {
      "enum": [
        "warn",
        "error",
        "escape",
        "off"
      ],
      "default": "warn"
    },
    "pii": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "detect": {
          "type": "array",
          "items": {
            "enum": [
              "rrn",
              "card",
              "phone",
              "email"
            ]
          },
          "default": [
            "rrn"
          ],
          "description": "Duplicates are rejected."
        },
        "mask": {
          "type": "boolean",
          "default": true
        }
      }
    },
    "normalize": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "trim": {
          "type": "boolean",
          "default": true
        },
        "invisible": {
          "type": "boolean",
          "default": true
        },
        "fullwidthDigits": {
          "type": "boolean",
          "default": true
        }
      }
    },
    "sheet": {
      "type": [
        "string",
        "integer"
      ],
      "minimum": 0
    },
    "headerRow": {
      "type": "integer",
      "minimum": 1
    },
    "maxSize": {
      "type": "number",
      "exclusiveMinimum": 0
    },
    "accept": {
      "type": "array",
      "items": {
        "enum": [
          "xlsx",
          "xls",
          "csv"
        ]
      }
    },
    "locale": {
      "type": "string"
    },
    "readRows": {
      "type": "boolean"
    },
    "maxCells": {
      "type": "number",
      "exclusiveMinimum": 0
    },
    "delimiter": {
      "enum": [
        ",",
        ";",
        "\t"
      ]
    },
    "maxUncompressedSize": {
      "type": "number",
      "exclusiveMinimum": 0
    },
    "previewRows": {
      "type": "integer",
      "minimum": 0
    },
    "failOn": {
      "type": "array",
      "items": {
        "type": "string"
      }
    },
    "telemetry": {
      "type": "boolean"
    }
  },
  "$defs": {
    "column": {
      "type": "object",
      "required": [
        "key"
      ],
      "additionalProperties": false,
      "properties": {
        "key": {
          "type": "string",
          "minLength": 1,
          "pattern": "^[^$]",
          "description": "Row-object key. Must not start with \"$\"; __proto__, constructor and prototype are reserved.",
          "not": {
            "enum": [
              "__proto__",
              "constructor",
              "prototype"
            ]
          }
        },
        "header": {
          "type": "string",
          "minLength": 1,
          "description": "Header text matched in the file (loose: whitespace, case and NFKC width are ignored). Defaults to key."
        },
        "aliases": {
          "type": "array",
          "items": {
            "type": "string"
          },
          "default": []
        },
        "required": {
          "type": "boolean",
          "default": true
        },
        "nullable": {
          "type": "boolean",
          "default": true
        },
        "type": {
          "enum": [
            "text",
            "number",
            "integer",
            "date",
            "boolean",
            "email",
            "kr.phone",
            "kr.brn",
            "kr.crn",
            "kr.rrn",
            "kr.zip",
            "kr.amount"
          ],
          "default": "text"
        },
        "unique": {
          "type": "boolean",
          "default": false
        },
        "enum": {
          "type": "array",
          "minItems": 1,
          "items": {
            "type": [
              "string",
              "number"
            ]
          }
        },
        "pattern": {
          "type": "string",
          "format": "regex",
          "description": "Regular expression source, compiled with the JavaScript \"u\" (unicode) flag; tested against the normalized value."
        },
        "min": {
          "type": "number"
        },
        "max": {
          "type": "number"
        },
        "minLength": {
          "type": "integer",
          "minimum": 0
        },
        "maxLength": {
          "type": "integer",
          "minimum": 0,
          "default": 32767
        },
        "date": {
          "type": "object",
          "additionalProperties": false,
          "properties": {
            "formats": {
              "type": "array",
              "items": {
                "type": "string",
                "pattern": "^(YYYY|YY|MM|M|DD|D|[./ 년월일-])+$"
              },
              "minItems": 1,
              "description": "Tokens YYYY YY MM M DD D (M/D: 1-2 digits, MM/DD: exactly 2); literals . - / space 년 월 일. Exactly one year, month and day token.",
              "default": [
                "YYYY-M-D",
                "YYYY.M.D",
                "YYYY. M. D.",
                "YYYY.M.D.",
                "YYYY/M/D",
                "YY-M-D",
                "YYYYMMDD",
                "YYYY년 M월 D일",
                "YYYY년M월D일"
              ]
            },
            "pivot": {
              "type": "integer",
              "minimum": 0,
              "maximum": 99,
              "default": 50
            }
          }
        },
        "phone": {
          "type": "object",
          "additionalProperties": false,
          "properties": {
            "kind": {
              "enum": [
                "mobile",
                "landline",
                "any"
              ],
              "default": "any"
            }
          }
        },
        "boolean": {
          "type": "object",
          "additionalProperties": false,
          "properties": {
            "true": {
              "type": "array",
              "items": {
                "type": "string"
              },
              "minItems": 1,
              "default": [
                "Y",
                "O",
                "예",
                "TRUE"
              ]
            },
            "false": {
              "type": "array",
              "items": {
                "type": "string"
              },
              "minItems": 1,
              "default": [
                "N",
                "X",
                "아니오",
                "FALSE"
              ]
            }
          }
        },
        "rrn": {
          "type": "object",
          "additionalProperties": false,
          "properties": {
            "checksum": {
              "enum": [
                "warn",
                "off"
              ],
              "default": "warn"
            }
          }
        },
        "crn": {
          "type": "object",
          "additionalProperties": false,
          "properties": {
            "checksum": {
              "enum": [
                "warn",
                "off"
              ],
              "default": "off"
            }
          }
        },
        "severity": {
          "enum": [
            "error",
            "warning"
          ],
          "default": "error"
        },
        "pii": {
          "enum": [
            "allow",
            "forbid"
          ]
        },
        "message": {
          "type": "string"
        }
      }
    },
    "rowRule": {
      "oneOf": [
        {
          "type": "object",
          "required": [
            "when"
          ],
          "additionalProperties": false,
          "anyOf": [
            {
              "required": [
                "require"
              ]
            },
            {
              "required": [
                "forbid"
              ]
            }
          ],
          "properties": {
            "when": {
              "type": "object",
              "required": [
                "col"
              ],
              "additionalProperties": false,
              "minProperties": 2,
              "maxProperties": 2,
              "properties": {
                "col": {
                  "type": "string",
                  "description": "A column key from columns[]"
                },
                "eq": {
                  "type": [
                    "string",
                    "number",
                    "boolean"
                  ]
                },
                "neq": {
                  "type": [
                    "string",
                    "number",
                    "boolean"
                  ]
                },
                "in": {
                  "type": "array",
                  "items": {
                    "type": [
                      "string",
                      "number",
                      "boolean"
                    ]
                  }
                },
                "empty": {
                  "const": true
                },
                "notEmpty": {
                  "const": true
                }
              }
            },
            "require": {
              "type": "array",
              "minItems": 1,
              "items": {
                "type": "string",
                "description": "A column key from columns[]"
              }
            },
            "forbid": {
              "type": "array",
              "minItems": 1,
              "items": {
                "type": "string",
                "description": "A column key from columns[]"
              }
            },
            "severity": {
              "enum": [
                "error",
                "warning"
              ]
            },
            "message": {
              "type": "string"
            }
          }
        },
        {
          "type": "object",
          "required": [
            "compare"
          ],
          "additionalProperties": false,
          "properties": {
            "compare": {
              "type": "array",
              "prefixItems": [
                {
                  "type": "string",
                  "description": "A column key from columns[]"
                },
                {
                  "enum": [
                    "<",
                    "<=",
                    "=",
                    "!=",
                    ">=",
                    ">"
                  ]
                },
                {
                  "type": "string",
                  "description": "A column key from columns[]"
                }
              ],
              "minItems": 3,
              "maxItems": 3
            },
            "severity": {
              "enum": [
                "error",
                "warning"
              ]
            },
            "message": {
              "type": "string"
            }
          }
        },
        {
          "type": "object",
          "required": [
            "uniqueWith"
          ],
          "additionalProperties": false,
          "properties": {
            "uniqueWith": {
              "type": "array",
              "minItems": 1,
              "items": {
                "type": "string",
                "description": "A column key from columns[]"
              }
            },
            "severity": {
              "enum": [
                "error",
                "warning"
              ]
            },
            "message": {
              "type": "string"
            }
          }
        }
      ]
    }
  }
}
