Class: EasyJSONMatcher::ValidationChainFactory

Inherits:
Object
  • Object
show all
Defined in:
lib/easy_json_matcher/validation_chain_factory.rb

Class Method Summary collapse

Class Method Details

.assemble_chain(head:, steps:, step_type:) ⇒ Object



15
16
17
18
19
# File 'lib/easy_json_matcher/validation_chain_factory.rb', line 15

def assemble_chain(head:, steps:, step_type:)
  steps.inject(head) do |last_link, step|
    last_link >> get_step_for(validating: step, uses: step_type)
  end
end

.create_head_for(steps:, step_type:) ⇒ Object



21
22
23
24
# File 'lib/easy_json_matcher/validation_chain_factory.rb', line 21

def create_head_for(steps:, step_type:)
  is_required = steps.delete(:required)
  get_step_for validating: is_required ? :required : :not_required, uses: step_type
end

.get_chain(steps:, of: ValidationStep) ⇒ Object



9
10
11
12
13
# File 'lib/easy_json_matcher/validation_chain_factory.rb', line 9

def get_chain(steps:, of: ValidationStep)
  head = create_head_for(steps: steps, step_type: of)
  assemble_chain(head: head, steps: steps, step_type: of)
  head
end

.get_step_for(validating:, uses: ValidationStep) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/easy_json_matcher/validation_chain_factory.rb', line 26

def get_step_for(validating:, uses: ValidationStep)
  if validating.respond_to? :call
    uses.new verify_with: validating
  elsif verifier = standard_validator(with: validating)
    uses.new verify_with: verifier
  elsif schema = SchemaLibrary.get_schema(name: validating)
    schema
  else
    #This is just here as a catch-all. The prior step should always succeed. 
    raise UnknownValidationStepError.new(type: validating)
  end
end

.standard_validator(with:) ⇒ Object



40
41
42
43
# File 'lib/easy_json_matcher/validation_chain_factory.rb', line 40

def self.standard_validator(with:)
  require "easy_json_matcher/validation_rules"
  VALIDATION_RULES[with]
end