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



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

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

.create_head_for(steps:, step_type:) ⇒ Object



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

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

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



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

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:, using: ValidationStep) ⇒ Object



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

def get_step_for(validating:, using: ValidationStep)
  if validating.respond_to? :call
    using.new verify_with: validating
  elsif verifier = standard_validator(with: validating)
    using.new verify_with: verifier
  elsif schema = SchemaLibrary.get_schema(name: validating)
    schema
  else
    #TODO this needs a little finesse: How will the user know if it was a missing schema?
    raise UnknownValidationStepError.new(type: validating)
  end
end

.standard_validator(with:) ⇒ Object



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

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