{
  "openapi": "3.1.0",
  "info": {
    "title": "ISO 6346 Container ID Checker API",
    "version": "1.0.0",
    "description": "Validate shipping container identification codes against the ISO 6346:2022 standard.",
    "license": {
      "name": "See repository"
    }
  },
  "servers": [
    {
      "url": "/",
      "description": "Same-origin (served from the app)"
    }
  ],
  "paths": {
    "/api/check": {
      "post": {
        "summary": "Validate a batch of container IDs",
        "description": "Accepts up to 1000 IDs (100 characters each). Input is normalized (uppercased, spaces/dashes stripped) before checking. Cross-origin requests are allowed (Access-Control-Allow-Origin: *).",
        "parameters": [
          {
            "name": "format",
            "in": "query",
            "required": false,
            "description": "Response format. Defaults to JSON. `jsonl` streams one ValidationResult per line as application/x-ndjson.",
            "schema": {
              "type": "string",
              "enum": ["json", "jsonl"],
              "default": "json"
            }
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": { "$ref": "#/components/schemas/CheckRequest" },
              "example": {
                "containerIds": ["MSCU1234561", "CSQU3054381"]
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Validation results in input order.",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/CheckResponse" },
                "example": {
                  "results": [
                    {
                      "containerId": "MSCU1234561",
                      "valid": true,
                      "errors": [],
                      "formatted": "MSCU 123456 1",
                      "expectedCheckDigit": 1
                    }
                  ]
                }
              },
              "application/x-ndjson": {
                "schema": {
                  "type": "string",
                  "description": "Newline-delimited JSON, one ValidationResult per line."
                }
              }
            }
          },
          "400": {
            "description": "Bad request — malformed body, missing key, empty or oversized array, non-string item, or oversized ID.",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/ErrorResponse" },
                "examples": {
                  "invalidJson": {
                    "value": { "error": "Invalid JSON body" }
                  },
                  "missingKey": {
                    "value": {
                      "error": "Request body must contain a \"containerIds\" array. Example: {\"containerIds\": [\"MSCU1234561\"]}"
                    }
                  },
                  "emptyArray": {
                    "value": { "error": "containerIds array must not be empty" }
                  },
                  "tooMany": {
                    "value": { "error": "Maximum 1000 container IDs per request" }
                  },
                  "nonString": {
                    "value": { "error": "All containerIds must be strings" }
                  },
                  "tooLong": {
                    "value": {
                      "error": "Each container ID must be 100 characters or fewer"
                    }
                  }
                }
              }
            }
          }
        }
      },
      "options": {
        "summary": "CORS preflight",
        "responses": {
          "204": {
            "description": "Preflight response.",
            "headers": {
              "Access-Control-Allow-Origin": {
                "schema": { "type": "string" },
                "description": "Always `*`."
              },
              "Access-Control-Allow-Methods": {
                "schema": { "type": "string" },
                "description": "`POST, OPTIONS`."
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "CheckRequest": {
        "type": "object",
        "required": ["containerIds"],
        "properties": {
          "containerIds": {
            "type": "array",
            "minItems": 1,
            "maxItems": 1000,
            "items": {
              "type": "string",
              "maxLength": 100
            }
          }
        }
      },
      "CheckResponse": {
        "type": "object",
        "required": ["results"],
        "properties": {
          "results": {
            "type": "array",
            "items": { "$ref": "#/components/schemas/ValidationResult" }
          }
        }
      },
      "ValidationResult": {
        "type": "object",
        "required": ["containerId", "valid", "errors"],
        "properties": {
          "containerId": {
            "type": "string",
            "description": "The original input, preserved verbatim."
          },
          "valid": {
            "type": "boolean"
          },
          "errors": {
            "type": "array",
            "items": { "$ref": "#/components/schemas/ValidationError" }
          },
          "formatted": {
            "type": "string",
            "description": "Canonical XXXU NNNNNN C form. Present when structure was recognized, even if the check digit failed.",
            "example": "CSQU 305438 3"
          },
          "expectedCheckDigit": {
            "type": "integer",
            "minimum": 0,
            "maximum": 9,
            "description": "The correct check digit. Present when structure was recognized."
          }
        }
      },
      "ValidationError": {
        "type": "object",
        "required": ["code", "message"],
        "properties": {
          "code": {
            "type": "string",
            "enum": [
              "empty_input",
              "invalid_length",
              "invalid_owner_code",
              "invalid_category",
              "invalid_serial",
              "invalid_check_digit_char",
              "check_digit_mismatch"
            ],
            "description": "Stable error code. Prefer this over the human-readable message for programmatic handling."
          },
          "message": {
            "type": "string"
          }
        }
      },
      "ErrorResponse": {
        "type": "object",
        "required": ["error"],
        "properties": {
          "error": {
            "type": "string"
          }
        }
      }
    }
  }
}
