From d9abf3297c123e7ccea2e72e0b947bda3759ecd5 Mon Sep 17 00:00:00 2001 From: Ariel Mashraki <7413593+a8m@users.noreply.github.com> Date: Sun, 22 Nov 2020 10:16:43 +0200 Subject: [PATCH] entc/integration: add validator to macaddr field (#971) --- entc/integration/ent/fieldtype/fieldtype.go | 2 ++ entc/integration/ent/fieldtype_create.go | 5 +++++ entc/integration/ent/fieldtype_update.go | 10 ++++++++++ entc/integration/ent/runtime.go | 4 ++++ entc/integration/ent/schema/fieldtype.go | 4 ++++ entc/integration/gremlin/ent/fieldtype/fieldtype.go | 2 ++ entc/integration/gremlin/ent/fieldtype_create.go | 5 +++++ entc/integration/gremlin/ent/fieldtype_update.go | 10 ++++++++++ entc/integration/gremlin/ent/runtime.go | 4 ++++ 9 files changed, 46 insertions(+) diff --git a/entc/integration/ent/fieldtype/fieldtype.go b/entc/integration/ent/fieldtype/fieldtype.go index cc6a84bcb..8a3ea3448 100644 --- a/entc/integration/ent/fieldtype/fieldtype.go +++ b/entc/integration/ent/fieldtype/fieldtype.go @@ -192,6 +192,8 @@ var ( NdirValidator func(string) error // LinkValidator is a validator for the "link" field. It is called by the builders before save. LinkValidator func(string) error + // MACValidator is a validator for the "mac" field. It is called by the builders before save. + MACValidator func(string) error ) // State defines the type for the state enum field. diff --git a/entc/integration/ent/fieldtype_create.go b/entc/integration/ent/fieldtype_create.go index da92838aa..fd355437e 100644 --- a/entc/integration/ent/fieldtype_create.go +++ b/entc/integration/ent/fieldtype_create.go @@ -656,6 +656,11 @@ func (ftc *FieldTypeCreate) check() error { return &ValidationError{Name: "role", err: fmt.Errorf("ent: validator failed for field \"role\": %w", err)} } } + if v, ok := ftc.mutation.MAC(); ok { + if err := fieldtype.MACValidator(v.String()); err != nil { + return &ValidationError{Name: "mac", err: fmt.Errorf("ent: validator failed for field \"mac\": %w", err)} + } + } return nil } diff --git a/entc/integration/ent/fieldtype_update.go b/entc/integration/ent/fieldtype_update.go index c1bf2aa6a..d65548b41 100644 --- a/entc/integration/ent/fieldtype_update.go +++ b/entc/integration/ent/fieldtype_update.go @@ -1093,6 +1093,11 @@ func (ftu *FieldTypeUpdate) check() error { return &ValidationError{Name: "role", err: fmt.Errorf("ent: validator failed for field \"role\": %w", err)} } } + if v, ok := ftu.mutation.MAC(); ok { + if err := fieldtype.MACValidator(v.String()); err != nil { + return &ValidationError{Name: "mac", err: fmt.Errorf("ent: validator failed for field \"mac\": %w", err)} + } + } return nil } @@ -2955,6 +2960,11 @@ func (ftuo *FieldTypeUpdateOne) check() error { return &ValidationError{Name: "role", err: fmt.Errorf("ent: validator failed for field \"role\": %w", err)} } } + if v, ok := ftuo.mutation.MAC(); ok { + if err := fieldtype.MACValidator(v.String()); err != nil { + return &ValidationError{Name: "mac", err: fmt.Errorf("ent: validator failed for field \"mac\": %w", err)} + } + } return nil } diff --git a/entc/integration/ent/runtime.go b/entc/integration/ent/runtime.go index c253059d0..c2f6f51ac 100644 --- a/entc/integration/ent/runtime.go +++ b/entc/integration/ent/runtime.go @@ -59,6 +59,10 @@ func init() { 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) + // fieldtypeDescMAC is the schema descriptor for mac field. + fieldtypeDescMAC := fieldtypeFields[45].Descriptor() + // fieldtype.MACValidator is a validator for the "mac" field. It is called by the builders before save. + fieldtype.MACValidator = fieldtypeDescMAC.Validators[0].(func(string) error) fileFields := schema.File{}.Fields() _ = fileFields // fileDescSize is the schema descriptor for size field. diff --git a/entc/integration/ent/schema/fieldtype.go b/entc/integration/ent/schema/fieldtype.go index c3d994dac..55e9895ac 100644 --- a/entc/integration/ent/schema/fieldtype.go +++ b/entc/integration/ent/schema/fieldtype.go @@ -135,6 +135,10 @@ func (FieldType) Fields() []ent.Field { GoType(&MAC{}). SchemaType(map[string]string{ dialect.Postgres: "macaddr", + }). + Validate(func(s string) error { + _, err := net.ParseMAC(s) + return err }), } } diff --git a/entc/integration/gremlin/ent/fieldtype/fieldtype.go b/entc/integration/gremlin/ent/fieldtype/fieldtype.go index de82f1e22..9f09534c8 100644 --- a/entc/integration/gremlin/ent/fieldtype/fieldtype.go +++ b/entc/integration/gremlin/ent/fieldtype/fieldtype.go @@ -118,6 +118,8 @@ var ( NdirValidator func(string) error // LinkValidator is a validator for the "link" field. It is called by the builders before save. LinkValidator func(string) error + // MACValidator is a validator for the "mac" field. It is called by the builders before save. + MACValidator func(string) error ) // State defines the type for the state enum field. diff --git a/entc/integration/gremlin/ent/fieldtype_create.go b/entc/integration/gremlin/ent/fieldtype_create.go index 9d1f9e82e..7bb90d99f 100644 --- a/entc/integration/gremlin/ent/fieldtype_create.go +++ b/entc/integration/gremlin/ent/fieldtype_create.go @@ -657,6 +657,11 @@ func (ftc *FieldTypeCreate) check() error { return &ValidationError{Name: "role", err: fmt.Errorf("ent: validator failed for field \"role\": %w", err)} } } + if v, ok := ftc.mutation.MAC(); ok { + if err := fieldtype.MACValidator(v.String()); err != nil { + return &ValidationError{Name: "mac", err: fmt.Errorf("ent: validator failed for field \"mac\": %w", err)} + } + } return nil } diff --git a/entc/integration/gremlin/ent/fieldtype_update.go b/entc/integration/gremlin/ent/fieldtype_update.go index f8995ee9c..cae1fa8b2 100644 --- a/entc/integration/gremlin/ent/fieldtype_update.go +++ b/entc/integration/gremlin/ent/fieldtype_update.go @@ -1095,6 +1095,11 @@ func (ftu *FieldTypeUpdate) check() error { return &ValidationError{Name: "role", err: fmt.Errorf("ent: validator failed for field \"role\": %w", err)} } } + if v, ok := ftu.mutation.MAC(); ok { + if err := fieldtype.MACValidator(v.String()); err != nil { + return &ValidationError{Name: "mac", err: fmt.Errorf("ent: validator failed for field \"mac\": %w", err)} + } + } return nil } @@ -2537,6 +2542,11 @@ func (ftuo *FieldTypeUpdateOne) check() error { return &ValidationError{Name: "role", err: fmt.Errorf("ent: validator failed for field \"role\": %w", err)} } } + if v, ok := ftuo.mutation.MAC(); ok { + if err := fieldtype.MACValidator(v.String()); err != nil { + return &ValidationError{Name: "mac", err: fmt.Errorf("ent: validator failed for field \"mac\": %w", err)} + } + } return nil } diff --git a/entc/integration/gremlin/ent/runtime.go b/entc/integration/gremlin/ent/runtime.go index 9bcb0dac6..f2f8f350e 100644 --- a/entc/integration/gremlin/ent/runtime.go +++ b/entc/integration/gremlin/ent/runtime.go @@ -59,6 +59,10 @@ func init() { 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) + // fieldtypeDescMAC is the schema descriptor for mac field. + fieldtypeDescMAC := fieldtypeFields[45].Descriptor() + // fieldtype.MACValidator is a validator for the "mac" field. It is called by the builders before save. + fieldtype.MACValidator = fieldtypeDescMAC.Validators[0].(func(string) error) fileFields := schema.File{}.Fields() _ = fileFields // fileDescSize is the schema descriptor for size field.