8000 Bytes min len by hedwigz · Pull Request #1867 · ent/ent · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Bytes min len #1867

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 10 commits into from
Aug 24, 2021
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
1 change: 1 addition & 0 deletions doc/md/schema-fields.md
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,7 @@ The framework provides a few built-in validators for each type:

- `[]byte`
- `MaxLen(i)`
- `MinLen(i)`

## Optional

Expand Down
16 changes: 15 additions & 1 deletion entc/integration/ent/runtime.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion entc/integration/ent/schema/fieldtype.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,8 @@ func (FieldType) Fields() []ent.Field { //nolint:funlen
GoType(&sql.NullTime{}),
field.Bytes("raw_data").
Optional().
MaxLen(20),
MaxLen(20).
MinLen(3),
field.Bytes("ip").
Optional().
GoType(net.IP("127.0.0.1")).
Expand Down
16 changes: 15 additions & 1 deletion entc/integration/gremlin/ent/runtime.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 10 additions & 2 deletions entc/integration/type_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,16 @@ func Types(t *testing.T, client *ent.Client) {
SetInt64(64).
SetRawData(make([]byte, 40)).
Save(ctx)
require.Error(err)

require.Error(err, "MaxLen validator should reject this operation")
_, err = client.FieldType.Create().
SetInt(1).
SetInt8(8).
SetInt16(16).
SetInt32(32).
SetInt64(64).
SetRawData(make([]byte, 2)).
Save(ctx)
require.Error(err, "MinLen validator should reject this operation")
ft = ft.Update().
SetInt(1).
SetInt8(math.MaxInt8).
Expand Down
12 changes: 12 additions & 0 deletions schema/field/field.go
7263
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,18 @@ func (b *bytesBuilder) MaxLen(i int) *bytesBuilder {
return b
}

// MinLen adds a length validator for this field.
// Operation fails if the length of the buffer is less than the given value.
func (b *bytesBuilder) MinLen(i int) *bytesBuilder {
b.desc.Validators = append(b.desc.Validators, func(b []byte) error {
if len(b) < 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.
//
// field.Bytes("blob").
Expand Down
0