mirror of
https://github.com/pocketbase/pocketbase.git
synced 2024-11-28 10:03:42 +02:00
[#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 `text` field min/max validators to properly count multi-byte characters ([#3735](https://github.com/pocketbase/pocketbase/issues/3735)).
|
||||
|
||||
|
||||
## v0.19.3
|
||||
|
||||
|
@ -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))
|
||||
}
|
||||
|
||||
|
@ -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,
|
||||
},
|
||||
},
|
||||
)
|
||||
@ -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{
|
||||
@ -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)",
|
||||
|
Loading…
Reference in New Issue
Block a user