Added email validation to user entities.
This commit is contained in:
parent
cabf8b9cdb
commit
db4da26af9
3 changed files with 57 additions and 4 deletions
34
ent/admin/templates/entity_types.tmpl
Normal file
34
ent/admin/templates/entity_types.tmpl
Normal file
|
|
@ -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 }}
|
||||||
|
|
@ -35,7 +35,21 @@ func init() {
|
||||||
// userDescEmail is the schema descriptor for email field.
|
// userDescEmail is the schema descriptor for email field.
|
||||||
userDescEmail := userFields[1].Descriptor()
|
userDescEmail := userFields[1].Descriptor()
|
||||||
// user.EmailValidator is a validator for the "email" field. It is called by the builders before save.
|
// 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 is the schema descriptor for password field.
|
||||||
userDescPassword := userFields[2].Descriptor()
|
userDescPassword := userFields[2].Descriptor()
|
||||||
// user.PasswordValidator is a validator for the "password" field. It is called by the builders before save.
|
// user.PasswordValidator is a validator for the "password" field. It is called by the builders before save.
|
||||||
|
|
@ -51,6 +65,6 @@ func init() {
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
Version = "v0.14.3" // Version of ent codegen.
|
Version = "v0.14.4" // Version of ent codegen.
|
||||||
Sum = "h1:wokAV/kIlH9TeklJWGGS7AYJdVckr0DloWjIcO9iIIQ=" // Sum of ent codegen.
|
Sum = "h1:/DhDraSLXIkBhyiVoJeSshr4ZYi7femzhj6/TckzZuI=" // Sum of ent codegen.
|
||||||
)
|
)
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@ package schema
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"net/mail"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
|
@ -25,7 +26,11 @@ func (User) Fields() []ent.Field {
|
||||||
NotEmpty(),
|
NotEmpty(),
|
||||||
field.String("email").
|
field.String("email").
|
||||||
NotEmpty().
|
NotEmpty().
|
||||||
Unique(),
|
Unique().
|
||||||
|
Validate(func(s string) error {
|
||||||
|
_, err := mail.ParseAddress(s)
|
||||||
|
return err
|
||||||
|
}),
|
||||||
field.String("password").
|
field.String("password").
|
||||||
Sensitive().
|
Sensitive().
|
||||||
NotEmpty(),
|
NotEmpty(),
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue