{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://howwordstravel.app/schema/word.schema.json",
  "title": "How Words Travel: one word",
  "description": "One file per word, written by scripts/build_words.py to output/<lang_code>/<slug>.json. See SCHEMA.md for how the front end uses each field.",
  "type": "object",
  "required": ["schema_version", "word", "lang_code", "lang", "slug", "path", "nodes", "edges", "tree", "cognates", "translations", "stats", "source"],
  "properties": {
    "schema_version": {"const": 1},
    "word": {"type": "string"},
    "lang_code": {"type": "string", "description": "Wiktionary language code of the head word, e.g. en, es"},
    "lang": {"type": "string"},
    "lang_es": {"type": ["string", "null"], "description": "Spanish name of the language, from the languages table's name_es column"},
    "slug": {"type": "string", "pattern": "^[a-z0-9-]+$"},
    "pos": {"type": "array", "items": {"type": "string"}},
    "gloss": {"type": ["string", "null"]},
    "etymology_text": {"type": ["string", "null"], "description": "Wiktionary's etymology, as plain text (CC BY-SA)"},
    "first_attested": {
      "oneOf": [
        {"type": "null"},
        {"type": "object", "required": ["year", "approx", "raw"],
         "properties": {"year": {"type": "integer"}, "approx": {"type": "boolean"}, "raw": {"type": "string"}}}
      ]
    },
    "path": {"type": "array", "minItems": 1, "items": {"type": "string"},
             "description": "Node ids of the main route, oldest first; the last one is the head word"},
    "nodes": {"type": "array", "items": {"$ref": "#/$defs/node"}},
    "edges": {"type": "array", "items": {"$ref": "#/$defs/edge"}},
    "tree": {"$ref": "#/$defs/treeNode"},
    "cognates": {"type": "array", "items": {"$ref": "#/$defs/looseForm"}},
    "translations": {"type": "array", "items": {"$ref": "#/$defs/looseForm"}},
    "time_range": {
      "oneOf": [
        {"type": "null"},
        {"type": "object", "required": ["start", "end"],
         "properties": {"start": {"type": "integer"}, "end": {"type": "integer"}}}
      ]
    },
    "stats": {
      "type": "object",
      "required": ["nodes", "edges", "nodes_without_place", "nodes_without_era", "truncated"],
      "properties": {
        "nodes": {"type": "integer"}, "edges": {"type": "integer"},
        "nodes_without_place": {"type": "integer"}, "nodes_without_era": {"type": "integer"},
        "truncated": {"type": "boolean"}
      }
    },
    "source": {"type": "object", "required": ["name", "license", "url"]}
  },
  "$defs": {
    "region": {
      "oneOf": [
        {"type": "null"},
        {"type": "object", "required": ["lat", "lng"],
         "properties": {"lat": {"type": "number"}, "lng": {"type": "number"}, "place": {"type": ["string", "null"]}}}
      ]
    },
    "relation": {
      "enum": ["inherited", "borrowed", "learned_borrowing", "derived", "calque", "semantic_loan",
               "phono_semantic_matching", "transliteration"]
    },
    "node": {
      "type": "object",
      "required": ["id", "lang_code", "lang", "form", "reconstructed", "role", "region", "era", "year", "year_source"],
      "properties": {
        "id": {"type": "string", "description": "<lang_code>:<form>, unique within the file"},
        "lang_code": {"type": "string"},
        "lang": {"type": "string"},
        "lang_es": {"type": ["string", "null"], "description": "Spanish name of the language"},
        "form": {"type": ["string", "null"], "description": "Word form in its own script, without the reconstruction asterisk; null when only the language is known"},
        "reconstructed": {"type": "boolean", "description": "True for proto-language forms; show with a leading *"},
        "roman": {"type": ["string", "null"], "description": "Romanization for non-Latin scripts"},
        "gloss": {"type": ["string", "null"]},
        "role": {"enum": ["head", "ancestor", "descendant"]},
        "region": {"$ref": "#/$defs/region"},
        "era": {
          "oneOf": [
            {"type": "null"},
            {"type": "object", "required": ["start", "end"],
             "properties": {"start": {"type": "integer"}, "end": {"type": ["integer", "null"]}}}
          ]
        },
        "year": {"type": ["integer", "null"], "description": "Best guess of when this form appears; negative is BCE"},
        "year_source": {"enum": ["attested_text", "attested_entry", "override", "language_era", "after_parent", "before_child", null],
                        "description": "attested_text: the head word's own etymology text; attested_entry: this node's own Wiktionary entry; override: hand-checked (attestations.json); language_era, after_parent and before_child (an undated ancestor placed at its child's year) are estimates"},
        "place_source": {"enum": ["glottolog", "override", "parent", null], "description": "Where region came from; parent = the place of the language this etymology-only variety belongs to"},
        "era_source": {"enum": ["wikidata", "glottolog", "override", null], "description": "Where era came from"}
      }
    },
    "edge": {
      "type": "object",
      "required": ["source", "target", "relation", "kind", "year"],
      "properties": {
        "source": {"type": "string", "description": "Older node id"},
        "target": {"type": "string", "description": "Newer node id"},
        "relation": {"$ref": "#/$defs/relation"},
        "kind": {"enum": ["ancestry", "descent"]},
        "year": {"type": ["integer", "null"], "description": "Same as the target node's year"}
      }
    },
    "treeNode": {
      "type": "object",
      "required": ["id", "relation", "children"],
      "properties": {
        "id": {"type": "string"},
        "relation": {"oneOf": [{"type": "null"}, {"$ref": "#/$defs/relation"}]},
        "children": {"type": "array", "items": {"$ref": "#/$defs/treeNode"}}
      }
    },
    "looseForm": {
      "type": "object",
      "required": ["lang_code", "lang", "node", "region"],
      "properties": {
        "lang_code": {"type": "string"},
        "lang": {"type": "string"},
        "lang_es": {"type": ["string", "null"]},
        "form": {"type": "string"},
        "word": {"type": "string"},
        "roman": {"type": ["string", "null"]},
        "gloss": {"type": ["string", "null"]},
        "node": {"type": ["string", "null"], "description": "Matching node id, if this form is on the map already"},
        "region": {"$ref": "#/$defs/region"}
      }
    }
  }
}
