Class: Interfacets::Test::ValidationEngine

Inherits:
Object
  • Object
show all
Defined in:
lib/interfacets/test/validation_engine.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(registry = ComponentRegistry.load, validation_mode: :strict) ⇒ ValidationEngine

Returns a new instance of ValidationEngine.



22
23
24
25
# File 'lib/interfacets/test/validation_engine.rb', line 22

def initialize(registry = ComponentRegistry.load, validation_mode: :strict)
  @registry = registry
  @validation_mode = validation_mode
end

Instance Attribute Details

#registry ⇒ Object (readonly)

Returns the value of attribute registry.



10
11
12
# File 'lib/interfacets/test/validation_engine.rb', line 10

def registry
  @registry
end

#validation_mode ⇒ Object (readonly)

Returns the value of attribute validation_mode.



10
11
12
# File 'lib/interfacets/test/validation_engine.rb', line 10

def validation_mode
  @validation_mode
end

Class Method Details

.reset_warnings! ⇒ Object



18
19
20
# File 'lib/interfacets/test/validation_engine.rb', line 18

def self.reset_warnings!
  @warned_components.clear
end

.warned_components ⇒ Object



14
15
16
# File 'lib/interfacets/test/validation_engine.rb', line 14

def self.warned_components
  @warned_components
end

Instance Method Details

#active? ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/interfacets/test/validation_engine.rb', line 27

def active?
  registry.contracts.any?
end

#validate_event(component_name, event_name, payload) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/interfacets/test/validation_engine.rb', line 51

def validate_event(component_name, event_name, payload)
  return unless active?

  contract = registry.find_component(component_name)
  is_standard = standard_element?(component_name)

  event_config = contract&.dig("props", event_name.to_s)

  if is_standard
    return unless event_config
  else
    return unless contract_exists?(component_name)

    raise ValidationError, "Missing events schema for custom component: #{component_name}" unless contract.key?("props")
    raise ValidationError, "Missing event schema for '#{event_name}' in custom component: #{component_name}" unless event_config
  end

  schema = event_config.is_a?(Hash) ? (event_config["payload"] || {}) : {}

  validate!(schema, payload, "event '#{event_name}' for #{component_name}")
end

#validate_props(component_name, props) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/interfacets/test/validation_engine.rb', line 31

def validate_props(component_name, props)
  return unless active?

  contract = registry.find_component(component_name)
  is_standard = standard_element?(component_name)

  props_config = contract&.dig("props")

  if is_standard
    return unless props_config
  else
    return unless contract_exists?(component_name)

    raise ValidationError, "Missing props schema for custom component: #{component_name}" unless props_config
  end

  schema = { "type" => "object", "properties" => props_config }
  validate!(schema, props, "properties for #{component_name}")
end