schema/field: add MaxLen built-in validator to bytes fields (#1863)

* added MaxLen built-in validator to `[]byte`

* typo

* move test to type_test.go

* Update doc/md/schema-fields.md

Co-authored-by: Ariel Mashraki <7413593+a8m@users.noreply.github.com>

Co-authored-by: Ariel Mashraki <7413593+a8m@users.noreply.github.com>
This commit is contained in:
Amit Shani
2021-08-24 10:23:43 +03:00
committed by GitHub
parent d84ac191b1
commit ad792fe79e
29 changed files with 649 additions and 13 deletions

View File

@@ -21,6 +21,10 @@ func init() {
userDescName := userFields[2].Descriptor()
// user.NameValidator is a validator for the "name" field. It is called by the builders before save.
user.NameValidator = userDescName.Validators[0].(func(string) error)
// userDescBlob is the schema descriptor for blob field.
userDescBlob := userFields[7].Descriptor()
// user.BlobValidator is a validator for the "blob" field. It is called by the builders before save.
user.BlobValidator = userDescBlob.Validators[0].(func([]byte) error)
// userDescWorkplace is the schema descriptor for workplace field.
userDescWorkplace := userFields[10].Descriptor()
// user.WorkplaceValidator is a validator for the "workplace" field. It is called by the builders before save.

View File

@@ -108,6 +108,8 @@ func ValidColumn(column string) bool {
var (
// NameValidator is a validator for the "name" field. It is called by the builders before save.
NameValidator func(string) error
// BlobValidator is a validator for the "blob" field. It is called by the builders before save.
BlobValidator func([]byte) error
// WorkplaceValidator is a validator for the "workplace" field. It is called by the builders before save.
WorkplaceValidator func(string) error
)

View File

@@ -294,6 +294,11 @@ func (uc *UserCreate) check() error {
if _, ok := uc.mutation.Nickname(); !ok {
return &ValidationError{Name: "nickname", err: errors.New(`entv1: missing required field "nickname"`)}
}
if v, ok := uc.mutation.Blob(); ok {
if err := user.BlobValidator(v); err != nil {
return &ValidationError{Name: "blob", err: fmt.Errorf(`entv1: validator failed for field "blob": %w`, err)}
}
}
if v, ok := uc.mutation.State(); ok {
if err := user.StateValidator(v); err != nil {
return &ValidationError{Name: "state", err: fmt.Errorf(`entv1: validator failed for field "state": %w`, err)}

View File

@@ -371,6 +371,11 @@ func (uu *UserUpdate) check() error {
return &ValidationError{Name: "name", err: fmt.Errorf("entv1: validator failed for field \"name\": %w", err)}
}
}
if v, ok := uu.mutation.Blob(); ok {
if err := user.BlobValidator(v); err != nil {
return &ValidationError{Name: "blob", err: fmt.Errorf("entv1: validator failed for field \"blob\": %w", err)}
}
}
if v, ok := uu.mutation.State(); ok {
if err := user.StateValidator(v); err != nil {
return &ValidationError{Name: "state", err: fmt.Errorf("entv1: validator failed for field \"state\": %w", err)}
@@ -1046,6 +1051,11 @@ func (uuo *UserUpdateOne) check() error {
return &ValidationError{Name: "name", err: fmt.Errorf("entv1: validator failed for field \"name\": %w", err)}
}
}
if v, ok := uuo.mutation.Blob(); ok {
if err := user.BlobValidator(v); err != nil {
return &ValidationError{Name: "blob", err: fmt.Errorf("entv1: validator failed for field \"blob\": %w", err)}
}
}
if v, ok := uuo.mutation.State(); ok {
if err := user.StateValidator(v); err != nil {
return &ValidationError{Name: "state", err: fmt.Errorf("entv1: validator failed for field \"state\": %w", err)}

View File

@@ -42,6 +42,10 @@ func init() {
userDescTitle := userFields[7].Descriptor()
// user.DefaultTitle holds the default value on creation for the title field.
user.DefaultTitle = userDescTitle.Default.(string)
// userDescBlob is the schema descriptor for blob field.
userDescBlob := userFields[9].Descriptor()
// user.BlobValidator is a validator for the "blob" field. It is called by the builders before save.
user.BlobValidator = userDescBlob.Validators[0].(func([]byte) error)
// userDescCreatedAt is the schema descriptor for created_at field.
userDescCreatedAt := userFields[13].Descriptor()
// user.DefaultCreatedAt holds the default value on creation for the created_at field.

View File

@@ -123,6 +123,8 @@ var (
DefaultBuffer func() []byte
// DefaultTitle holds the default value on creation for the "title" field.
DefaultTitle string
// BlobValidator is a validator for the "blob" field. It is called by the builders before save.
BlobValidator func([]byte) error
// DefaultCreatedAt holds the default value on creation for the "created_at" field.
DefaultCreatedAt func() time.Time
)

View File

@@ -381,6 +381,11 @@ func (uc *UserCreate) check() error {
if _, ok := uc.mutation.Title(); !ok {
return &ValidationError{Name: "title", err: errors.New(`entv2: missing required field "title"`)}
}
if v, ok := uc.mutation.Blob(); ok {
if err := user.BlobValidator(v); err != nil {
return &ValidationError{Name: "blob", err: fmt.Errorf(`entv2: validator failed for field "blob": %w`, err)}
}
}
if v, ok := uc.mutation.State(); ok {
if err := user.StateValidator(v); err != nil {
return &ValidationError{Name: "state", err: fmt.Errorf(`entv2: validator failed for field "state": %w`, err)}

View File

@@ -426,6 +426,11 @@ func (uu *UserUpdate) check() error {
return &ValidationError{Name: "nickname", err: fmt.Errorf("entv2: validator failed for field \"nickname\": %w", err)}
}
}
if v, ok := uu.mutation.Blob(); ok {
if err := user.BlobValidator(v); err != nil {
return &ValidationError{Name: "blob", err: fmt.Errorf("entv2: validator failed for field \"blob\": %w", err)}
}
}
if v, ok := uu.mutation.State(); ok {
if err := user.StateValidator(v); err != nil {
return &ValidationError{Name: "state", err: fmt.Errorf("entv2: validator failed for field \"state\": %w", err)}
@@ -1173,6 +1178,11 @@ func (uuo *UserUpdateOne) check() error {
return &ValidationError{Name: "nickname", err: fmt.Errorf("entv2: validator failed for field \"nickname\": %w", err)}
}
}
if v, ok := uuo.mutation.Blob(); ok {
if err := user.BlobValidator(v); err != nil {
return &ValidationError{Name: "blob", err: fmt.Errorf("entv2: validator failed for field \"blob\": %w", err)}
}
}
if v, ok := uuo.mutation.State(); ok {
if err := user.StateValidator(v); err != nil {
return &ValidationError{Name: "state", err: fmt.Errorf("entv2: validator failed for field \"state\": %w", err)}