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

refactor: move validations to value types #113

Merged
merged 1 commit into from
Nov 25, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
207 changes: 109 additions & 98 deletions pkg/generators/models/models.go

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions pkg/generators/models/models_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,20 +118,20 @@ var cases = []struct {
directory: "testdata/cases/parameter_model",
},
{
name: "model with recursive definition within an allof",
name: "model with recursive definition within an allOf",
directory: "testdata/cases/allof_self_reference",
},
{
name: "allof merges required list",
name: "allOf merges required list",
directory: "testdata/cases/allof_merges_required_list",
},
// 25
{
name: "allof merges enum list",
name: "allOf merges enum list",
directory: "testdata/cases/allof_enum",
},
{
name: "allof supports intermediate array types",
name: "allOf supports intermediate array types",
directory: "testdata/cases/allof_arrays",
},
{
Expand Down
7 changes: 4 additions & 3 deletions pkg/generators/models/templates/model.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ import (
{{- end}}

validation "github.com/go-ozzo/ozzo-validation/v4"
{{- range .Imports}}
"{{.}}"
{{ end}}

{{- range $package, $alias := .Imports}}
{{$alias}} "{{$package}}"
{{- end}}
)

{{- $modelName := .Name }}
Expand Down
45 changes: 45 additions & 0 deletions pkg/generators/models/templates/value.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,50 @@
// Version: {{.SpecVersion}}
package {{ .PackageName }}

{{- if .Imports}}
import (
{{- range $package, $alias := .Imports}}
{{$alias}} "{{$package}}"
{{- end}}
)
{{- end}}

{{- if .PatternErrorMsg}}
// {{.Name}}PatternError is the error message returned for pattern validation errors on {{.Name}}
var {{.Name}}PatternError = validation.NewError("validation_{{.Name }}_pattern_invalid", "{{.PatternErrorMsg}}")
{{- end }}
{{- if .Pattern }}
// {{.Name| firstLower}}Pattern is the validation regexp for {{.Name}}
var {{.Name| firstLower}}Pattern = regexp.MustCompile(`{{ .Pattern }}`)
{{ end }}

{{ (printf "%s is a value type. %s" .Name .Description) | commentBlock }}
type {{.Name}} {{.GoType}}


// Validate implements basic validation for this model
func (v {{.Name}}) Validate() error {
{{- if .NeedsValidation }}
return validation.Validate(
{{.GoType}}(v),
{{- if .HasMin }}validation.Min({{ .GoType }}({{ .Min }})),{{ end }}
{{- if .HasMax }}validation.Max({{ .GoType }}({{ .Max }})),{{ end }}
{{- if or .HasMinLength .HasMaxLength }}validation.Length({{ .MinLength }},{{ .MaxLength }}),{{ end }}
{{- if .IsDate }}validation.Date("2006-01-02"),{{ end }}
{{- if .IsDateTime }}validation.Date(time.RFC3339),{{ end }}
{{- if .IsBase64 }}is.Base64,{{ end }}
{{- if .IsEmail }}is.EmailFormat,{{ end }}
{{- if .IsUUID }}is.UUID,{{ end }}
{{- if .IsURL }}is.URL.Error("must be a valid URL with HTTP or HTTPS scheme"),{{ end }}
{{- if .IsURI }}is.RequestURI,{{ end }}
{{- if .IsRequestURI }}is.RequestURL.Error("must be valid URI with scheme"),{{ end }}
{{- if .IsHostname }}is.Host,{{ end }}
{{- if .IsIPv4 }}is.IPv4,{{ end }}
{{- if .IsIPv6 }}is.IPv6,{{ end }}
{{- if .IsIP }}is.IP,{{ end }}
{{- if .Pattern }}validation.Match({{.Name| firstLower}}Pattern){{if .PatternErrorMsg}}.ErrorObject({{.Name}}PatternError){{end}},{{ end }}
)
{{- else }}
return nil
{{- end }}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 6 additions & 7 deletions pkg/generators/models/testdata/cases/validation/api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ components:
- cron
properties:
cron:
type: string
pattern: (@(annually|yearly|monthly|weekly|daily|hourly|reboot))|(@every (\d+(ns|us|µs|ms|s|m|h))+)|((((\d+,)+\d+|(\d+(\/|-)\d+)|\d+|\*) ?){5,7})
x-pattern-error: "must be a valid cron value"
$ref: "#/components/schemas/Cron"
pomodoro:
type: string
pattern: "^\\d{1,2}m$"
Expand All @@ -43,7 +41,7 @@ components:
minLength: 1
email:
type: string
format: email
format: "email"
date:
type: string
format: "date"
Expand All @@ -53,9 +51,6 @@ components:
base64:
type: string
format: "byte"
email:
type: string
format: "email"
uuid:
type: string
format: "uuid"
Expand All @@ -80,6 +75,10 @@ components:
ip:
type: string
format: "ip"
Cron:
type: string
pattern: (@(annually|yearly|monthly|weekly|daily|hourly|reboot))|(@every (\d+(ns|us|µs|ms|s|m|h))+)|((((\d+,)+\d+|(\d+(\/|-)\d+)|\d+|\*) ?){5,7})
x-pattern-error: "must be a valid cron value"
Gender:
type: string
enum:
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading