Skip to content

Commit

Permalink
normalized the caster to return always non-null value and fixed minor…
Browse files Browse the repository at this point in the history
… ui issues
  • Loading branch information
ganigeorgiev committed Jul 17, 2022
1 parent f19b9e3 commit 994761b
Show file tree
Hide file tree
Showing 34 changed files with 346 additions and 321 deletions.
27 changes: 17 additions & 10 deletions apis/record_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,12 @@ func TestRecordsList(t *testing.T) {
`"manyrels":[{`,
`"manyrels":[]`,
`"rel_cascade":"`,
`"rel_cascade":null`,
`"rel_cascade":""`,
`"onerel":{"@collectionId":"3f2888f8-075d-49fe-9d09-ea7e951000dc","@collectionName":"demo",`,
`"json":[1,2,3]`,
`"select":["a","b"]`,
`"select":[]`,
`"user":null`,
`"user":""`,
`"bool":true`,
`"number":456`,
`"user":"97cc3d3d-6ba2-383f-b42a-7bc84d27410c"`,
Expand Down Expand Up @@ -650,7 +650,8 @@ func TestRecordCreate(t *testing.T) {
"onerel": "577bd676-aacb-4072-b7da-99d00ee210a4",
"manyrels": ["577bd676-aacb-4072-b7da-99d00ee210a4"],
"text": "test123",
"bool": "false"
"bool": "false",
"number": 1
}`),
RequestHeaders: map[string]string{
// [email protected]
Expand All @@ -668,7 +669,8 @@ func TestRecordCreate(t *testing.T) {
"onerel":"577bd676-aacb-4072-b7da-99d00ee210a4",
"manyrels":["577bd676-aacb-4072-b7da-99d00ee210a4"],
"text":"test123",
"bool":true
"bool":true,
"number":1
}`),
RequestHeaders: map[string]string{
// [email protected]
Expand All @@ -682,6 +684,7 @@ func TestRecordCreate(t *testing.T) {
`"manyrels":["577bd676-aacb-4072-b7da-99d00ee210a4"]`,
`"text":"test123"`,
`"bool":true`,
`"number":1`,
},
ExpectedEvents: map[string]int{
"OnRecordBeforeCreateRequest": 1,
Expand All @@ -695,11 +698,12 @@ func TestRecordCreate(t *testing.T) {
Method: http.MethodPost,
Url: "/api/collections/demo2/records",
Body: strings.NewReader(`{
"rel_cascade":"577bd676-aacb-4072-b7da-99d00ee210a4",
"onerel":"577bd676-aacb-4072-b7da-99d00ee210a4",
"manyrels":["577bd676-aacb-4072-b7da-99d00ee210a4"],
"text":"test123",
"bool":false
"rel_cascade": "577bd676-aacb-4072-b7da-99d00ee210a4",
"onerel": "577bd676-aacb-4072-b7da-99d00ee210a4",
"manyrels" :["577bd676-aacb-4072-b7da-99d00ee210a4"],
"text": "test123",
"bool": false,
"number": 1
}`),
RequestHeaders: map[string]string{
"Authorization": "Admin eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjJiNGE5N2NjLTNmODMtNGQwMS1hMjZiLTNkNzdiYzg0MmQzYyIsInR5cGUiOiJhZG1pbiIsImV4cCI6MTg3MzQ2Mjc5Mn0.AtRtXR6FHBrCUGkj5OffhmxLbSZaQ4L_Qgw4gfoHyfo",
Expand All @@ -712,6 +716,7 @@ func TestRecordCreate(t *testing.T) {
`"manyrels":["577bd676-aacb-4072-b7da-99d00ee210a4"]`,
`"text":"test123"`,
`"bool":false`,
`"number":1`,
},
ExpectedEvents: map[string]int{
"OnRecordBeforeCreateRequest": 1,
Expand Down Expand Up @@ -867,7 +872,8 @@ func TestRecordUpdate(t *testing.T) {
Method: http.MethodPatch,
Url: "/api/collections/demo2/records/63c2ab80-84ab-4057-a592-4604a731f78f",
Body: strings.NewReader(`{
"text":"test_new"
"text":"test_new",
"number":1
}`),
RequestHeaders: map[string]string{
"Authorization": "Admin eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjJiNGE5N2NjLTNmODMtNGQwMS1hMjZiLTNkNzdiYzg0MmQzYyIsInR5cGUiOiJhZG1pbiIsImV4cCI6MTg3MzQ2Mjc5Mn0.AtRtXR6FHBrCUGkj5OffhmxLbSZaQ4L_Qgw4gfoHyfo",
Expand All @@ -876,6 +882,7 @@ func TestRecordUpdate(t *testing.T) {
ExpectedContent: []string{
`"id":"63c2ab80-84ab-4057-a592-4604a731f78f"`,
`"text":"test_new"`,
`"number":1`,
},
ExpectedEvents: map[string]int{
"OnRecordBeforeUpdateRequest": 1,
Expand Down
2 changes: 1 addition & 1 deletion forms/record_upsert.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ func (form *RecordUpsert) LoadData(r *http.Request) error {
if len(oldNames) > 0 {
form.filesToDelete = append(form.filesToDelete, oldNames...)
}
form.Data[key] = nil
form.Data[key] = ""
}
} else if options.MaxSelect > 1 {
// search for individual file index to delete (eg. "file.0")
Expand Down
8 changes: 4 additions & 4 deletions forms/record_upsert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ func TestRecordUpsertLoadDataJson(t *testing.T) {
if !ok {
t.Fatal("Expect onefile field to be set")
}
if onefile != nil {
t.Fatalf("Expect onefile field to be nil, got %v", onefile)
if onefile != "" {
t.Fatalf("Expect onefile field to be empty string, got %v", onefile)
}

manyfiles, ok := form.Data["manyfiles"]
Expand Down Expand Up @@ -166,8 +166,8 @@ func TestRecordUpsertLoadDataMultipart(t *testing.T) {
if !ok {
t.Fatal("Expect onefile field to be set")
}
if onefile != nil {
t.Fatalf("Expect onefile field to be nil, got %v", onefile)
if onefile != "" {
t.Fatalf("Expect onefile field to be empty string, got %v", onefile)
}

manyfiles, ok := form.Data["manyfiles"]
Expand Down
40 changes: 14 additions & 26 deletions forms/validators/record_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,6 @@ func (validator *RecordDataValidator) checkUrlValue(field *schema.SchemaField, v

func (validator *RecordDataValidator) checkDateValue(field *schema.SchemaField, value any) error {
val, _ := value.(types.DateTime)

if val.IsZero() {
if field.Required {
return requiredErr
Expand Down Expand Up @@ -268,6 +267,9 @@ func (validator *RecordDataValidator) checkDateValue(field *schema.SchemaField,
func (validator *RecordDataValidator) checkSelectValue(field *schema.SchemaField, value any) error {
normalizedVal := list.ToUniqueStringSlice(value)
if len(normalizedVal) == 0 {
if field.Required {
return requiredErr
}
return nil // nothing to check
}

Expand Down Expand Up @@ -302,13 +304,9 @@ func (validator *RecordDataValidator) checkJsonValue(field *schema.SchemaField,
}

func (validator *RecordDataValidator) checkFileValue(field *schema.SchemaField, value any) error {
// normalize value access
var names []string
switch v := value.(type) {
case []string:
names = v
case string:
names = []string{v}
names := list.ToUniqueStringSlice(value)
if len(names) == 0 && field.Required {
return requiredErr
}

options, _ := field.Options.(*schema.FileOptions)
Expand Down Expand Up @@ -343,16 +341,11 @@ func (validator *RecordDataValidator) checkFileValue(field *schema.SchemaField,
}

func (validator *RecordDataValidator) checkRelationValue(field *schema.SchemaField, value any) error {
// normalize value access
var ids []string
switch v := value.(type) {
case []string:
ids = v
case string:
ids = []string{v}
}

ids := list.ToUniqueStringSlice(value)
if len(ids) == 0 {
if field.Required {
return requiredErr
}
return nil // nothing to check
}

Expand Down Expand Up @@ -383,16 +376,11 @@ func (validator *RecordDataValidator) checkRelationValue(field *schema.SchemaFie
}

func (validator *RecordDataValidator) checkUserValue(field *schema.SchemaField, value any) error {
// normalize value access
var ids []string
switch v := value.(type) {
case []string:
ids = v
case string:
ids = []string{v}
}

ids := list.ToUniqueStringSlice(value)
if len(ids) == 0 {
if field.Required {
return requiredErr
}
return nil // nothing to check
}

Expand Down
Loading

0 comments on commit 994761b

Please sign in to comment.