Skip to content

Latest commit

 

History

History
121 lines (112 loc) · 2.01 KB

File metadata and controls

121 lines (112 loc) · 2.01 KB

🇺🇸 English | 🇷🇺 Русский | 🇨🇳 中文

OpenAPI Specification Examples

Simple String

{
  "type": "string",
  "description": "Field description",
  "minLength": 1,
  "maxLength": 100,
  "pattern": "^[A-Za-z0-9]+$",
  "format": "email" // Supported formats: email, date, date-time, uri, uuid, etc.
}

Simple String Enum:

{
  "type": "string",
  "enum": ["a", "b", "c"],
  "description": "Choose one of the allowed values",
  "default": "a"
}

Number:

{
  "type": "number",
  "description": "Numeric value",
  "minimum": 0,
  "maximum": 100,
  "exclusiveMinimum": true,
  "exclusiveMaximum": true,
  "multipleOf": 0.5,
  "format": "float" // Supported formats: float, double, int32, int64
}

Array:

{
  "type": "array",
  "description": "Array of elements",
  "items": {
    "type": "string",
    "description": "Array element"
  },
  "minItems": 1,
  "maxItems": 10,
  "uniqueItems": true
}

Object:

{
  "type": "object",
  "description": "Complex object",
  "properties": {
    "name": {
      "type": "string",
      "description": "Object name"
    },
    "value": {
      "type": "number",
      "description": "Object value"
    }
  },
  "required": ["name"],
  "additionalProperties": false,
  "minProperties": 1,
  "maxProperties": 5
}

Reference to Definition:

{
  "$ref": "#/components/schemas/User"
}

Combined Types:

{
  "oneOf": [
    { "type": "string" },
    { "type": "number" }
  ],
  "description": "Value can be either a string or a number"
}

Conditional Fields:

{
  "type": "object",
  "properties": {
    "type": {
      "type": "string",
      "enum": ["user", "admin"]
    },
    "permissions": {
      "type": "array",
      "items": {
        "type": "string"
      }
    }
  },
  "if": {
    "properties": {
      "type": { "const": "admin" }
    }
  },
  "then": {
    "required": ["permissions"]
  }
}