Class: SingleObjectMemberValidation

Inherits:
ObjectMembersValidation show all
Defined in:
lib/json_patterns.rb

Constant Summary

Constants included from Inspectable

Inspectable::INSPECTING_KEY

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from ObjectMembersValidation

memoized_new_from_pattern

Methods included from HashInitialized

#initialize

Methods included from Inspectable

#inspect

Instance Attribute Details

#nameObject

Returns the value of attribute name.



745
746
747
# File 'lib/json_patterns.rb', line 745

def name
  @name
end

#value_validationObject

Returns the value of attribute value_validation.



745
746
747
# File 'lib/json_patterns.rb', line 745

def value_validation
  @value_validation
end

Instance Method Details

#first_value_match?(name, value) ⇒ Boolean

Returns:



759
760
761
762
# File 'lib/json_patterns.rb', line 759

def first_value_match?(name, value)
  return false unless name == @name
  @value_validation.shallow_match?(value)
end

#first_value_validationObject



751
752
753
# File 'lib/json_patterns.rb', line 751

def first_value_validation
  @value_validation
end

#first_value_validations(name) ⇒ Object



764
765
766
# File 'lib/json_patterns.rb', line 764

def first_value_validations(name)
  Set[@value_validation]
end

#matching_first_names(data) ⇒ Object



755
756
757
# File 'lib/json_patterns.rb', line 755

def matching_first_names(data)
  data.has_key?(@name) ? Set[@name] : Set[]
end

#possible_first_namesObject



747
748
749
# File 'lib/json_patterns.rb', line 747

def possible_first_names
  Set[@name]
end

#to_sObject



792
793
794
# File 'lib/json_patterns.rb', line 792

def to_s
  "\"#{@name}\": #{@value_validation}"
end

#validate_members(path, data) ⇒ Object



768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
# File 'lib/json_patterns.rb', line 768

def validate_members(path, data)
  if data.has_key? @name
    failures = @value_validation.validate(path + [@name], data[@name])
    remainder = data.dup
    remainder.delete @name
    return ObjectMembersValidationResult.new(
      failures: failures,
      remainder: remainder,
    )
  else
    found_names = data.empty? ?
      'end of object members' :
      "names: #{data.keys.map { |name| name.inspect }.join(', ')}"
    return ObjectMembersValidationResult.new(
      failures: [ValidationUnexpected.new(
        path: path,
        expected: "name: \"#@name\"",
        found: found_names,
      )],
      remainder: data,
    )
  end
end