lib/schemacop/v3/hash_node.rb in schemacop-3.0.0.rc2 vs lib/schemacop/v3/hash_node.rb in schemacop-3.0.0.rc3

- old
+ new

@@ -9,18 +9,28 @@ property_names ].freeze supports_children(name: true) + attr_reader :properties + def self.allowed_options super + ATTRIBUTES - %i[dependencies] + %i[additional_properties] end def self.dsl_methods super + NodeRegistry.dsl_methods(true) + %i[dsl_dep dsl_add] end + def self.sanitize_exp(exp) + exp = exp.to_s + if exp.start_with?('(?-mix:') + exp = exp.to_s.gsub(/^\(\?-mix:/, '').gsub(/\)$/, '') + end + return exp + end + def add_child(node) unless node.name fail Exceptions::InvalidSchemaError, 'Child nodes must have a name.' end @@ -52,11 +62,11 @@ end end json = {} json[:properties] = Hash[properties.values.map { |p| [p.name, p.as_json] }] if properties.any? - json[:patternProperties] = Hash[pattern_properties.values.map { |p| [sanitize_exp(p.name), p.as_json] }] if pattern_properties.any? + json[:patternProperties] = Hash[pattern_properties.values.map { |p| [self.class.sanitize_exp(p.name), p.as_json] }] if pattern_properties.any? # In schemacop, by default, additional properties are not allowed, # the users explicitly need to enable additional properties if options[:additional_properties] == true json[:additionalProperties] = true @@ -73,18 +83,10 @@ end return process_json(ATTRIBUTES, json) end - def sanitize_exp(exp) - exp = exp.to_s - if exp.start_with?('(?-mix:') - exp = exp.to_s.gsub(/^\(\?-mix:/, '').gsub(/\)$/, '') - end - return exp - end - def allowed_types { Hash => :object } end def _validate(data, result: Result.new) @@ -189,13 +191,13 @@ if prop.name.is_a?(Regexp) property_patterns[prop.name] = prop next end - result[prop.name] = prop.cast(data_hash[prop.name]) + result[prop.as || prop.name] = prop.cast(data_hash[prop.name]) - if result[prop.name].nil? && !data_hash.include?(prop.name) - result.delete(prop.name) + if result[prop.as || prop.name].nil? && !data_hash.include?(prop.name) + result.delete(prop.as || prop.name) end end # Handle regex properties specified_properties = @properties.keys.to_set