Skip to content

Commit

Permalink
Add symbol option to insert_property_defaults
Browse files Browse the repository at this point in the history
This allows default properties to be inserted using symbol keys.

I didn't think about this when adding symbol key support everywhere. I
don't love overloading `insert_property_defaults` like this, but it
seems slightly better than adding another option (or
`property_default_resolver: :symbol` or whatever).

Addresses: #166
  • Loading branch information
davishmcclurg committed Feb 25, 2024
1 parent e8750cf commit a72473d
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
3 changes: 2 additions & 1 deletion README.md
Expand Up @@ -212,7 +212,8 @@ JSONSchemer.schema(
},

# insert default property values during validation
# true/false
# string keys by default (use `:symbol` to insert symbol keys)
# true/false/:symbol
# default: false
insert_property_defaults: true,

Expand Down
11 changes: 9 additions & 2 deletions lib/json_schemer/schema.rb
Expand Up @@ -46,10 +46,13 @@ def original_instance(instance_location)
false
end
end
SYMBOL_PROPERTY_DEFAULT_RESOLVER = proc do |instance, property, results_with_tree_validity|
DEFAULT_PROPERTY_DEFAULT_RESOLVER.call(instance, property.to_sym, results_with_tree_validity)
end

attr_accessor :base_uri, :meta_schema, :keywords, :keyword_order
attr_reader :value, :parent, :root, :parsed
attr_reader :vocabulary, :format, :formats, :content_encodings, :content_media_types, :custom_keywords, :before_property_validation, :after_property_validation, :insert_property_defaults, :property_default_resolver
attr_reader :vocabulary, :format, :formats, :content_encodings, :content_media_types, :custom_keywords, :before_property_validation, :after_property_validation, :insert_property_defaults

def initialize(
value,
Expand All @@ -67,7 +70,7 @@ def initialize(
before_property_validation: DEFAULT_BEFORE_PROPERTY_VALIDATION,
after_property_validation: DEFAULT_AFTER_PROPERTY_VALIDATION,
insert_property_defaults: false,
property_default_resolver: DEFAULT_PROPERTY_DEFAULT_RESOLVER,
property_default_resolver: nil,
ref_resolver: DEFAULT_REF_RESOLVER,
regexp_resolver: 'ruby',
output_format: 'classic',
Expand Down Expand Up @@ -401,6 +404,10 @@ def root_keyword_location
@root_keyword_location ||= Location.root
end

def property_default_resolver
@property_default_resolver ||= insert_property_defaults == :symbol ? SYMBOL_PROPERTY_DEFAULT_RESOLVER : DEFAULT_PROPERTY_DEFAULT_RESOLVER
end

def ref_resolver
@ref_resolver ||= @original_ref_resolver == 'net/http' ? CachedResolver.new(&NET_HTTP_REF_RESOLVER) : @original_ref_resolver
end
Expand Down
22 changes: 22 additions & 0 deletions test/hooks_test.rb
Expand Up @@ -100,6 +100,28 @@ def test_it_inserts_defaults_in_symbol_keys
assert_equal('x', instance.dig(:a, :b, 'c'))
end

def test_it_can_insert_symbol_keys
schema = {
'properties' => {
'a' => {
'default' => 'x'
}
}
}

schemer = JSONSchemer.schema(schema, insert_property_defaults: true)
instance = {}
schemer.validate(instance)
refute(instance.key?(:a))
assert_equal('x', instance.fetch('a'))

schemer = JSONSchemer.schema(schema, insert_property_defaults: :symbol)
instance = {}
schemer.validate(instance)
refute(instance.key?('a'))
assert_equal('x', instance.fetch(:a))
end

def test_it_does_not_fail_using_insert_defaults_when_no_properties_are_defined_by_schema
schema = {
'$comment' => 'Mostly empty schema'
Expand Down

0 comments on commit a72473d

Please sign in to comment.