Skip to content

[REQ] [Go] Support min and max length validations #22102

@srgoni

Description

@srgoni

Is your feature request related to a problem? Please describe.

Since 7.7.0, openapi-generator supports pattern validation using Go struct tags.

However, there are other constraints on that should be validated, in particular minLength and maxLength on strings: https://www.ietf.org/archive/id/draft-bhutton-json-schema-validation-01.html#name-validation-keywords-for-str
These are currently ignored.

Describe the solution you'd like

The minLength and maxLength attributes should be translated to min= and max= tags, possibly listing them alongside a regexp= tag if a pattern is specified.

The following spec:

---
openapi: 3.0.0
info:
  title: unset
  version: "1"
paths: {}
components:
  schemas:
    Bar:
      type: object
      properties:
        Email:
          type: string
          pattern: '^\S+@\S+$'
          minLength: 3
          maxLength: 100

should produce the following struct:

type Bar struct {
        Email *string `json:"Email,omitempty" validate:"min=3,max=100,regexp=^\S+@\S+$"`
}

Describe alternatives you've considered

Inserting validation tags is currently possibly by specifying the x-go-custom-tag: validate:"..." attribute, but this requires duplicating all attributes.

Example:

    Bar:
      type: object
      properties:
        Email:
          type: string
          #pattern: '^\S+@\S+$'
          #minLength: 3
          #maxLength: 100
          x-go-custom-tag: validate:"min=6,max=64,regexp=^\S+@\S+$"

Additional context

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions