-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
OpenAPI: parses the 'validate' struct tag
- Loading branch information
Showing
5 changed files
with
102 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package openapi3 | ||
|
||
import ( | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
// parses the "validate" tag used by go-playground/validator and sets the schema accordingly | ||
func parseValidate(s *Schema, tag string) { | ||
for _, item := range strings.Split(tag, ",") { | ||
if strings.Contains(item, "min=") { | ||
min := strings.Split(item, "min=")[1] | ||
if s.Type == "integer" || s.Type == "number" { | ||
s.Minimum, _ = strconv.Atoi(min) | ||
} else if s.Type == "string" { | ||
s.MinLength, _ = strconv.Atoi(min) | ||
} | ||
} | ||
|
||
if strings.Contains(item, "max=") { | ||
max := strings.Split(item, "max=")[1] | ||
if s.Type == "integer" || s.Type == "number" { | ||
s.Maximum, _ = strconv.Atoi(max) | ||
} else if s.Type == "string" { | ||
s.MaxLength, _ = strconv.Atoi(max) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package openapi3 | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestParseValidate(t *testing.T) { | ||
t.Run("empty", func(t *testing.T) { | ||
s := &Schema{} | ||
parseValidate(s, "") | ||
}) | ||
|
||
t.Run("required", func(t *testing.T) { | ||
t.Skip("TODO") | ||
s := &Schema{} | ||
parseValidate(s, "required") | ||
require.Equal(t, true, s.Required) | ||
}) | ||
|
||
t.Run("min for int", func(t *testing.T) { | ||
s := &Schema{ | ||
Type: "integer", | ||
} | ||
parseValidate(s, "min=10") | ||
require.Equal(t, 10, s.Minimum) | ||
}) | ||
|
||
t.Run("max for int", func(t *testing.T) { | ||
s := &Schema{ | ||
Type: "integer", | ||
} | ||
parseValidate(s, "max=10") | ||
require.Equal(t, 10, s.Maximum) | ||
}) | ||
|
||
t.Run("min for string", func(t *testing.T) { | ||
s := &Schema{ | ||
Type: "string", | ||
} | ||
parseValidate(s, "min=10") | ||
require.Equal(t, 10, s.MinLength) | ||
}) | ||
|
||
t.Run("max for string", func(t *testing.T) { | ||
s := &Schema{ | ||
Type: "string", | ||
} | ||
parseValidate(s, "max=10") | ||
require.Equal(t, 10, s.MaxLength) | ||
}) | ||
|
||
t.Run("multiple", func(t *testing.T) { | ||
s := &Schema{ | ||
Type: "string", | ||
} | ||
parseValidate(s, "required,min=10,max=20") | ||
require.Equal(t, 10, s.MinLength) | ||
require.Equal(t, 20, s.MaxLength) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters