Class: CustomDependenciesAttribute

Inherits:
JSON::Schema::Attribute
  • Object
show all
Defined in:
lib/snowly/extensions/custom_dependencies.rb

Class Method Summary collapse

Class Method Details

.accept_value?(value) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/snowly/extensions/custom_dependencies.rb', line 40

def self.accept_value?(value)
  value.is_a?(Array) || value.is_a?(Hash)
end

.validate(current_schema, data, fragments, processor, validator, options = {}) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/snowly/extensions/custom_dependencies.rb', line 17

def self.validate(current_schema, data, fragments, processor, validator, options = {})
  return unless data.is_a?(Hash)
  current_schema.schema['custom_dependencies'].each do |property, dependency_value|
    next unless accept_value?(dependency_value)
    case dependency_value
    when Array
      dependency_value.each do |dependency_hash|
        validate_dependency(current_schema, data, property, dependency_hash, fragments, processor, self, options)
      end
    when Hash
      validate_dependency(current_schema, data, property, dependency_value, fragments, processor, self, options)
    end
  end
end

.validate_dependency(schema, data, property, dependency_hash, fragments, processor, attribute, options) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/snowly/extensions/custom_dependencies.rb', line 32

def self.validate_dependency(schema, data, property, dependency_hash, fragments, processor, attribute, options)
  key, value = Array(dependency_hash).flatten
  return unless data[key.to_s] == value.to_s
  return if data.has_key?(property.to_s)
  message = "The property '#{build_fragment(fragments)}' did not contain a required property of '#{property}' when property '#{key}' is '#{value}'"
  validation_error(processor, message, fragments, schema, attribute, options[:record_errors])
end