entc/integration: add validator to macaddr field (#971)

This commit is contained in:
Ariel Mashraki
2020-11-22 10:16:43 +02:00
committed by GitHub
parent 68f40030a6
commit d9abf3297c
9 changed files with 46 additions and 0 deletions

View File

@@ -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.

View File

@@ -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
}

View File

@@ -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
}

View File

@@ -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.

View File

@@ -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
}),
}
}

View File

@@ -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.

View File

@@ -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
}

View File

@@ -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
}

View File

@@ -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.