Class: ObjectValidation

Inherits:
Validation 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 Validation

memoized_new_from_pattern, new_from_pattern, #validate_from_root

Methods included from HashInitialized

#initialize

Methods included from DeepEquality

#==

Methods included from Inspectable

#inspect

Instance Attribute Details

#membersObject

Returns the value of attribute members.



471
472
473
# File 'lib/json_patterns.rb', line 471

def members
  @members
end

Instance Method Details

#as_object_membersObject



510
511
512
# File 'lib/json_patterns.rb', line 510

def as_object_members
  @members
end

#expects_an_object?Boolean

Returns:



506
507
508
# File 'lib/json_patterns.rb', line 506

def expects_an_object?
  true
end

#shallow_describeObject



498
499
500
# File 'lib/json_patterns.rb', line 498

def shallow_describe
  Set['object']
end

#shallow_match?(data) ⇒ Boolean

Returns:



494
495
496
# File 'lib/json_patterns.rb', line 494

def shallow_match?(data)
  data.is_a? Hash
end

#to_sObject



502
503
504
# File 'lib/json_patterns.rb', line 502

def to_s
  "{ #{@members} }"
end

#validate(path, data) ⇒ Object



473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
# File 'lib/json_patterns.rb', line 473

def validate(path, data)
  if data.is_a? Hash
    result = @members.validate_members(path, data)
    failures = result.failures
    if result.remainder.length > 0
      failures << ValidationUnexpected.new(
        path: path,
        expected: 'end of object members',
        found: "names: " + result.remainder.keys.map { |name| name.inspect }.join(', ')
      )
    end
    return failures
  else
    return [ValidationUnexpected.new(
      path: path,
      expected: 'object',
      found: json_type_name(data),
    )]
  end
end