{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://circuitgenome.com/schemas/cges-core-1.0.schema.json",
  "title": "CGES Core Contract 1.0",
  "description": "Canonical interoperability schema for CGES-JSON payloads. Uniqueness, referential integrity, per-board extension consistency, derived-count equality, and deprecated metadata alias handling are checked by the canonical semantic rule set in conformance/cges-core-1.0.rules.md.",
  "type": "object",
  "required": [
    "schema_version",
    "identity",
    "metadata",
    "components",
    "nets"
  ],
  "properties": {
    "schema_version": {
      "type": "string",
      "const": "1.0"
    },
    "identity": {
      "$ref": "#/$defs/identity"
    },
    "metadata": {
      "$ref": "#/$defs/metadata"
    },
    "components": {
      "type": "array",
      "items": {
        "$ref": "#/$defs/component"
      }
    },
    "nets": {
      "type": "array",
      "items": {
        "$ref": "#/$defs/net"
      }
    },
    "test_integration": {
      "$ref": "#/$defs/testIntegration"
    },
    "analysis": {
      "$ref": "#/$defs/analysis"
    },
    "additional_boards": {
      "type": "array",
      "items": {
        "$ref": "#/$defs/additionalBoard"
      }
    }
  },
  "$defs": {
    "identity": {
      "type": "object",
      "required": [
        "board_pn",
        "board_rev",
        "board_type"
      ],
      "properties": {
        "board_pn": {
          "type": "string"
        },
        "board_rev": {
          "type": "string",
          "description": "Authoritative board identity revision token."
        },
        "board_type": {
          "type": "string",
          "minLength": 1
        }
      },
      "additionalProperties": true
    },
    "metadata": {
      "type": "object",
      "required": [
        "core_contract_version",
        "profile_id",
        "project_name",
        "schematic_revision",
        "source_tool"
      ],
      "properties": {
        "core_contract_version": {
          "type": "string",
          "const": "1.0"
        },
        "profile_id": {
          "type": "string",
          "const": "CGES-JSON-1.0"
        },
        "project_name": {
          "type": "string"
        },
        "source_tool": {
          "type": "string"
        },
        "schematic_revision": {
          "type": "string",
          "description": "Schematic or source-document revision used to produce the export. It may equal identity.board_rev when no separate schematic revision exists."
        },
        "revision": {
          "type": "string",
          "description": "Deprecated legacy alias for metadata.schematic_revision. If present, semantic validation requires it to equal metadata.schematic_revision."
        },
        "extensions": {
          "type": "array",
          "items": {
            "type": "string"
          },
          "uniqueItems": true
        },
        "analysis_context": {
          "type": "object",
          "properties": {
            "epsa_board": {
              "$ref": "cges-epsa-board-0.1.schema.json"
            }
          },
          "additionalProperties": true
        },
        "capability_notes": {
          "type": "array",
          "items": {
            "type": "string"
          }
        }
      },
      "additionalProperties": true
    },
    "component": {
      "type": "object",
      "required": [
        "refdes",
        "value",
        "footprint",
        "role",
        "function",
        "pins"
      ],
      "properties": {
        "refdes": {
          "type": "string",
          "minLength": 1
        },
        "value": {
          "type": "string"
        },
        "footprint": {
          "type": "string"
        },
        "role": {
          "type": "string",
          "minLength": 1,
          "description": "Required semantic role. Placeholder values such as 'unspecified' are structurally valid but low quality."
        },
        "function": {
          "type": "string",
          "minLength": 1,
          "description": "Required semantic function. Placeholder values such as 'unspecified' are structurally valid but low quality."
        },
        "pins": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/pin"
          }
        },
        "analysis_modules": {
          "$ref": "#/$defs/analysisModules"
        }
      },
      "additionalProperties": true
    },
    "analysisModules": {
      "type": "object",
      "properties": {
        "epsa": {
          "$ref": "cges-epsa-component-0.1.schema.json"
        },
        "fmea": {
          "$ref": "cges-fmea-0.1.schema.json"
        },
        "wcca": {
          "$ref": "cges-wcca-0.1.schema.json"
        }
      },
      "additionalProperties": true
    },
    "pin": {
      "type": "object",
      "required": [
        "pin_number",
        "net"
      ],
      "properties": {
        "pin_number": {
          "type": "string"
        },
        "net": {
          "type": "string"
        }
      },
      "additionalProperties": true
    },
    "net": {
      "type": "object",
      "required": [
        "name",
        "connections"
      ],
      "properties": {
        "name": {
          "type": "string"
        },
        "connections": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/netConnection"
          }
        }
      },
      "additionalProperties": true
    },
    "netConnection": {
      "type": "object",
      "required": [
        "refdes",
        "pin"
      ],
      "properties": {
        "refdes": {
          "type": "string"
        },
        "pin": {
          "type": "string"
        }
      },
      "additionalProperties": true
    },
    "testIntegration": {
      "type": "object",
      "required": [
        "coverage_status",
        "auto_generated",
        "test_points"
      ],
      "properties": {
        "coverage_status": {
          "type": "string",
          "enum": [
            "none",
            "partial",
            "complete",
            "unknown"
          ]
        },
        "auto_generated": {
          "type": "boolean"
        },
        "test_points": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/testPoint"
          }
        }
      },
      "additionalProperties": true
    },
    "testPoint": {
      "type": "object",
      "required": [
        "refdes",
        "net",
        "measurement_type"
      ],
      "properties": {
        "refdes": {
          "type": "string",
          "pattern": "^[Tt][Pp]"
        },
        "net": {
          "type": "string"
        },
        "measurement_type": {
          "type": "string",
          "enum": [
            "voltage",
            "current",
            "resistance",
            "frequency",
            "logic",
            "temperature",
            "other",
            "unknown"
          ]
        }
      },
      "additionalProperties": true
    },
    "analysis": {
      "type": "object",
      "required": [
        "component_count",
        "net_count"
      ],
      "properties": {
        "component_count": {
          "type": "integer",
          "minimum": 0
        },
        "net_count": {
          "type": "integer",
          "minimum": 0
        },
        "enabled_modules": {
          "type": "array",
          "items": {
            "type": "string",
            "enum": [
              "epsa",
              "fmea",
              "wcca"
            ]
          },
          "uniqueItems": true
        },
        "module_notes": {
          "type": "array",
          "items": {
            "type": "string"
          }
        }
      },
      "additionalProperties": true
    },
    "additionalBoard": {
      "type": "object",
      "required": [
        "identity",
        "metadata",
        "components",
        "nets"
      ],
      "properties": {
        "identity": {
          "$ref": "#/$defs/identity"
        },
        "metadata": {
          "$ref": "#/$defs/metadata"
        },
        "components": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/component"
          }
        },
        "nets": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/net"
          }
        },
        "test_integration": {
          "$ref": "#/$defs/testIntegration"
        },
        "analysis": {
          "$ref": "#/$defs/analysis"
        },
        "drawing": {
          "type": "object",
          "properties": {
            "refdes": {
              "type": "string"
            },
            "dimensions": {
              "type": "string"
            }
          },
          "additionalProperties": true
        }
      },
      "additionalProperties": true
    }
  },
  "allOf": [
    {
      "if": {
        "properties": {
          "metadata": {
            "properties": {
              "analysis_context": {
                "required": [
                  "epsa_board"
                ]
              }
            }
          }
        }
      },
      "then": {
        "properties": {
          "metadata": {
            "required": [
              "extensions"
            ],
            "properties": {
              "extensions": {
                "contains": {
                  "const": "CGES-EPSA-BOARD-0.1"
                }
              }
            }
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "components": {
            "contains": {
              "type": "object",
              "required": [
                "analysis_modules"
              ],
              "properties": {
                "analysis_modules": {
                  "type": "object",
                  "required": [
                    "epsa"
                  ]
                }
              }
            }
          }
        }
      },
      "then": {
        "properties": {
          "metadata": {
            "required": [
              "extensions"
            ],
            "properties": {
              "extensions": {
                "contains": {
                  "const": "CGES-EPSA-COMPONENT-0.1"
                }
              }
            }
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "components": {
            "contains": {
              "type": "object",
              "required": [
                "analysis_modules"
              ],
              "properties": {
                "analysis_modules": {
                  "type": "object",
                  "required": [
                    "fmea"
                  ]
                }
              }
            }
          }
        }
      },
      "then": {
        "properties": {
          "metadata": {
            "required": [
              "extensions"
            ],
            "properties": {
              "extensions": {
                "contains": {
                  "const": "CGES-FMEA-0.1"
                }
              }
            }
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "components": {
            "contains": {
              "type": "object",
              "required": [
                "analysis_modules"
              ],
              "properties": {
                "analysis_modules": {
                  "type": "object",
                  "required": [
                    "wcca"
                  ]
                }
              }
            }
          }
        }
      },
      "then": {
        "properties": {
          "metadata": {
            "required": [
              "extensions"
            ],
            "properties": {
              "extensions": {
                "contains": {
                  "const": "CGES-WCCA-0.1"
                }
              }
            }
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "metadata": {
            "required": [
              "extensions"
            ],
            "properties": {
              "extensions": {
                "contains": {
                  "const": "CGES-EPSA-BOARD-0.1"
                }
              }
            }
          }
        }
      },
      "then": {
        "properties": {
          "metadata": {
            "required": [
              "analysis_context"
            ],
            "properties": {
              "analysis_context": {
                "required": [
                  "epsa_board"
                ]
              }
            }
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "metadata": {
            "required": [
              "extensions"
            ],
            "properties": {
              "extensions": {
                "contains": {
                  "const": "CGES-EPSA-COMPONENT-0.1"
                }
              }
            }
          }
        }
      },
      "then": {
        "properties": {
          "components": {
            "contains": {
              "type": "object",
              "required": [
                "analysis_modules"
              ],
              "properties": {
                "analysis_modules": {
                  "type": "object",
                  "required": [
                    "epsa"
                  ]
                }
              }
            }
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "metadata": {
            "required": [
              "extensions"
            ],
            "properties": {
              "extensions": {
                "contains": {
                  "const": "CGES-FMEA-0.1"
                }
              }
            }
          }
        }
      },
      "then": {
        "properties": {
          "components": {
            "contains": {
              "type": "object",
              "required": [
                "analysis_modules"
              ],
              "properties": {
                "analysis_modules": {
                  "type": "object",
                  "required": [
                    "fmea"
                  ]
                }
              }
            }
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "metadata": {
            "required": [
              "extensions"
            ],
            "properties": {
              "extensions": {
                "contains": {
                  "const": "CGES-WCCA-0.1"
                }
              }
            }
          }
        }
      },
      "then": {
        "properties": {
          "components": {
            "contains": {
              "type": "object",
              "required": [
                "analysis_modules"
              ],
              "properties": {
                "analysis_modules": {
                  "type": "object",
                  "required": [
                    "wcca"
                  ]
                }
              }
            }
          }
        }
      }
    }
  ]
}

