From fcb09408b6d1bbea1a158fdea3a7c52de6901ae1 Mon Sep 17 00:00:00 2001 From: Jaime Soriano Pastor Date: Thu, 20 Jun 2024 11:45:47 +0200 Subject: [PATCH 1/2] Improve error message for undefined arrays of objects --- internal/fields/validate.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/internal/fields/validate.go b/internal/fields/validate.go index b4fb238cd..7aa4d9184 100644 --- a/internal/fields/validate.go +++ b/internal/fields/validate.go @@ -584,7 +584,12 @@ func (v *Validator) validateScalarElement(key string, val interface{}, doc commo } if definition == nil { - return fmt.Errorf(`field "%s" is undefined`, key) + switch val.(type) { + case []any, []map[string]interface{}: + return fmt.Errorf(`field "%s" is used as array of objects, exected explicit definition with type group or nested`, key) + default: + return fmt.Errorf(`field "%s" is undefined`, key) + } } // Convert numeric keyword fields to string for validation. From de043cdfc5a0d7791c86e98b300c1bf278978d11 Mon Sep 17 00:00:00 2001 From: Jaime Soriano Pastor Date: Thu, 20 Jun 2024 12:09:18 +0200 Subject: [PATCH 2/2] Add test --- internal/fields/testdata/fields/fields.yml | 2 ++ .../fields/testdata/undefined-array-of-objects.json | 12 ++++++++++++ internal/fields/validate.go | 2 +- internal/fields/validate_test.go | 11 +++++++++++ 4 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 internal/fields/testdata/undefined-array-of-objects.json diff --git a/internal/fields/testdata/fields/fields.yml b/internal/fields/testdata/fields/fields.yml index 77d39fd73..1fb7459af 100644 --- a/internal/fields/testdata/fields/fields.yml +++ b/internal/fields/testdata/fields/fields.yml @@ -86,3 +86,5 @@ type: keyword normalize: - array +- name: user.group.id + type: keyword diff --git a/internal/fields/testdata/undefined-array-of-objects.json b/internal/fields/testdata/undefined-array-of-objects.json new file mode 100644 index 000000000..4a4f4a697 --- /dev/null +++ b/internal/fields/testdata/undefined-array-of-objects.json @@ -0,0 +1,12 @@ +{ + "user": { + "group": [ + { + "id": "42" + }, + { + "id": "0" + } + ] + } +} diff --git a/internal/fields/validate.go b/internal/fields/validate.go index 7aa4d9184..5e41f3d55 100644 --- a/internal/fields/validate.go +++ b/internal/fields/validate.go @@ -586,7 +586,7 @@ func (v *Validator) validateScalarElement(key string, val interface{}, doc commo if definition == nil { switch val.(type) { case []any, []map[string]interface{}: - return fmt.Errorf(`field "%s" is used as array of objects, exected explicit definition with type group or nested`, key) + return fmt.Errorf(`field "%s" is used as array of objects, expected explicit definition with type group or nested`, key) default: return fmt.Errorf(`field "%s" is undefined`, key) } diff --git a/internal/fields/validate_test.go b/internal/fields/validate_test.go index 5ed37050f..a7c57ff73 100644 --- a/internal/fields/validate_test.go +++ b/internal/fields/validate_test.go @@ -148,6 +148,17 @@ func TestValidate_ipAddress(t *testing.T) { require.Empty(t, errs) } +func TestValidate_undefinedArrayOfObjects(t *testing.T) { + validator, err := CreateValidatorForDirectory("testdata", WithSpecVersion("2.0.0"), WithDisabledDependencyManagement()) + require.NoError(t, err) + require.NotNil(t, validator) + + e := readSampleEvent(t, "testdata/undefined-array-of-objects.json") + errs := validator.ValidateDocumentBody(e) + require.Len(t, errs, 1) + require.Contains(t, errs[0].Error(), `field "user.group" is used as array of objects, expected explicit definition with type group or nested`) +} + func TestValidate_WithSpecVersion(t *testing.T) { validator, err := CreateValidatorForDirectory("testdata", WithSpecVersion("2.0.0"), WithDisabledDependencyManagement()) require.NoError(t, err)