[#3735] fixed `text` field min/max validators to properly count multi-byte characters
This commit is contained in:
parent
4abe199acc
commit
5835193900
|
@ -2,6 +2,8 @@
|
||||||
|
|
||||||
- Fixed TinyMCE source code viewer textarea styles ([#3715](https://github.com/pocketbase/pocketbase/issues/3715)).
|
- 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
|
## v0.19.3
|
||||||
|
|
||||||
|
|
|
@ -132,11 +132,14 @@ func (validator *RecordDataValidator) checkTextValue(field *schema.SchemaField,
|
||||||
|
|
||||||
options, _ := field.Options.(*schema.TextOptions)
|
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))
|
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))
|
return validation.NewError("validation_max_text_constraint", fmt.Sprintf("Must be less than %d character(s)", *options.Max))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -63,6 +63,9 @@ func TestRecordDataValidatorValidateText(t *testing.T) {
|
||||||
Name: "field2",
|
Name: "field2",
|
||||||
Required: true,
|
Required: true,
|
||||||
Type: schema.FieldTypeText,
|
Type: schema.FieldTypeText,
|
||||||
|
Options: &schema.TextOptions{
|
||||||
|
Pattern: pattern,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
&schema.SchemaField{
|
&schema.SchemaField{
|
||||||
Name: "field3",
|
Name: "field3",
|
||||||
|
@ -71,7 +74,6 @@ func TestRecordDataValidatorValidateText(t *testing.T) {
|
||||||
Options: &schema.TextOptions{
|
Options: &schema.TextOptions{
|
||||||
Min: &min,
|
Min: &min,
|
||||||
Max: &max,
|
Max: &max,
|
||||||
Pattern: pattern,
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
@ -109,6 +111,16 @@ func TestRecordDataValidatorValidateText(t *testing.T) {
|
||||||
nil,
|
nil,
|
||||||
[]string{"field3"},
|
[]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",
|
"(text) check max constraint",
|
||||||
map[string]any{
|
map[string]any{
|
||||||
|
@ -119,15 +131,25 @@ func TestRecordDataValidatorValidateText(t *testing.T) {
|
||||||
nil,
|
nil,
|
||||||
[]string{"field3"},
|
[]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",
|
"(text) check pattern constraint",
|
||||||
map[string]any{
|
map[string]any{
|
||||||
"field1": nil,
|
"field1": nil,
|
||||||
"field2": "test",
|
"field2": "test!",
|
||||||
"field3": "test!",
|
"field3": "test",
|
||||||
},
|
},
|
||||||
nil,
|
nil,
|
||||||
[]string{"field3"},
|
[]string{"field2"},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"(text) valid data (only required)",
|
"(text) valid data (only required)",
|
||||||
|
|
Loading…
Reference in New Issue