lib/schemacop/v3/hash_node.rb in schemacop-3.0.0.rc0 vs lib/schemacop/v3/hash_node.rb in schemacop-3.0.0.rc1
- old
+ new
@@ -33,11 +33,11 @@
end
@options[:additional_properties] = create(type, **options, &block)
end
- def dsl_dep(source, *targets)
+ def dsl_dep(source, *targets, **_kwargs)
@options[:dependencies] ||= {}
@options[:dependencies][source] = targets
end
def as_json
@@ -163,19 +163,37 @@
def cast(data)
result = {}
data ||= default
return nil if data.nil?
- # TODO: How to handle regex / etc.?
+ property_patterns = {}
+
@properties.each_value do |prop|
+ if prop.name.is_a?(Regexp)
+ property_patterns[prop.name] = prop
+ next
+ end
+
result[prop.name] = prop.cast(data[prop.name])
if result[prop.name].nil? && !data.include?(prop.name)
result.delete(prop.name)
end
end
+ # Handle regex properties
+ specified_properties = @properties.keys.to_set
+ additional_properties = data.reject { |k, _v| specified_properties.include?(k.to_s.to_sym) }
+
+ if additional_properties.any? && property_patterns.any?
+ additional_properties.each do |name, additional_property|
+ match_key = property_patterns.keys.find { |p| p.match?(name.to_s) }
+ match = property_patterns[match_key]
+ result[name] = match.cast(additional_property)
+ end
+ end
+
# Handle additional properties
if options[:additional_properties] == true
result = data.merge(result)
elsif options[:additional_properties].is_a?(Node)
specified_properties = @properties.keys.to_set
@@ -195,9 +213,12 @@
protected
def init
@properties = {}
@options[:type] = :object
+ unless @options[:additional_properties].nil? || @options[:additional_properties].is_a?(TrueClass) || @options[:additional_properties].is_a?(FalseClass)
+ fail Schemacop::Exceptions::InvalidSchemaError, 'Option "additional_properties" must be a boolean value'
+ end
end
def validate_self
unless options[:min_properties].nil? || options[:min_properties].is_a?(Integer)
fail 'Option "min_properties" must be an "integer"'