Skip to content

Commit

Permalink
Simple check for symbol keys in schemas
Browse files Browse the repository at this point in the history
I didn't want to iterate over the entire schema and I think checking the
first key should catch most errors.

Fixes #9
  • Loading branch information
davishmcclurg committed Aug 27, 2018
1 parent d3939c1 commit b014aba
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/json_schemer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ module JSONSchemer
class UnsupportedMetaSchema < StandardError; end
class UnknownRef < StandardError; end
class InvalidFileURI < StandardError; end
class InvalidSymbolKey < StandardError; end

DRAFT_CLASS_BY_META_SCHEMA = {
'http://json-schema.org/draft-04/schema#' => Schema::Draft4,
Expand Down
1 change: 1 addition & 0 deletions lib/json_schemer/schema/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ def initialize(
keywords: nil,
ref_resolver: DEFAULT_REF_RESOLVER
)
raise InvalidSymbolKey, 'schemas must use string keys' if schema.is_a?(Hash) && schema.first.first.is_a?(Symbol)
@root = schema
@format = format
@formats = formats
Expand Down
11 changes: 11 additions & 0 deletions test/json_schemer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,17 @@ def test_it_handles_json_strings
assert !schema.valid?('1')
end

def test_it_checks_for_symbol_keys
assert_raises(JSONSchemer::InvalidSymbolKey) { JSONSchemer.schema({ :type => 'integer' }) }
schema = JSONSchemer.schema(
{ '$ref' => 'http://example.com' },
:ref_resolver => proc do |uri|
{ :type => 'integer' }
end
)
assert_raises(JSONSchemer::InvalidSymbolKey) { schema.valid?(1) }
end

def test_cached_ref_resolver
schema = {
'properties' => {
Expand Down

0 comments on commit b014aba

Please sign in to comment.