From db4da26af93241aa8fc231e8ebb7a9fb80344ad2 Mon Sep 17 00:00:00 2001 From: mikestefanello <552328+mikestefanello@users.noreply.github.com> Date: Sat, 12 Apr 2025 10:03:47 -0400 Subject: [PATCH] Added email validation to user entities. --- ent/admin/templates/entity_types.tmpl | 34 +++++++++++++++++++++++++++ ent/runtime/runtime.go | 20 +++++++++++++--- ent/schema/user.go | 7 +++++- 3 files changed, 57 insertions(+), 4 deletions(-) create mode 100644 ent/admin/templates/entity_types.tmpl diff --git a/ent/admin/templates/entity_types.tmpl b/ent/admin/templates/entity_types.tmpl new file mode 100644 index 0000000..2fbc9a6 --- /dev/null +++ b/ent/admin/templates/entity_types.tmpl @@ -0,0 +1,34 @@ +{{/* Tell Intellij/GoLand to enable the autocompletion based on the *gen.Graph type. */}} +{{/* gotype: entgo.io/ent/entc/gen.Graph */}} + +{{ define "admin/types" }} + // Code generated by ent, DO NOT EDIT. + package admin + + {{ range $n := $.Nodes }} + type {{ $n.Name }} struct { + // Fields. + {{- range $f := $n.Fields }} + {{ fieldName $f.Name }} {{ $f.Type }} `form:"{{ $f.Name }}"` + {{- end }} + // Edges. + {{- range $e := $n.Edges }} + {{- if not $e.Inverse}} + // {{ fieldName $e.Name }} int `form:"{{ $e.Name }}"` + {{- end }} + {{- end }} + } + {{ end }} + + type EntityList struct { + Columns []string + Entities []EntityValues + HasNextPage bool + } + + type EntityValues struct { + ID int + Values []string + } + +{{ end }} \ No newline at end of file diff --git a/ent/runtime/runtime.go b/ent/runtime/runtime.go index f45a5da..1f430e4 100644 --- a/ent/runtime/runtime.go +++ b/ent/runtime/runtime.go @@ -35,7 +35,21 @@ func init() { // userDescEmail is the schema descriptor for email field. userDescEmail := userFields[1].Descriptor() // user.EmailValidator is a validator for the "email" field. It is called by the builders before save. - user.EmailValidator = userDescEmail.Validators[0].(func(string) error) + user.EmailValidator = func() func(string) error { + validators := userDescEmail.Validators + fns := [...]func(string) error{ + validators[0].(func(string) error), + validators[1].(func(string) error), + } + return func(email string) error { + for _, fn := range fns { + if err := fn(email); err != nil { + return err + } + } + return nil + } + }() // userDescPassword is the schema descriptor for password field. userDescPassword := userFields[2].Descriptor() // user.PasswordValidator is a validator for the "password" field. It is called by the builders before save. @@ -51,6 +65,6 @@ func init() { } const ( - Version = "v0.14.3" // Version of ent codegen. - Sum = "h1:wokAV/kIlH9TeklJWGGS7AYJdVckr0DloWjIcO9iIIQ=" // Sum of ent codegen. + Version = "v0.14.4" // Version of ent codegen. + Sum = "h1:/DhDraSLXIkBhyiVoJeSshr4ZYi7femzhj6/TckzZuI=" // Sum of ent codegen. ) diff --git a/ent/schema/user.go b/ent/schema/user.go index ffd80f5..13f9ff7 100644 --- a/ent/schema/user.go +++ b/ent/schema/user.go @@ -2,6 +2,7 @@ package schema import ( "context" + "net/mail" "strings" "time" @@ -25,7 +26,11 @@ func (User) Fields() []ent.Field { NotEmpty(), field.String("email"). NotEmpty(). - Unique(), + Unique(). + Validate(func(s string) error { + _, err := mail.ParseAddress(s) + return err + }), field.String("password"). Sensitive(). NotEmpty(),