@@ -45,7 +45,14 @@ func MatchJSON(jsonStr string, schema string) ([]MatchViolation, error) {
4545 return violations , nil
4646}
4747
48- func matchMap (path string , data , schema map [string ]interface {}) (violations []MatchViolation ) {
48+ func matchMap (path string , data interface {}, schema map [string ]interface {}) (violations []MatchViolation ) {
49+ dataMap , ok := data .(map [string ]interface {})
50+ if ! ok {
51+ if ok {
52+ violations = append (violations , typeMismatchViolation (path , "[[object]]" , data ))
53+ }
54+ return
55+ }
4956 for key , schemaValue := range schema {
5057 if key == "[[...rest]]" {
5158 continue
@@ -55,7 +62,7 @@ func matchMap(path string, data, schema map[string]interface{}) (violations []Ma
5562 continue
5663 }
5764
58- dataValue , ok := data [key ]
65+ dataValue , ok := dataMap [key ]
5966 if schemaValue == "[[never]]" {
6067 if ok {
6168 violations = append (violations , typeMismatchViolation (path + "/" + key , "[[never]]" , dataValue ))
@@ -76,9 +83,9 @@ func matchMap(path string, data, schema map[string]interface{}) (violations []Ma
7683 // Allow extra fields by default
7784 restSchemaValue = "[[ignore]]"
7885 }
79- for dataKey := range data {
86+ for dataKey := range dataMap {
8087 if _ , ok := schema [dataKey ]; ! ok {
81- violations = append (violations , matchValue (path + "/" + dataKey , data [dataKey ], restSchemaValue )... )
88+ violations = append (violations , matchValue (path + "/" + dataKey , dataMap [dataKey ], restSchemaValue )... )
8289 }
8390 }
8491
@@ -90,7 +97,7 @@ func matchValue(path string, dataValue, schemaValue interface{}) (violations []M
9097 case string :
9198 violations = append (violations , matchScalar (path , dataValue , schemaValue )... )
9299 case map [string ]interface {}:
93- violations = append (violations , matchMap (path , dataValue .( map [ string ] interface {}) , schemaValue )... )
100+ violations = append (violations , matchMap (path , dataValue , schemaValue )... )
94101 case []interface {}:
95102 violations = append (violations , matchArray (path , dataValue .([]interface {}), schemaValue )... )
96103 default :
@@ -108,19 +115,21 @@ func matchArray(path string, data []interface{}, schemaValue interface{}) (viola
108115
109116 for i , item := range data {
110117 var itemType interface {}
111- if schemaArray [0 ] == "[[arrayof]]" {
118+ if len ( schemaArray ) > 0 && schemaArray [0 ] == "[[arrayof]]" {
112119 itemType = schemaArray [1 ]
113120 } else {
114- itemType = schemaArray [i ]
121+ if i > len (schemaArray )- 1 {
122+ violations = append (violations , typeMismatchViolation (fmt .Sprintf ("%s/%d" , path , i ), "[[undefined]]" , data [i ]))
123+ continue
124+ } else {
125+ itemType = schemaArray [i ]
126+ }
115127 }
116128
117129 violations = append (violations , matchValue (fmt .Sprintf ("%s/%d" , path , i ), item , itemType )... )
118130 }
119131
120132 if len (data ) != len (schemaArray ) && schemaArray [0 ] != "[[arrayof]]" {
121- for i := len (schemaArray ); i < len (data ); i ++ {
122- violations = append (violations , matchValue (fmt .Sprintf ("%s/%d" , path , i ), data [i ], schemaArray [len (schemaArray )- 1 ])... )
123- }
124133 for i := len (data ); i < len (schemaArray ); i ++ {
125134 violations = append (violations , missingFieldViolation (fmt .Sprintf ("%s/%d" , path , i ), schemaArray [i ]))
126135 }
0 commit comments