Class: JsonValidation::Validators::Dependencies

Inherits:
Validator
  • Object
show all
Defined in:
lib/json_validation/validators/dependencies.rb

Instance Attribute Summary

Attributes inherited from Validator

#base_uri, #fragment

Instance Method Summary collapse

Methods inherited from Validator

#build_validator, #build_validator_with_new_base_uri, #initialize, type

Constructor Details

This class inherits a constructor from JsonValidation::Validators::Validator

Instance Method Details

#inner_validatorsObject



29
30
31
32
33
# File 'lib/json_validation/validators/dependencies.rb', line 29

def inner_validators
  @inner_validators ||= Hash[fragment['dependencies'].map {|k, f|
    [k, build_validator(f)]
  }]
end

#validate(record) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/json_validation/validators/dependencies.rb', line 6

def validate(record)
  fragment['dependencies'].all? {|property, v|
    case v
    when Hash
      dependency_fragment = v
      if record.has_key?(property)
        inner_validators[property].validate(record)
      else
        true
      end
    when Array
      property_set = v
      if record.has_key?(property)
        property_set.all? {|p| record.has_key?(p)}
      else
        true
      end
    else
      raise "Unexpected type for fragment['dependencies']['#{property}']"
    end
  }
end