mirror of
https://github.com/ent/ent.git
synced 2026-05-22 09:31:45 +03:00
entc/gen: convert GoType to basic type for validators (#568)
Fixed #567
This commit is contained in:
File diff suppressed because one or more lines are too long
@@ -48,7 +48,8 @@ func ({{ $receiver }} *{{ $builder }}) Save(ctx context.Context) (*{{ $.Name }},
|
||||
{{- end }}
|
||||
{{- with or $f.Validators $f.IsEnum }}
|
||||
if v, ok := {{ $mutation }}.{{ $f.MutationGet }}(); ok {
|
||||
if err := {{ $.Package }}.{{ $f.Validator }}(v); err != nil {
|
||||
{{- $basic := $f.BasicType "v" }}
|
||||
if err := {{ $.Package }}.{{ $f.Validator }}({{ $basic }}); err != nil {
|
||||
return nil, &ValidationError{Name: "{{ $f.Name }}", err: fmt.Errorf("{{ $pkg }}: validator failed for field \"{{ $f.Name }}\": %w", err)}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -242,7 +242,8 @@ func ({{ $receiver }} *{{ $onebuilder }}) ExecX(ctx context.Context) {
|
||||
{{ end -}}
|
||||
{{ with and (or $f.Validators $f.IsEnum) (not $f.Immutable) -}}
|
||||
if v, ok := {{ $mutation }}.{{ $f.MutationGet }}(); ok {
|
||||
if err := {{ $.Package }}.{{ $f.Validator }}(v); err != nil {
|
||||
{{- $basic := $f.BasicType "v" }}
|
||||
if err := {{ $.Package }}.{{ $f.Validator }}({{ $basic }}); err != nil {
|
||||
return {{ $zero }}, &ValidationError{Name: "{{ $f.Name }}", err: fmt.Errorf("{{ $pkg }}: validator failed for field \"{{ $f.Name }}\": %w", err)}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -72,7 +72,7 @@ const (
|
||||
{{- end }}
|
||||
{{- with $f.Validators }}
|
||||
{{- $name := $f.Validator }}
|
||||
{{- $type := printf "func (%s) error" $f.Type }}
|
||||
{{- $type := printf "func (%s) error" $f.Type.Type }}
|
||||
// {{ $name }} is a validator for the "{{ $f.Name }}" field. It is called by the builders before save.
|
||||
{{ $name }} {{ $type }}
|
||||
{{- end }}
|
||||
|
||||
@@ -155,7 +155,7 @@ func init() {
|
||||
{{- end }}
|
||||
{{- with $f.Validators }}
|
||||
{{- $name := print $pkg "." $f.Validator }}
|
||||
{{- $type := printf "func (%s) error" $f.Type }}
|
||||
{{- $type := printf "func (%s) error" $f.Type.Type }}
|
||||
// {{ $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 }})
|
||||
@@ -164,7 +164,7 @@ func init() {
|
||||
validators := {{ $desc }}.Validators
|
||||
fns := [...]func({{ $f.Type }}) error {
|
||||
{{- range $j, $n := xrange $f.Validators }}
|
||||
validators[{{ $j }}].(func({{ $f.Type }}) error),
|
||||
validators[{{ $j }}].(func({{ $f.Type.Type }}) error),
|
||||
{{- end }}
|
||||
}
|
||||
return func({{ $f.BuilderField }} {{ $f.Type }}) error {
|
||||
|
||||
@@ -162,9 +162,6 @@ func NewType(c *Config, schema *load.Schema) (*Type, error) {
|
||||
return nil, err
|
||||
}
|
||||
for _, f := range schema.Fields {
|
||||
if err := typ.checkField(f); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
tf := &Field{
|
||||
def: f,
|
||||
Name: f.Name,
|
||||
@@ -180,6 +177,9 @@ func NewType(c *Config, schema *load.Schema) (*Type, error) {
|
||||
Validators: f.Validators,
|
||||
UserDefined: true,
|
||||
}
|
||||
if err := typ.checkField(tf, f); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// User defined id field.
|
||||
if tf.Name == typ.ID.Name {
|
||||
typ.ID = tf
|
||||
@@ -556,7 +556,7 @@ func (t *Type) check() error {
|
||||
}
|
||||
|
||||
// checkField checks the schema field.
|
||||
func (t *Type) checkField(f *load.Field) (err error) {
|
||||
func (t *Type) checkField(tf *Field, f *load.Field) (err error) {
|
||||
switch {
|
||||
case f.Name == "":
|
||||
err = fmt.Errorf("field name cannot be empty")
|
||||
@@ -575,6 +575,8 @@ func (t *Type) checkField(f *load.Field) (err error) {
|
||||
// Enum types should be named as follows: typepkg.Field.
|
||||
f.Info.Ident = fmt.Sprintf("%s.%s", t.Package(), pascal(f.Name))
|
||||
}
|
||||
case tf.Validators > 0 && !tf.ConvertedToBasic():
|
||||
err = fmt.Errorf("GoType %q for field %q must be converted to basic Go type for validators", tf.Type, f.Name)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -165,6 +165,10 @@ var ForeignKeys = []string{
|
||||
var (
|
||||
// ValidateOptionalInt32Validator is a validator for the "validate_optional_int32" field. It is called by the builders before save.
|
||||
ValidateOptionalInt32Validator func(int32) error
|
||||
// NdirValidator is a validator for the "ndir" field. It is called by the builders before save.
|
||||
NdirValidator func(string) error
|
||||
// LinkValidator is a validator for the "link" field. It is called by the builders before save.
|
||||
LinkValidator func(string) error
|
||||
)
|
||||
|
||||
// State defines the type for the state enum field.
|
||||
|
||||
@@ -564,6 +564,16 @@ func (ftc *FieldTypeCreate) Save(ctx context.Context) (*FieldType, error) {
|
||||
return nil, &ValidationError{Name: "state", err: fmt.Errorf("ent: validator failed for field \"state\": %w", err)}
|
||||
}
|
||||
}
|
||||
if v, ok := ftc.mutation.Ndir(); ok {
|
||||
if err := fieldtype.NdirValidator(string(v)); err != nil {
|
||||
return nil, &ValidationError{Name: "ndir", err: fmt.Errorf("ent: validator failed for field \"ndir\": %w", err)}
|
||||
}
|
||||
}
|
||||
if v, ok := ftc.mutation.Link(); ok {
|
||||
if err := fieldtype.LinkValidator(v.String()); err != nil {
|
||||
return nil, &ValidationError{Name: "link", err: fmt.Errorf("ent: validator failed for field \"link\": %w", err)}
|
||||
}
|
||||
}
|
||||
var (
|
||||
err error
|
||||
node *FieldType
|
||||
|
||||
@@ -995,6 +995,16 @@ func (ftu *FieldTypeUpdate) Save(ctx context.Context) (int, error) {
|
||||
return 0, &ValidationError{Name: "state", err: fmt.Errorf("ent: validator failed for field \"state\": %w", err)}
|
||||
}
|
||||
}
|
||||
if v, ok := ftu.mutation.Ndir(); ok {
|
||||
if err := fieldtype.NdirValidator(string(v)); err != nil {
|
||||
return 0, &ValidationError{Name: "ndir", err: fmt.Errorf("ent: validator failed for field \"ndir\": %w", err)}
|
||||
}
|
||||
}
|
||||
if v, ok := ftu.mutation.Link(); ok {
|
||||
if err := fieldtype.LinkValidator(v.String()); err != nil {
|
||||
return 0, &ValidationError{Name: "link", err: fmt.Errorf("ent: validator failed for field \"link\": %w", err)}
|
||||
}
|
||||
}
|
||||
var (
|
||||
err error
|
||||
affected int
|
||||
@@ -2785,6 +2795,16 @@ func (ftuo *FieldTypeUpdateOne) Save(ctx context.Context) (*FieldType, error) {
|
||||
return nil, &ValidationError{Name: "state", err: fmt.Errorf("ent: validator failed for field \"state\": %w", err)}
|
||||
}
|
||||
}
|
||||
if v, ok := ftuo.mutation.Ndir(); ok {
|
||||
if err := fieldtype.NdirValidator(string(v)); err != nil {
|
||||
return nil, &ValidationError{Name: "ndir", err: fmt.Errorf("ent: validator failed for field \"ndir\": %w", err)}
|
||||
}
|
||||
}
|
||||
if v, ok := ftuo.mutation.Link(); ok {
|
||||
if err := fieldtype.LinkValidator(v.String()); err != nil {
|
||||
return nil, &ValidationError{Name: "link", err: fmt.Errorf("ent: validator failed for field \"link\": %w", err)}
|
||||
}
|
||||
}
|
||||
var (
|
||||
err error
|
||||
node *FieldType
|
||||
|
||||
@@ -50,6 +50,14 @@ func init() {
|
||||
fieldtypeDescValidateOptionalInt32 := fieldtypeFields[15].Descriptor()
|
||||
// fieldtype.ValidateOptionalInt32Validator is a validator for the "validate_optional_int32" field. It is called by the builders before save.
|
||||
fieldtype.ValidateOptionalInt32Validator = fieldtypeDescValidateOptionalInt32.Validators[0].(func(int32) error)
|
||||
// fieldtypeDescNdir is the schema descriptor for ndir field.
|
||||
fieldtypeDescNdir := fieldtypeFields[27].Descriptor()
|
||||
// fieldtype.NdirValidator is a validator for the "ndir" field. It is called by the builders before save.
|
||||
fieldtype.NdirValidator = fieldtypeDescNdir.Validators[0].(func(string) error)
|
||||
// fieldtypeDescLink is the schema descriptor for link field.
|
||||
fieldtypeDescLink := fieldtypeFields[30].Descriptor()
|
||||
// fieldtype.LinkValidator is a validator for the "link" field. It is called by the builders before save.
|
||||
fieldtype.LinkValidator = fieldtypeDescLink.Validators[0].(func(string) error)
|
||||
fileFields := schema.File{}.Fields()
|
||||
_ = fileFields
|
||||
// fileDescSize is the schema descriptor for size field.
|
||||
|
||||
@@ -72,6 +72,7 @@ func (FieldType) Fields() []ent.Field {
|
||||
field.String("ndir").
|
||||
Optional().
|
||||
Nillable().
|
||||
NotEmpty().
|
||||
GoType(http.Dir("ndir")),
|
||||
field.String("str").
|
||||
Optional().
|
||||
@@ -82,6 +83,7 @@ func (FieldType) Fields() []ent.Field {
|
||||
GoType(&sql.NullString{}),
|
||||
field.String("link").
|
||||
Optional().
|
||||
NotEmpty().
|
||||
GoType(&Link{}),
|
||||
field.String("null_link").
|
||||
Optional().
|
||||
|
||||
@@ -108,6 +108,10 @@ const (
|
||||
var (
|
||||
// ValidateOptionalInt32Validator is a validator for the "validate_optional_int32" field. It is called by the builders before save.
|
||||
ValidateOptionalInt32Validator func(int32) error
|
||||
// NdirValidator is a validator for the "ndir" field. It is called by the builders before save.
|
||||
NdirValidator func(string) error
|
||||
// LinkValidator is a validator for the "link" field. It is called by the builders before save.
|
||||
LinkValidator func(string) error
|
||||
)
|
||||
|
||||
// State defines the type for the state enum field.
|
||||
|
||||
@@ -565,6 +565,16 @@ func (ftc *FieldTypeCreate) Save(ctx context.Context) (*FieldType, error) {
|
||||
return nil, &ValidationError{Name: "state", err: fmt.Errorf("ent: validator failed for field \"state\": %w", err)}
|
||||
}
|
||||
}
|
||||
if v, ok := ftc.mutation.Ndir(); ok {
|
||||
if err := fieldtype.NdirValidator(string(v)); err != nil {
|
||||
return nil, &ValidationError{Name: "ndir", err: fmt.Errorf("ent: validator failed for field \"ndir\": %w", err)}
|
||||
}
|
||||
}
|
||||
if v, ok := ftc.mutation.Link(); ok {
|
||||
if err := fieldtype.LinkValidator(v.String()); err != nil {
|
||||
return nil, &ValidationError{Name: "link", err: fmt.Errorf("ent: validator failed for field \"link\": %w", err)}
|
||||
}
|
||||
}
|
||||
var (
|
||||
err error
|
||||
node *FieldType
|
||||
|
||||
@@ -997,6 +997,16 @@ func (ftu *FieldTypeUpdate) Save(ctx context.Context) (int, error) {
|
||||
return 0, &ValidationError{Name: "state", err: fmt.Errorf("ent: validator failed for field \"state\": %w", err)}
|
||||
}
|
||||
}
|
||||
if v, ok := ftu.mutation.Ndir(); ok {
|
||||
if err := fieldtype.NdirValidator(string(v)); err != nil {
|
||||
return 0, &ValidationError{Name: "ndir", err: fmt.Errorf("ent: validator failed for field \"ndir\": %w", err)}
|
||||
}
|
||||
}
|
||||
if v, ok := ftu.mutation.Link(); ok {
|
||||
if err := fieldtype.LinkValidator(v.String()); err != nil {
|
||||
return 0, &ValidationError{Name: "link", err: fmt.Errorf("ent: validator failed for field \"link\": %w", err)}
|
||||
}
|
||||
}
|
||||
var (
|
||||
err error
|
||||
affected int
|
||||
@@ -2378,6 +2388,16 @@ func (ftuo *FieldTypeUpdateOne) Save(ctx context.Context) (*FieldType, error) {
|
||||
return nil, &ValidationError{Name: "state", err: fmt.Errorf("ent: validator failed for field \"state\": %w", err)}
|
||||
}
|
||||
}
|
||||
if v, ok := ftuo.mutation.Ndir(); ok {
|
||||
if err := fieldtype.NdirValidator(string(v)); err != nil {
|
||||
return nil, &ValidationError{Name: "ndir", err: fmt.Errorf("ent: validator failed for field \"ndir\": %w", err)}
|
||||
}
|
||||
}
|
||||
if v, ok := ftuo.mutation.Link(); ok {
|
||||
if err := fieldtype.LinkValidator(v.String()); err != nil {
|
||||
return nil, &ValidationError{Name: "link", err: fmt.Errorf("ent: validator failed for field \"link\": %w", err)}
|
||||
}
|
||||
}
|
||||
var (
|
||||
err error
|
||||
node *FieldType
|
||||
|
||||
@@ -50,6 +50,14 @@ func init() {
|
||||
fieldtypeDescValidateOptionalInt32 := fieldtypeFields[15].Descriptor()
|
||||
// fieldtype.ValidateOptionalInt32Validator is a validator for the "validate_optional_int32" field. It is called by the builders before save.
|
||||
fieldtype.ValidateOptionalInt32Validator = fieldtypeDescValidateOptionalInt32.Validators[0].(func(int32) error)
|
||||
// fieldtypeDescNdir is the schema descriptor for ndir field.
|
||||
fieldtypeDescNdir := fieldtypeFields[27].Descriptor()
|
||||
// fieldtype.NdirValidator is a validator for the "ndir" field. It is called by the builders before save.
|
||||
fieldtype.NdirValidator = fieldtypeDescNdir.Validators[0].(func(string) error)
|
||||
// fieldtypeDescLink is the schema descriptor for link field.
|
||||
fieldtypeDescLink := fieldtypeFields[30].Descriptor()
|
||||
// fieldtype.LinkValidator is a validator for the "link" field. It is called by the builders before save.
|
||||
fieldtype.LinkValidator = fieldtypeDescLink.Validators[0].(func(string) error)
|
||||
fileFields := schema.File{}.Fields()
|
||||
_ = fileFields
|
||||
// fileDescSize is the schema descriptor for size field.
|
||||
|
||||
Reference in New Issue
Block a user