Skip to content

Commit

Permalink
feat(jsonschema): Cleanup mistakes, test for unknown schema type.
Browse files Browse the repository at this point in the history
  • Loading branch information
dustmop committed Jun 7, 2018
1 parent 4a66928 commit 9ab452b
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
2 changes: 1 addition & 1 deletion keywords.go
Expand Up @@ -60,7 +60,7 @@ func NewType() Validator {
return &Type{}
}

// FirstValue returns the first element in the values of this type.
// String returns the type(s) as a string, or unknown if there is no known type.
func (t Type) String() string {
if len(t.vals) == 0 {
return "unknown"
Expand Down
8 changes: 2 additions & 6 deletions schema.go
Expand Up @@ -50,12 +50,8 @@ type RootSchema struct {

// TopLevelType returns a string representing the schema's top-level type.
func (rs *RootSchema) TopLevelType() string {
validator, ok := rs.Schema.Validators["type"]
if ok {
typeValidator, ok := validator.(*Type)
if ok {
return typeValidator.String()
}
if t, ok := rs.Schema.Validators["type"].(*Type); ok {
return t.String()
}
return "unknown"
}
Expand Down
12 changes: 12 additions & 0 deletions schema_test.go
Expand Up @@ -110,6 +110,18 @@ func TestTopLevelType(t *testing.T) {
if rs.TopLevelType() != "array" {
t.Errorf("error: schemaArray should be an array")
}

schemaUnknown := []byte(`{
"title": "Typeless",
"items" : { "title" : "REFERENCE", "$ref" : "#" }
}`)
rs = &RootSchema{}
if err := json.Unmarshal(schemaUnknown, rs); err != nil {
panic("unmarshal schema: " + err.Error())
}
if rs.TopLevelType() != "unknown" {
t.Errorf("error: schemaUnknown should have unknown type")
}
}

func TestMust(t *testing.T) {
Expand Down

0 comments on commit 9ab452b

Please sign in to comment.