Skip to content

Latest commit

 

History

History
121 lines (112 loc) · 2.34 KB

File metadata and controls

121 lines (112 loc) · 2.34 KB

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

Примеры спецификаций для OpenAPI

Простая строка

{
  "type": "string",
  "description": "Описание поля",
  "minLength": 1,
  "maxLength": 100,
  "pattern": "^[A-Za-z0-9]+$",
  "format": "email" // Поддерживаемые форматы: email, date, date-time, uri, uuid и др.
}

Простая строка enum:

{
  "type": "string",
  "enum": ["a", "b", "c"],
  "description": "Выберите одно из допустимых значений",
  "default": "a"
}

Число:

{
  "type": "number",
  "description": "Числовое значение",
  "minimum": 0,
  "maximum": 100,
  "exclusiveMinimum": true,
  "exclusiveMaximum": true,
  "multipleOf": 0.5,
  "format": "float" // Поддерживаемые форматы: float, double, int32, int64
}

Массив:

{
  "type": "array",
  "description": "Массив элементов",
  "items": {
    "type": "string",
    "description": "Элемент массива"
  },
  "minItems": 1,
  "maxItems": 10,
  "uniqueItems": true
}

Объект:

{
  "type": "object",
  "description": "Сложный объект",
  "properties": {
    "name": {
      "type": "string",
      "description": "Имя объекта"
    },
    "value": {
      "type": "number",
      "description": "Значение объекта"
    }
  },
  "required": ["name"],
  "additionalProperties": false,
  "minProperties": 1,
  "maxProperties": 5
}

Ссылка на определение:

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

Комбинированные типы:

{
  "oneOf": [
    { "type": "string" },
    { "type": "number" }
  ],
  "description": "Значение может быть строкой или числом"
}

Условные поля:

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