schema-constraints

schema-constraints

This rule ensures that schema objects define constraints restricting the size of the underlying data.

Rule Details

Defining constraints enhances the robustness and reliability of the API’s structure.

The schema-constraints rule is designed to ensure that all schema objects define constraints restricting the size of the underlying data. This includes:

Configuration

The configuration should be an object whose keys are supported schema types, and values are boolean values. At least one schema type must be specified.

Supported schema types are integer, number, string, array and object.

By default, constraints for all supported schema types are verified.

Configuration Example. The rule with the following configuration checks only array and object schemas, and does not check numbers and strings.

lint:
  rules:
    schema-constraints:
      - warning
      - array: true
        object: true

Examples

Good.

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.

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

When Not to Use It

If your API does not require specific constraints on its schema objects, you may not need to use this rule.

Compatibility

This rule is compatible with all OpenAPI 3.x versions.

← Back to Index