OpenAPITools / OpenAPITools/openapi-generator

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

Open
#22,102 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Enhancement: Feature
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by locating the Go generator's handling of schema string constraints and the tests or generated fixtures for validation tags. Use the supplied OpenAPI YAML as input; done means the generated Email field includes min=3,max=100 alongside regexp=^\S+@\S+$.

Written by the indexing model from the issue text.

Assessment

Tech stack
go, openapi
Domain
api, tooling
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.