Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(net/goai): add min, max, length, min-length, max-length and between support for OpenAPIv3 #3914

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions net/goai/goai_shema.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package goai

import (
"reflect"
"strings"

"github.com/gogf/gf/v2/container/gmap"
"github.com/gogf/gf/v2/container/gset"
Expand Down Expand Up @@ -204,6 +205,61 @@ func (oai *OpenApiV3) structToSchema(object interface{}) (*Schema, error) {
if validationRuleSet.Contains(validationRuleKeyForRequired) {
schema.Required = append(schema.Required, key)
}

// Extract validation rules to schema. like min, max, length
validationRules := ref.Value.ValidationRules
// remove custom error message
if idx := strings.Index(ref.Value.ValidationRules, "#"); idx != -1 {
gqcn marked this conversation as resolved.
Show resolved Hide resolved
validationRules = ref.Value.ValidationRules[:idx]
}
lstRules := gstr.Split(validationRules, "|")
for _, rule := range lstRules {
if strings.HasPrefix(rule, "max") {
gqcn marked this conversation as resolved.
Show resolved Hide resolved
if ref.Value.Type == "integer" || ref.Value.Type == "number" {
f := gconv.Float64(rule[4:])
ref.Value.Max = &f
}
}

if strings.HasPrefix(rule, "min") {
if ref.Value.Type == "integer" || ref.Value.Type == "number" {
f := gconv.Float64(rule[4:])
ref.Value.Min = &f
}
}

if strings.HasPrefix(rule, "length") {
lengthRule := gstr.Split(rule[7:], ",")
if len(lengthRule) == 2 {
minlength := gconv.Uint64(lengthRule[0])
ref.Value.MinLength = minlength
maxlength := gconv.Uint64(lengthRule[1])
ref.Value.MaxLength = &maxlength
}
}

if strings.HasPrefix(rule, "min-length") {
minlength := gconv.Uint64(rule[11:])
ref.Value.MinLength = minlength
}

if strings.HasPrefix(rule, "max-length") {
maxlength := gconv.Uint64(rule[11:])
ref.Value.MaxLength = &maxlength
}

if strings.HasPrefix(rule, "between") {
if ref.Value.Type == "integer" || ref.Value.Type == "number" {
lengthRule := gstr.Split(rule[8:], ",")
if len(lengthRule) == 2 {
minimum := gconv.Float64(lengthRule[0])
ref.Value.Min = &minimum
maximum := gconv.Float64(lengthRule[1])
ref.Value.Max = &maximum
}
}
}
}
}
if !isValidParameterName(key) {
ignoreProperties = append(ignoreProperties, key)
Expand Down
Loading