Skip to content

Commit 4a66928

Browse files
committed
feat(jsonschema): Change to TopLevelType function, more general.
1 parent 8bd68f0 commit 4a66928

File tree

3 files changed

+18
-17
lines changed

3 files changed

+18
-17
lines changed

keywords.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"reflect"
77
"strconv"
8+
"strings"
89
)
910

1011
// primitiveTypes is a map of strings to check types against
@@ -60,11 +61,11 @@ func NewType() Validator {
6061
}
6162

6263
// FirstValue returns the first element in the values of this type.
63-
func (t Type) FirstValue() string {
64+
func (t Type) String() string {
6465
if len(t.vals) == 0 {
65-
return ""
66+
return "unknown"
6667
}
67-
return t.vals[0]
68+
return strings.Join(t.vals, ",")
6869
}
6970

7071
// Validate checks to see if input data satisfies the type constraint

schema.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,18 @@ 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
5249
}
5350

54-
func determineTopIsArray(sch *Schema) bool {
55-
validator := sch.Validators["type"]
56-
typeValidator, ok := validator.(*Type)
57-
return ok && typeValidator.FirstValue() == "array"
51+
// TopLevelType returns a string representing the schema's top-level type.
52+
func (rs *RootSchema) TopLevelType() string {
53+
validator, ok := rs.Schema.Validators["type"]
54+
if ok {
55+
typeValidator, ok := validator.(*Type)
56+
if ok {
57+
return typeValidator.String()
58+
}
59+
}
60+
return "unknown"
5861
}
5962

6063
// UnmarshalJSON implements the json.Unmarshaler interface for
@@ -64,7 +67,6 @@ func (rs *RootSchema) UnmarshalJSON(data []byte) error {
6467
if err := json.Unmarshal(data, sch); err != nil {
6568
return err
6669
}
67-
topIsArray := determineTopIsArray(sch)
6870

6971
if sch.schemaType == schemaTypeFalse || sch.schemaType == schemaTypeTrue {
7072
*rs = RootSchema{Schema: *sch}
@@ -81,7 +83,6 @@ func (rs *RootSchema) UnmarshalJSON(data []byte) error {
8183
root := &RootSchema{
8284
Schema: *sch,
8385
SchemaURI: suri.SchemaURI,
84-
TopIsArray: topIsArray,
8586
}
8687

8788
// collect IDs for internal referencing:
@@ -135,7 +136,6 @@ func (rs *RootSchema) UnmarshalJSON(data []byte) error {
135136
*rs = RootSchema{
136137
Schema: *sch,
137138
SchemaURI: suri.SchemaURI,
138-
TopIsArray: topIsArray,
139139
}
140140
return nil
141141
}

schema_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,8 @@ func TestTopLevelType(t *testing.T) {
9494
if err := json.Unmarshal(schemaObject, rs); err != nil {
9595
panic("unmarshal schema: " + err.Error())
9696
}
97-
if rs.TopIsArray {
98-
t.Errorf("error: schemaObject should not be an array")
97+
if rs.TopLevelType() != "object" {
98+
t.Errorf("error: schemaObject should be an object")
9999
}
100100

101101
schemaArray := []byte(`{
@@ -107,8 +107,8 @@ func TestTopLevelType(t *testing.T) {
107107
if err := json.Unmarshal(schemaArray, rs); err != nil {
108108
panic("unmarshal schema: " + err.Error())
109109
}
110-
if !rs.TopIsArray {
111-
t.Errorf("error: schemaArray should not be an object")
110+
if rs.TopLevelType() != "array" {
111+
t.Errorf("error: schemaArray should be an array")
112112
}
113113
}
114114

0 commit comments

Comments
 (0)