1
0
mirror of https://github.com/pocketbase/pocketbase.git synced 2025-11-06 09:29:19 +02:00

synced with master

This commit is contained in:
Gani Georgiev
2023-11-10 15:18:14 +02:00
35 changed files with 83 additions and 47 deletions

View File

@@ -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))
}

View File

@@ -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)",