From 9ab452b8d1d236f2396b704afe67e9fbe5395afb Mon Sep 17 00:00:00 2001 From: Dustin Long Date: Thu, 7 Jun 2018 10:56:49 -0400 Subject: [PATCH] feat(jsonschema): Cleanup mistakes, test for unknown schema type. --- keywords.go | 2 +- schema.go | 8 ++------ schema_test.go | 12 ++++++++++++ 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/keywords.go b/keywords.go index 943ee42..92d6dd3 100644 --- a/keywords.go +++ b/keywords.go @@ -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" diff --git a/schema.go b/schema.go index 92804f6..e7c9f75 100644 --- a/schema.go +++ b/schema.go @@ -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" } diff --git a/schema_test.go b/schema_test.go index 1429ef9..d020cce 100644 --- a/schema_test.go +++ b/schema_test.go @@ -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) {