-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Validate that mappings are a JSON object, not just valid json (#719)
* Validate that mappings are a JSON object, not just valid json * Docs * Changelog * Update internal/elasticsearch/index/validation_test.go Co-authored-by: Boris Ilyushonak <[email protected]> --------- Co-authored-by: Boris Ilyushonak <[email protected]>
- Loading branch information
Showing
8 changed files
with
95 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package index | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
) | ||
|
||
func stringIsJSONObject(i interface{}, s string) (warnings []string, errors []error) { | ||
iStr, ok := i.(string) | ||
if !ok { | ||
errors = append(errors, fmt.Errorf("expected type of %s to be string", s)) | ||
return warnings, errors | ||
} | ||
|
||
m := map[string]interface{}{} | ||
if err := json.Unmarshal([]byte(iStr), &m); err != nil { | ||
errors = append(errors, fmt.Errorf("expected %s to be a JSON object. Check the documentation for the expected format. %w", s, err)) | ||
return | ||
} | ||
|
||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package index | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func Test_stringIsJSONObject(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
fieldVal interface{} | ||
expectedErrsToContain []string | ||
}{ | ||
{ | ||
name: "should not return an error for a valid json object", | ||
fieldVal: "{}", | ||
}, | ||
{ | ||
name: "should not return an error for a null", | ||
fieldVal: "null", | ||
}, | ||
|
||
{ | ||
name: "should return an error if the field is not a string", | ||
fieldVal: true, | ||
expectedErrsToContain: []string{ | ||
"expected type of field-name to be string", | ||
}, | ||
}, | ||
{ | ||
name: "should return an error if the field is valid json, but not an object", | ||
fieldVal: "[]", | ||
expectedErrsToContain: []string{ | ||
"expected field-name to be a JSON object. Check the documentation for the expected format.", | ||
}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
warnings, errors := stringIsJSONObject(tt.fieldVal, "field-name") | ||
require.Empty(t, warnings) | ||
|
||
require.Equal(t, len(tt.expectedErrsToContain), len(errors)) | ||
for i, err := range errors { | ||
require.ErrorContains(t, err, tt.expectedErrsToContain[i]) | ||
} | ||
}) | ||
} | ||
} |