Skip to content

Commit

Permalink
schema/fields: validate for slices builder (ent#3566)
Browse files Browse the repository at this point in the history
This PR changes the way slice types are built and adds the possibility to add a custom validation function to json slice types.
  • Loading branch information
masseelch authored May 30, 2023
1 parent a8851db commit 9f917c7
Show file tree
Hide file tree
Showing 15 changed files with 894 additions and 37 deletions.
7 changes: 5 additions & 2 deletions entc/gen/template/meta.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,12 @@ const (
{{- end }}
{{- with $f.Validators }}
{{- $name := $f.Validator }}
{{- $type := printf "func (%s) error" $f.Type.Type }}
{{- $type := $f.Type.Type.String }}
{{- if $f.IsJSON }}
{{- $type = $f.Type.String }}
{{- end }}
// {{ $name }} is a validator for the "{{ $f.Name }}" field. It is called by the builders before save.
{{ $name }} {{ $type }}
{{ $name }} {{ printf "func (%s) error" $type }}
{{- end }}
{{- end }}
{{- if $.HasValueScanner }}
Expand Down
9 changes: 6 additions & 3 deletions entc/gen/template/runtime.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -195,12 +195,15 @@ func init() {
{{- end }}
{{- with $f.Validators }}
{{- $name := print $pkg "." $f.Validator }}
{{- $type := printf "func (%s) error" $f.Type.Type }}
{{- $type := $f.Type.Type.String }}
{{- if $f.IsJSON }}
{{- $type = $f.Type.String }}
{{- end }}
// {{ $name }} is a validator for the "{{ $f.Name }}" field. It is called by the builders before save.
{{- if eq $f.Validators 1 }}
{{ $name }} = {{ $desc }}.Validators[0].({{ $type }})
{{ $name }} = {{ $desc }}.Validators[0].({{ printf "func (%s) error" $type }})
{{- else }}
{{ $name }} = func() {{ $type }} {
{{ $name }} = func() {{ printf "func (%s) error" $type }} {
validators := {{ $desc }}.Validators
fns := [...]func({{ $f.Type.Type }}) error {
{{- range $j, $n := xrange $f.Validators }}
Expand Down
4 changes: 3 additions & 1 deletion entc/gen/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -1039,7 +1039,7 @@ func (t *Type) checkField(tf *Field, f *load.Field) (err error) {
// Enum types should be named as follows: typepkg.Field.
f.Info.Ident = fmt.Sprintf("%s.%s", t.PackageDir(), pascal(f.Name))
}
case tf.Validators > 0 && !tf.ConvertedToBasic():
case tf.Validators > 0 && !tf.ConvertedToBasic() && f.Info.Type != field.TypeJSON:
err = fmt.Errorf("GoType %q for field %q must be converted to the basic %q type for validators", tf.Type, f.Name, tf.Type.Type)
case ant != nil && ant.Default != "" && (ant.DefaultExpr != "" || ant.DefaultExprs != nil):
err = fmt.Errorf("field %q cannot have both default value and default expression annotations", f.Name)
Expand Down Expand Up @@ -1804,6 +1804,8 @@ func (f Field) BasicType(ident string) (expr string) {
case rt.TypeEqual(nullStringType) || rt.TypeEqual(nullStringPType):
expr = fmt.Sprintf("%s.String", ident)
}
case field.TypeJSON:
expr = ident
default:
if t.Numeric() && rt.Kind >= reflect.Int && rt.Kind <= reflect.Float64 {
expr = fmt.Sprintf("%s(%s)", rt.Kind, ident)
Expand Down
3 changes: 3 additions & 0 deletions entc/integration/json/ent/migrate/schema.go

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

Loading

0 comments on commit 9f917c7

Please sign in to comment.