Skip to content

Commit

Permalink
[pocketbase#3735] fixed text field min/max validators to properly c…
Browse files Browse the repository at this point in the history
…ount multi-byte characters
  • Loading branch information
ganigeorgiev committed Nov 10, 2023
1 parent 4abe199 commit 5835193
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 8 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

- Fixed TinyMCE source code viewer textarea styles ([#3715](https://github.com/pocketbase/pocketbase/issues/3715)).

- Fixed `text` field min/max validators to properly count multi-byte characters ([#3735](https://github.com/pocketbase/pocketbase/issues/3735)).


## v0.19.3

Expand Down
7 changes: 5 additions & 2 deletions forms/validators/record_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,14 @@ func (validator *RecordDataValidator) checkTextValue(field *schema.SchemaField,

options, _ := field.Options.(*schema.TextOptions)

if options.Min != nil && len(val) < *options.Min {
// note: casted to []rune to count multi-byte chars as one
length := len([]rune(val))

if options.Min != nil && length < *options.Min {
return validation.NewError("validation_min_text_constraint", fmt.Sprintf("Must be at least %d character(s)", *options.Min))
}

if options.Max != nil && len(val) > *options.Max {
if options.Max != nil && length > *options.Max {
return validation.NewError("validation_max_text_constraint", fmt.Sprintf("Must be less than %d character(s)", *options.Max))
}

Expand Down
34 changes: 28 additions & 6 deletions forms/validators/record_data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,17 @@ func TestRecordDataValidatorValidateText(t *testing.T) {
Name: "field2",
Required: true,
Type: schema.FieldTypeText,
Options: &schema.TextOptions{
Pattern: pattern,
},
},
&schema.SchemaField{
Name: "field3",
Unique: true,
Type: schema.FieldTypeText,
Options: &schema.TextOptions{
Min: &min,
Max: &max,
Pattern: pattern,
Min: &min,
Max: &max,
},
},
)
Expand Down Expand Up @@ -109,6 +111,16 @@ func TestRecordDataValidatorValidateText(t *testing.T) {
nil,
[]string{"field3"},
},
{
"(text) check min constraint with multi-bytes char",
map[string]any{
"field1": "test",
"field2": "test",
"field3": "𝌆", // 4 bytes should be counted as 1 char
},
nil,
[]string{"field3"},
},
{
"(text) check max constraint",
map[string]any{
Expand All @@ -119,15 +131,25 @@ func TestRecordDataValidatorValidateText(t *testing.T) {
nil,
[]string{"field3"},
},
{
"(text) check max constraint with multi-bytes chars",
map[string]any{
"field1": "test",
"field2": "test",
"field3": strings.Repeat("𝌆", max), // shouldn't exceed the max limit even though max*4bytes chars are used
},
nil,
[]string{},
},
{
"(text) check pattern constraint",
map[string]any{
"field1": nil,
"field2": "test",
"field3": "test!",
"field2": "test!",
"field3": "test",
},
nil,
[]string{"field3"},
[]string{"field2"},
},
{
"(text) valid data (only required)",
Expand Down

0 comments on commit 5835193

Please sign in to comment.