Skip to content

Commit

Permalink
move validations to value types
Browse files Browse the repository at this point in the history
Also fix date and date-time validation
  • Loading branch information
injeniero committed Nov 23, 2023
1 parent e56ce04 commit b664735
Show file tree
Hide file tree
Showing 15 changed files with 281 additions and 131 deletions.
197 changes: 112 additions & 85 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

0 comments on commit b664735

Please sign in to comment.