mirror of
https://github.com/ent/ent.git
synced 2026-05-24 09:31:56 +03:00
committed by
Ariel Mashraki
parent
c8ed4e9d7a
commit
4eefbb6af9
@@ -575,6 +575,21 @@ func (b *bytesBuilder) MaxLen(i int) *bytesBuilder {
|
||||
return b
|
||||
}
|
||||
|
||||
// Validate adds a validator for this field. Operation fails if the validation fails.
|
||||
//
|
||||
// field.Bytes("blob").
|
||||
// Validate(func(b []byte) error {
|
||||
// if len(b) % 2 == 0 {
|
||||
// return fmt.Errorf("ent/schema: blob length is even: %d", len(b))
|
||||
// }
|
||||
// return nil
|
||||
// })
|
||||
//
|
||||
func (b *bytesBuilder) Validate(fn func([]byte) error) *bytesBuilder {
|
||||
b.desc.Validators = append(b.desc.Validators, fn)
|
||||
return b
|
||||
}
|
||||
|
||||
// StorageKey sets the storage key of the field.
|
||||
// In SQL dialects is the column name and Gremlin is the property.
|
||||
func (b *bytesBuilder) StorageKey(key string) *bytesBuilder {
|
||||
|
||||
@@ -187,12 +187,19 @@ func (*Pair) Scan(interface{}) error { return nil }
|
||||
func (Pair) Value() (driver.Value, error) { return nil, nil }
|
||||
|
||||
func TestBytes(t *testing.T) {
|
||||
fd := field.Bytes("active").Default([]byte("{}")).Comment("comment").Descriptor()
|
||||
fd := field.Bytes("active").
|
||||
Default([]byte("{}")).
|
||||
Comment("comment").
|
||||
Validate(func(bytes []byte) error {
|
||||
return nil
|
||||
}).
|
||||
Descriptor()
|
||||
assert.Equal(t, "active", fd.Name)
|
||||
assert.Equal(t, field.TypeBytes, fd.Info.Type)
|
||||
assert.NotNil(t, fd.Default)
|
||||
assert.Equal(t, []byte("{}"), fd.Default)
|
||||
assert.Equal(t, "comment", fd.Comment)
|
||||
assert.Len(t, fd.Validators, 1)
|
||||
|
||||
fd = field.Bytes("ip").GoType(net.IP("127.0.0.1")).Descriptor()
|
||||
assert.NoError(t, fd.Err)
|
||||
|
||||
Reference in New Issue
Block a user