{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "title": "nd-yaml（schema_version 1.x）",
  "description": "ND YAML交換の YAML の書式を検査する JSON スキーマ。プロジェクトによらない。クラス・フィールド・型・列挙値は検査しない（yamlexchange validate --metamodel がメタモデル情報ファイルで検査する）。$file の先（分割先・サイドカー）の中身は検査しない。",
  "type": "object",
  "required": [
    "schema_version",
    "root"
  ],
  "additionalProperties": false,
  "properties": {
    "schema_version": {
      "type": "string",
      "pattern": "^1\\.(0|[1-9][0-9]*)$"
    },
    "root": {
      "description": "モデルノード。エクスポートは常に1件で書く。取り込み・検証の入力では配列で複数書ける。一部だけエクスポートしたときの目次ファイルは、分割したファイルへの $file だけの配列。",
      "anyOf": [
        {
          "$ref": "#/$defs/node"
        },
        {
          "type": "array",
          "minItems": 1,
          "items": {
            "$ref": "#/$defs/node"
          }
        },
        {
          "type": "array",
          "minItems": 1,
          "items": {
            "$ref": "#/$defs/file"
          }
        }
      ]
    }
  },
  "$defs": {
    "file": {
      "description": "別ファイルを指す。所有フィールドの要素では分割先ファイル（nd-yaml）、リッチテキストの値ではサイドカー（HTML）。パスは書いてあるファイルのフォルダからの相対パス。",
      "type": "object",
      "required": [
        "$file"
      ],
      "additionalProperties": false,
      "properties": {
        "$file": {
          "type": "string",
          "minLength": 1
        }
      }
    },
    "item": {
      "description": "値1つ。スカラー、所有の子（モデルノード）、参照のマッピング、別ファイルを指す $file のいずれか。",
      "anyOf": [
        {
          "type": [
            "string",
            "number",
            "boolean"
          ]
        },
        {
          "$ref": "#/$defs/node"
        },
        {
          "$ref": "#/$defs/reference"
        },
        {
          "$ref": "#/$defs/file"
        }
      ]
    },
    "node": {
      "description": "モデルノード。name と class を必ず書く。そのほかのキーはフィールドで、値の形だけを検査する。予約キーと同じ名前のフィールドは f: を付けて書く。",
      "type": "object",
      "required": [
        "name",
        "class"
      ],
      "properties": {
        "name": {
          "type": "string",
          "description": "モデル名。名前の無いモデルは ''。"
        },
        "class": {
          "type": "string",
          "minLength": 1,
          "description": "クラスの完全修飾名。すべてのノードに書く。"
        },
        "id": {
          "type": "string",
          "minLength": 1,
          "description": "既存モデルのハンドル（短縮ID）・完全GUID、または新規要素の仮ID（new-1 など）。新規要素では省いてよい。"
        }
      },
      "propertyNames": {
        "description": "予約キーと同じ名前のフィールドは f: を付けて書く。",
        "not": {
          "enum": [
            "schema_version",
            "root",
            "rel",
            "path",
            "type",
            "value",
            "$file"
          ]
        }
      },
      "additionalProperties": {
        "$ref": "#/$defs/value"
      }
    },
    "reference": {
      "description": "参照の要素。照合は id だけで行う。参照先の名前は行末の # コメントで、データではない。",
      "type": "object",
      "minProperties": 1,
      "additionalProperties": false,
      "properties": {
        "id": {
          "type": "string",
          "description": "参照先のハンドル（短縮ID）・完全GUID、またはファイル内の新規要素のローカルID（new-…）。"
        },
        "path": {
          "type": "string",
          "description": "範囲外の参照先までのパス（読み手向けの補助）。"
        },
        "name": {
          "type": "string",
          "description": "id を知らないときだけ使う取り込み用の書き方。対象範囲内で一意なら解決される。"
        },
        "rel": {
          "$ref": "#/$defs/rel"
        }
      }
    },
    "rel": {
      "description": "関連そのもの。関連元の側の参照に書く。name・class・id と、値のあるフィールドを書く。",
      "type": "object",
      "properties": {
        "name": {
          "type": "string"
        },
        "class": {
          "type": "string"
        },
        "id": {
          "type": "string"
        }
      },
      "propertyNames": {
        "description": "予約キーと同じ名前のフィールドは f: を付けて書く。",
        "not": {
          "enum": [
            "schema_version",
            "root",
            "rel",
            "path",
            "type",
            "value",
            "$file"
          ]
        }
      },
      "additionalProperties": {
        "$ref": "#/$defs/value"
      }
    },
    "value": {
      "description": "フィールドの値。値1つ、またはそのリスト（多値・所有の子・参照）。",
      "anyOf": [
        {
          "$ref": "#/$defs/item"
        },
        {
          "type": "array",
          "items": {
            "$ref": "#/$defs/item"
          }
        }
      ]
    }
  }
}
