Class: Chronicle::Schema::Validation::EdgeValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/chronicle/schema/validation/edge_validator.rb

Instance Method Summary collapse

Instance Method Details

#validate(type, edge, value) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/chronicle/schema/validation/edge_validator.rb', line 5

def validate(type, edge, value)
  errors = {}
  return errors unless value.is_a?(Hash)

  value_type = (value[:@type] || value['@type']).to_sym

  property = fetch_property(type, edge)
  unless property
    errors[:base] = 'not a valid edge'
    return errors
  end

  complete_range = fetch_complete_range(property)

  unless complete_range.include?(value_type)
    errors[:base] =
      "#{value_type} is not a valid type for #{edge}. Valid types are #{complete_range.join(', ')}"
    return errors
  end

  contract_klass = Chronicle::Schema::Validation.get_contract(value_type)
  unless contract_klass
    errors[:base] = "no contract found for #{value_type}"
    return errors
  end

  result = contract_klass.new.call(value)

  result.errors.to_h
end