Class: JsonValidation::Validators::AdditionalProperties

Inherits:
Validator
  • Object
show all
Defined in:
lib/json_validation/validators/additional_properties.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_properties(fragment, record) ⇒ Object



25
26
27
28
29
30
31
32
33
34
# File 'lib/json_validation/validators/additional_properties.rb', line 25

def find_additional_properties(fragment, record)
  record.reject {|k, v|
    fragment.fetch('properties', {}).keys.include?(k)
  }.reject {|k, v|
    fragment.fetch('patternProperties', {}).keys.any? {|pattern|
      rx = Regexp.new(pattern)
      rx.match(k)
    }
  }
end

#inner_validatorObject



21
22
23
# File 'lib/json_validation/validators/additional_properties.rb', line 21

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

#validate(record) ⇒ Object



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

def validate(record)
  case fragment['additionalProperties']
  when true
    true
  when false
    find_additional_properties(fragment, record).empty?
  when Hash
    find_additional_properties(fragment, record).values.all? {|value|
      inner_validator.validate(value)
    }
  else
    raise "Unexpected type for fragment['additionalProperties']"
  end
end