Class: JSON::Schema::AdditionalPropertiesAttribute

Inherits:
Attribute
  • Object
show all
Defined in:
lib/json-schema/attributes/additionalproperties.rb

Class Method Summary collapse

Methods inherited from Attribute

build_fragment, validation_error, validation_errors

Class Method Details

.remove_valid_properties(extra_properties, current_schema, validator) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/json-schema/attributes/additionalproperties.rb', line 34

def self.remove_valid_properties(extra_properties, current_schema, validator)

    if current_schema.schema['properties']
      extra_properties = extra_properties - current_schema.schema['properties'].keys
    end

    if current_schema.schema['patternProperties']
      current_schema.schema['patternProperties'].each_key do |key|
        r = Regexp.new(key)
        extras_clone = extra_properties.clone
        extras_clone.each do |prop|
          if r.match(prop)
            extra_properties = extra_properties - [prop]
          end
        end
      end
    end

    if schemas= current_schema.schema['extends']
      schemas = [schemas] if !schemas.is_a?(Array)
      schemas.each do |schema_value|
        temp_uri,extended_schema= JSON::Schema::ExtendsAttribute.get_extended_uri_and_schema(schema_value, current_schema, validator)
        if extended_schema
          extra_properties= remove_valid_properties(extra_properties, extended_schema, validator)
        end
      end
    end

    extra_properties
end

.validate(current_schema, data, fragments, processor, validator, options = {}) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/json-schema/attributes/additionalproperties.rb', line 6

def self.validate(current_schema, data, fragments, processor, validator, options = {})
  if data.is_a?(Hash) && (
    current_schema.schema['type'].nil? || (
      current_schema.schema['type'].is_a?(String) &&
      current_schema.schema['type'].downcase == 'object'
    )
  )
    extra_properties = data.keys
    extra_properties = remove_valid_properties(extra_properties, current_schema, validator)

    addprop = current_schema.schema['additionalProperties']
    if addprop.is_a?(Hash)
      matching_properties= extra_properties # & addprop.keys
      matching_properties.each do |key|
        schema = JSON::Schema.new(addprop[key] || addprop, current_schema.uri, validator)
        fragments << key
        schema.validate(data[key],fragments,processor,options)
        fragments.pop
      end
      extra_properties -= matching_properties
    end
    if !extra_properties.empty? and (addprop == false or (addprop.is_a?(Hash) and !addprop.empty?))
      message = "The property '#{build_fragment(fragments)}' contains additional properties #{extra_properties.inspect} outside of the schema when none are allowed"
      validation_error(processor, message, fragments, current_schema, self, options[:record_errors])
    end
  end
end