Skip to content

Commit

Permalink
Validation: fix reflect error on nil arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
System-Glitch committed May 14, 2024
1 parent 118aa7b commit d8b7697
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 4 deletions.
50 changes: 46 additions & 4 deletions util/walk/walk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ func TestMustParse(t *testing.T) {
})
}

func testWalk(t *testing.T, data map[string]any, p string) []*Context {
func testWalk(t *testing.T, data any, p string) []*Context {
matches := make([]*Context, 0, 5)
path, err := Parse(p)

Expand Down Expand Up @@ -877,15 +877,15 @@ func TestPathWalkWildcardEmptyObject(t *testing.T) {
}

func TestPathWalkEmptyArray(t *testing.T) {
data := map[string]any{
var data any = map[string]any{
"array": []string{},
"narray": [][][]string{},
}

expected := []*Context{
{
Value: nil,
Parent: data["array"],
Parent: data.(map[string]any)["array"],
Name: "",
Path: &Path{
Name: strPtr("array"),
Expand All @@ -904,7 +904,7 @@ func TestPathWalkEmptyArray(t *testing.T) {
expected = []*Context{
{
Value: nil,
Parent: data["narray"],
Parent: data.(map[string]any)["narray"],
Name: "",
Path: &Path{
Name: strPtr("narray"),
Expand All @@ -916,6 +916,48 @@ func TestPathWalkEmptyArray(t *testing.T) {
},
}
assert.Equal(t, expected, matches)

// nil array
data = map[string]any{
"array": nil,
}
expected = []*Context{
{
Value: nil,
Parent: data,
Name: "array",
Path: &Path{
Name: strPtr("array"),
Type: PathTypeElement,
Next: nil,
},
Index: -1,
Found: ParentNotFound,
},
}

matches = testWalk(t, data, "array[]")
assert.Equal(t, expected, matches)

// array is the root element
data = []any{}
expected = []*Context{
{
Value: nil,
Parent: data,
Name: "",
Path: &Path{
Name: nil,
Type: PathTypeArray,
Next: &Path{},
},
Index: -1,
Found: ElementNotFound,
},
}

matches = testWalk(t, data, "[]")
assert.Equal(t, expected, matches)
}

func TestPathWalkNotFoundInObject(t *testing.T) {
Expand Down
3 changes: 3 additions & 0 deletions validation/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,9 @@ func makeGenericSlice(original any) ([]any, bool) {
return o, false
}
list := reflect.ValueOf(original)
if !list.IsValid() {
return []any{}, false
}
length := list.Len()
newSlice := make([]any, 0, length)
for i := 0; i < length; i++ {
Expand Down
39 changes: 39 additions & 0 deletions validation/validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -716,6 +716,45 @@ func TestValidate(t *testing.T) {
},
wantData: map[string]any{"property": 123, "object": map[string]any{"property": 456}, "array": []int{7}, "narray": [][]int{{1, 8, 3}}},
},
{
desc: "empty_array",
options: &Options{
Data: map[string]any{"narray": []any{}},
Language: lang.New().GetDefault(),
Rules: RuleSet{
{Path: "narray", Rules: List{Required(), Array()}},
{Path: "narray[]", Rules: List{Array()}},
{Path: "narray[][]", Rules: List{Int()}},
},
},
wantData: map[string]any{"narray": []any{}},
},
{
desc: "empty_narray",
options: &Options{
Data: map[string]any{"narray": []any{[]any{}}},
Language: lang.New().GetDefault(),
Rules: RuleSet{
{Path: "narray", Rules: List{Required(), Array()}},
{Path: "narray[]", Rules: List{Array()}},
{Path: "narray[][]", Rules: List{Int()}},
},
},
wantData: map[string]any{"narray": [][]any{{}}},
},
{
desc: "nil_array",
options: &Options{
Data: map[string]any{"narray": nil},
Language: lang.New().GetDefault(),
Rules: RuleSet{
{Path: "narray", Rules: List{Required(), Nullable(), Array()}},
{Path: "narray[]", Rules: List{Required(), Array()}},
{Path: "narray[][]", Rules: List{Int()}},
},
},
wantData: map[string]any{"narray": nil},
},
{
desc: "type-dependent",
options: &Options{
Expand Down

0 comments on commit d8b7697

Please sign in to comment.