# schema-constraints

Source: https://apigenie.pl/docs/linter/openapi/rules/schema-constraints

A rule of the OpenAPI linter, which reads OpenAPI 3.0, 3.1 and 3.2 documents. Since 0.0.1. In the rulesets: Recommended at warning, Strict at warning, All at warning.

This rule checks that every schema bounds the size of its values: a minimum and a maximum for
integers and numbers, a length, format, pattern or enum for strings, a maximum size for arrays,
a maximum number of properties or `additionalProperties: false` for objects.

## Rule details

A schema without bounds accepts a 2 GB string in a name and an array of a million items in a
request: the server that stores them is the first to find out. Bounds in the document are
checked by validators and mocks, and generated clients and servers pick the right types.

The rule reports, by type:

- `Integer schema does not define constraints restricting its range` and `Numeric schema does not
  define constraints restricting its range`: no `minimum` or `exclusiveMinimum`, or no `maximum`
  or `exclusiveMaximum`.
- `String schema does not define constraints restricting its length`: no `maxLength`, `format`,
  `pattern` or `enum`.
- `Array schema does not define constraints restricting its size`: no `maxItems`.
- `Object schema does not define constraints restricting its size`: no `maxProperties` and no
  `additionalProperties: false`.

## Configuration

An object whose properties are the types to check, `integer`, `number`, `string`, `array` and
`object`, each `true` or `false`. At least one must be `true`. Every type is checked by default.

**Configuration example**. Arrays and objects only.

```yaml
commands:
  lint:
    rules:
      schema-constraints:
        - warning
        - array: true
          object: true
```

## Examples

**Good**. Every value is bounded.

```yaml
components:
  schemas:
    User:
      type: object
      properties:
        username:
          type: string
          maxLength: 20
          pattern: "^[a-z0-9_-]{3,16}$"
        roles:
          type: array
          items:
            type: string
          maxItems: 5
        age:
          type: integer
          minimum: 18
          maximum: 100
      maxProperties: 3
```

**Bad**. A username of any length, any number of roles, any age.

```yaml
components:
  schemas:
    User:
      type: object
      properties:
        username:
          type: string
        roles:
          type: array
          items:
            type: string
        age:
          type: integer
```
