Alex Ivanov
Alex Ivanov

Reputation: 473

Does `minLength: 1` imply required in OpenAPI spec?

Consider this OAS3 spec (testMinMax3.yaml):

openapi: 3.0.1
info:
  title: OpenAPI Test
  description: Test
  license:
    name: Apache-2.0
    url: http://www.apache.org/licenses/LICENSE-2.0.html
  version: 1.0.0
servers:
- url: http://localhost:9999/v2
paths:
  /ping:
    post:
      summary: test
      description: test it
      operationId: pingOp
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/SomeObj'
        required: true
      responses:
        200:
          description: OK
          content: {}
components:
  schemas:
    SomeObj:
      type: string
      minLength: 1
      maxLength: 3

Does non-empty (aka minLength: 1) implies required even if a field is not set based on the openapi spec (1)? Or does that apply only if a value for the field is provided by the user (2)?

Upvotes: 13

Views: 9410

Answers (1)

Helen
Helen

Reputation: 97847

No.

minLength and required are separate constraints. minLength means that if a string value is provided, its length must be minLength or more.

Upvotes: 13

Related Questions