OpenAPITools / OpenAPITools/openapi-generator
[REQ] [Go] Support min and max length validations
Nobody has claimed this yet.
- 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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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