8000 schema/field: validate rune length with `MinRuneLen` / `MaxRuneLen` by liangminhua · Pull Request #4398 · ent/ent · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

schema/field: validate rune length with MinRuneLen / MaxRuneLen #4398

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 20, 2025
Merged
Show file tree
Hide file tree
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
26 changes: 26 additions & 0 deletions schema/field/field.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"regexp"
"strings"
"time"
"unicode/utf8"

"entgo.io/ent/schema"
)
Expand Down Expand Up @@ -222,6 +223,18 @@ func (b *stringBuilder) MinLen(i int) *stringBuilder {
return b
}

// MinRuneLen adds a rune length validator for this field.
// Operation fails if the rune count of the string is less than the given value.
func (b *stringBuilder) MinRuneLen(i int) *stringBuilder {
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 rune length")
}
return nil
})
return b
}

// NotEmpty adds a length validator for this field.
// Operation fails if the length of the string is zero.
func (b *stringBuilder) NotEmpty() *stringBuilder {
Expand All @@ -241,6 +254,19 @@ func (b *stringBuilder) MaxLen(i int) *stringBuilder {
return b
}

// MaxRuneLen adds a rune length validator for this field.
// Operation fails if the rune count 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 rune 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
40 changes: 40 additions & 0 deletions schema/field/field_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -975,3 +975,43 @@ func TestTypeConstName(t *testing.T) {
typ = 21
assert.Equal(t, "invalid", typ.ConstName())
}

func TestString_MinRuneLen(t *testing.T) {
fd := field.String("name").MinRuneLen(5).Descriptor()
assert.Len(t, fd.Validators, 1)

err := fd.Validators[0].(func(string) error)("hello")
assert.NoError(t, err)

err = fd.Validators[0].(func(string) error)("hi")
assert.EqualError(t, err, "value is less than the required rune length")

err = fd.Validators[0].(func(string) error)("你好")
assert.EqualError(t, err, "value is less than the required rune length")

err = fd.Validators[0].(func(string) error)("你好世界!")
assert.NoError(t, err)

err = fd.Validators[0].(func(string) error)("")
assert.Error(t, err)
}

func TestString_MaxRuneLen(t *testing.T) {
fd := field.String("name").MaxRuneLen(5).Descriptor()
assert.Len(t, fd.Validators, 1)

err := fd.Validators[0].(func(string) error)("hello")
assert.NoError(t, err)

err = fd.Validators[0].(func(string) error)("hello world")
assert.EqualError(t, err, "value is greater than the required rune length")

err = fd.Validators[0].(func(string) error)("你好世界你好")
assert.EqualError(t, err, "value is greater than the required rune length")

err = fd.Validators[0].(func(string) error)("你好世界!")
assert.NoError(t, err)

err = fd.Validators[0].(func(string) error)("")
assert.NoError(t, err)
}
Loading
0