Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions schema/field/field.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"regexp"
"strings"
"time"
"unicode/utf8"

"entgo.io/ent/schema"
)
Expand Down Expand Up @@ -219,6 +220,32 @@ func (b *stringBuilder) MaxLen(i int) *stringBuilder {
return b
}

// MaxRuneLen adds a length validator for this field.
// Operation fails if the length of the string is greater than the given value.
func (b *stringBuilder) MaxRuneLen(i int) *stringBuilder {
b.desc.Size = i
b.desc.Validators = append(b.desc.Validators, func(v string) error {
if utf8.RuneCountInString(v) > i {
return errors.New("value is greater than the required length")
}
return nil
})
return b
}

// MinRuneLen adds a length validator for this field.
// Operation fails if the length of the string is less than the given value.
func (b *stringBuilder) MinRuneLen(i int) *stringBuilder {
b.desc.Size = i
b.desc.Validators = append(b.desc.Validators, func(v string) error {
if utf8.RuneCountInString(v) < i {
return errors.New("value is less than the required length")
}
return nil
})
return b
}

// Validate adds a validator for this field. Operation fails if the validation fails.
func (b *stringBuilder) Validate(fn func(string) error) *stringBuilder {
b.desc.Validators = append(b.desc.Validators, fn)
Expand Down