@@ -46,6 +46,15 @@ type RootSchema struct {
4646 // for current and previous published drafts of JSON Schema
4747 // vocabularies as deemed reasonable.
4848 SchemaURI string `json:"$schema"`
49+ // Whether the top-level of the schema is an array or not.
50+ // Assume it's an object otherwise.
51+ TopIsArray bool
52+ }
53+
54+ func determineTopIsArray (sch * Schema ) bool {
55+ validator := sch .Validators ["type" ]
56+ typeValidator , ok := validator .(* Type )
57+ return ok && typeValidator .FirstValue () == "array"
4958}
5059
5160// UnmarshalJSON implements the json.Unmarshaler interface for
@@ -55,6 +64,7 @@ func (rs *RootSchema) UnmarshalJSON(data []byte) error {
5564 if err := json .Unmarshal (data , sch ); err != nil {
5665 return err
5766 }
67+ topIsArray := determineTopIsArray (sch )
5868
5969 if sch .schemaType == schemaTypeFalse || sch .schemaType == schemaTypeTrue {
6070 * rs = RootSchema {Schema : * sch }
@@ -69,8 +79,9 @@ func (rs *RootSchema) UnmarshalJSON(data []byte) error {
6979 }
7080
7181 root := & RootSchema {
72- Schema : * sch ,
73- SchemaURI : suri .SchemaURI ,
82+ Schema : * sch ,
83+ SchemaURI : suri .SchemaURI ,
84+ TopIsArray : topIsArray ,
7485 }
7586
7687 // collect IDs for internal referencing:
@@ -122,8 +133,9 @@ func (rs *RootSchema) UnmarshalJSON(data []byte) error {
122133 }
123134
124135 * rs = RootSchema {
125- Schema : * sch ,
126- SchemaURI : suri .SchemaURI ,
136+ Schema : * sch ,
137+ SchemaURI : suri .SchemaURI ,
138+ TopIsArray : topIsArray ,
127139 }
128140 return nil
129141}
0 commit comments