Class: ObjectMembersValidation

Inherits:
Object
  • Object
show all
Includes:
HashInitialized, Inspectable
Defined in:
lib/json_patterns.rb

Constant Summary

Constants included from Inspectable

Inspectable::INSPECTING_KEY

Class Method Summary collapse

Methods included from HashInitialized

#initialize

Methods included from Inspectable

#inspect

Class Method Details

.memoized_new_from_pattern(translation, pattern) ⇒ Object



518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
# File 'lib/json_patterns.rb', line 518

def self.memoized_new_from_pattern(translation, pattern)
  case pattern
  when Hash
    validation = pattern.to_a.reverse.reduce(EmptyObjectMembersValidation.new) do |es, e|
      (name, value) = e
      name = name.to_s if name.is_a? Symbol
      case name
      when String
        value_validation = Validation.memoized_new_from_pattern(translation, value)
        v = SingleObjectMemberValidation.new(name: name, value_validation: value_validation)
      when DisjunctionKey
        unless value.is_a?(Array)
          raise "one_of should only be used with an array"
        end
        vals = value.map { |pat|
          ObjectMembersFromObjectValidation.new(object_validation:
            Validation.memoized_new_from_pattern(translation, pat)
          )
        }
        v = ObjectMembersDisjunctionValidation.new(alternatives: vals)
      when OptionalKey
        v = OptionalObjectMembersValidation.new(members:
          ObjectMembersFromObjectValidation.new(object_validation:
            Validation.memoized_new_from_pattern(translation, value)
          )
        )
      when ManyKey
        v = ManyObjectMembersValidation.new(
          value_validation: Validation.memoized_new_from_pattern(translation, value)
        )
      when MembersKey
        v = ObjectMembersFromObjectValidation.new(
          object_validation: Validation.memoized_new_from_pattern(translation, value)
        )
      else
        raise "unrecognized key type in pattern: #{name.class}"
      end
      SequencedObjectMembersValidation.new(left: v, right: es)
    end
  else
    raise "cannot create object members validation from a #{pattern.class}"
  end
end