Skip to content

Commit 9f917c7

Browse files
authored
schema/fields: validate for slices builder (ent#3566)
This PR changes the way slice types are built and adds the possibility to add a custom validation function to json slice types.
1 parent a8851db commit 9f917c7

File tree

15 files changed

+894
-37
lines changed

15 files changed

+894
-37
lines changed

entc/gen/template/meta.tmpl

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,12 @@ const (
8787
{{- end }}
8888
{{- with $f.Validators }}
8989
{{- $name := $f.Validator }}
90-
{{- $type := printf "func (%s) error" $f.Type.Type }}
90+
{{- $type := $f.Type.Type.String }}
91+
{{- if $f.IsJSON }}
92+
{{- $type = $f.Type.String }}
93+
{{- end }}
9194
// {{ $name }} is a validator for the "{{ $f.Name }}" field. It is called by the builders before save.
92-
{{ $name }} {{ $type }}
95+
{{ $name }} {{ printf "func (%s) error" $type }}
9396
{{- end }}
9497
{{- end }}
9598
{{- if $.HasValueScanner }}

entc/gen/template/runtime.tmpl

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -195,12 +195,15 @@ func init() {
195195
{{- end }}
196196
{{- with $f.Validators }}
197197
{{- $name := print $pkg "." $f.Validator }}
198-
{{- $type := printf "func (%s) error" $f.Type.Type }}
198+
{{- $type := $f.Type.Type.String }}
199+
{{- if $f.IsJSON }}
200+
{{- $type = $f.Type.String }}
201+
{{- end }}
199202
// {{ $name }} is a validator for the "{{ $f.Name }}" field. It is called by the builders before save.
200203
{{- if eq $f.Validators 1 }}
201-
{{ $name }} = {{ $desc }}.Validators[0].({{ $type }})
204+
{{ $name }} = {{ $desc }}.Validators[0].({{ printf "func (%s) error" $type }})
202205
{{- else }}
203-
{{ $name }} = func() {{ $type }} {
206+
{{ $name }} = func() {{ printf "func (%s) error" $type }} {
204207
validators := {{ $desc }}.Validators
205208
fns := [...]func({{ $f.Type.Type }}) error {
206209
{{- range $j, $n := xrange $f.Validators }}

entc/gen/type.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1039,7 +1039,7 @@ func (t *Type) checkField(tf *Field, f *load.Field) (err error) {
10391039
// Enum types should be named as follows: typepkg.Field.
10401040
f.Info.Ident = fmt.Sprintf("%s.%s", t.PackageDir(), pascal(f.Name))
10411041
}
1042-
case tf.Validators > 0 && !tf.ConvertedToBasic():
1042+
case tf.Validators > 0 && !tf.ConvertedToBasic() && f.Info.Type != field.TypeJSON:
10431043
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)
10441044
case ant != nil && ant.Default != "" && (ant.DefaultExpr != "" || ant.DefaultExprs != nil):
10451045
err = fmt.Errorf("field %q cannot have both default value and default expression annotations", f.Name)
@@ -1804,6 +1804,8 @@ func (f Field) BasicType(ident string) (expr string) {
18041804
case rt.TypeEqual(nullStringType) || rt.TypeEqual(nullStringPType):
18051805
expr = fmt.Sprintf("%s.String", ident)
18061806
}
1807+
case field.TypeJSON:
1808+
expr = ident
18071809
default:
18081810
if t.Numeric() && rt.Kind >= reflect.Int && rt.Kind <= reflect.Float64 {
18091811
expr = fmt.Sprintf("%s(%s)", rt.Kind, ident)

entc/integration/json/ent/migrate/schema.go

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)