{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "title": "Noma inspection template import",
  "description": "The accepted file structure for one property inspection template and its shared comments and variables.",
  "$comment": "Before schema validation, trim outer spaces from names, titles, labels, and choices; keep body text unchanged. Also check unique keys, body placeholders, references, total counts, and valid Unicode without null characters. The server repeats validation, reviews exact company matches, and saves the whole import in one transaction.",
  "type": "object",
  "additionalProperties": false,
  "required": ["template", "comments", "variables"],
  "properties": {
    "template": {
      "$ref": "#/$defs/template"
    },
    "comments": {
      "type": "array",
      "maxItems": 20000,
      "items": { "$ref": "#/$defs/comment" },
      "description": "Define each comment once. Sections refer to its key. Unplaced comments still join the company library."
    },
    "variables": {
      "type": "array",
      "maxItems": 2000,
      "items": { "$ref": "#/$defs/variable" },
      "description": "Define each variable once. Comment bodies refer to its key. Unused variables still join the company library."
    }
  },
  "$defs": {
    "key": {
      "type": "string",
      "pattern": "^[a-z][a-z0-9_]{0,63}$",
      "not": { "pattern": "[^a-z0-9_]" },
      "maxLength": 64,
      "description": "A local key, such as roof_material. It is not a Noma record ID."
    },
    "name": {
      "type": "string",
      "minLength": 1,
      "maxLength": 255,
      "pattern": "\\S",
      "description": "A nonblank display name. Trim outer spaces before storing it."
    },
    "plainText": {
      "type": "string",
      "minLength": 1,
      "pattern": "\\S"
    },
    "template": {
      "type": "object",
      "additionalProperties": false,
      "required": ["name", "visit_kind", "areas"],
      "properties": {
        "name": { "$ref": "#/$defs/name" },
        "visit_kind": { "const": "property_inspection" },
        "areas": {
          "type": "array",
          "minItems": 1,
          "maxItems": 500,
          "items": { "$ref": "#/$defs/area" },
          "description": "Top-level areas in display order."
        }
      }
    },
    "area": {
      "type": "object",
      "additionalProperties": false,
      "required": ["name", "sections"],
      "properties": {
        "name": { "$ref": "#/$defs/name" },
        "sections": {
          "type": "array",
          "maxItems": 5000,
          "items": { "$ref": "#/$defs/section" },
          "description": "Sections in this area, in display order. File-wide validation also limits the whole file to 5,000 sections."
        }
      }
    },
    "section": {
      "type": "object",
      "additionalProperties": false,
      "required": ["name", "comments"],
      "properties": {
        "name": { "$ref": "#/$defs/name" },
        "comments": {
          "type": "array",
          "maxItems": 20000,
          "uniqueItems": true,
          "items": { "$ref": "#/$defs/key" },
          "description": "Comment keys in display order. Each key must exist in comments. File-wide validation limits the whole file to 100,000 placements."
        }
      }
    },
    "comment": {
      "type": "object",
      "additionalProperties": false,
      "required": ["key", "title", "body"],
      "properties": {
        "key": { "$ref": "#/$defs/key" },
        "title": { "$ref": "#/$defs/name" },
        "body": {
          "$ref": "#/$defs/plainText",
          "description": "Plain text with {{variable_key}} placeholders. Preserve spaces and line breaks. Every double-brace pair must belong to a valid variable placeholder. Use single braces or parentheses for plain text. File validation checks this grammar."
        },
        "default_severity": {
          "enum": [null, "info", "minor", "moderate", "major"],
          "description": "Omit this or use null to leave severity unset. These stored values stay fixed while company labels can change."
        }
      }
    },
    "variable": {
      "type": "object",
      "additionalProperties": false,
      "required": ["key", "label", "choices"],
      "properties": {
        "key": { "$ref": "#/$defs/key" },
        "label": { "$ref": "#/$defs/name" },
        "choices": {
          "type": "array",
          "minItems": 1,
          "items": { "$ref": "#/$defs/plainText" },
          "description": "Plain text choices in display order. The first choice is the default. Trim outer spaces; never parse choices for placeholders."
        }
      }
    }
  }
}
