Class: JsonValidation::Validators::AdditionalItems

Inherits:
Validator
  • Object
show all
Defined in:
lib/json_validation/validators/additional_items.rb

Instance Attribute Summary

Attributes inherited from Validator

#base_uri, #fragment

Instance Method Summary collapse

Methods inherited from Validator

#build_validator, #build_validator_with_new_base_uri, #initialize, type

Constructor Details

This class inherits a constructor from JsonValidation::Validators::Validator

Instance Method Details

#find_additional_items(fragment, record) ⇒ Object



29
30
31
# File 'lib/json_validation/validators/additional_items.rb', line 29

def find_additional_items(fragment, record)
  record.drop(fragment['items'].size)
end

#inner_validatorObject



25
26
27
# File 'lib/json_validation/validators/additional_items.rb', line 25

def inner_validator
  @inner_validator ||= build_validator(fragment["additionalItems"])
end

#validate(record) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/json_validation/validators/additional_items.rb', line 6

def validate(record)
  if !fragment.has_key?('items') || fragment['items'].is_a?(Hash)
    true
  else
    case fragment['additionalItems']
    when true
      true
    when false
      find_additional_items(fragment, record).empty?
    when Hash
      find_additional_items(fragment, record).all? {|item|
        inner_validator.validate(item)
      }
    else
      raise "Unexpected type for fragment['additionalItems']"
    end
  end
end