Class: JSON::Schema::OneOfAttribute

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

Constant Summary

Constants inherited from Attribute

Attribute::TYPE_CLASS_MAPPINGS

Class Method Summary collapse

Methods inherited from Attribute

build_fragment, data_valid_for_type?, validation_error, validation_errors

Class Method Details

.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
33
34
35
36
37
38
39
40
# File 'lib/json-schema/attributes/oneof.rb', line 6

def self.validate(current_schema, data, fragments, processor, validator, options = {})
  validation_errors = 0
  one_of = current_schema.schema['oneOf']

  original_data = data.is_a?(Hash) ? data.clone : data
  success_data = nil

  one_of.each do |element|
    schema = JSON::Schema.new(element,current_schema.uri,validator)

    begin
      # need to raise exceptions on error because
      # schema.validate doesn't reliably return true/false
      schema.validate(data,fragments,processor,options.merge(:record_errors => false))
      success_data = data.is_a?(Hash) ? data.clone : data
    rescue ValidationError
      validation_errors += 1
    end

    data = original_data
  end

  if validation_errors == one_of.length - 1
    data = success_data
    return
  end

  if validation_errors == one_of.length
    message = "The property '#{build_fragment(fragments)}' of type #{data.class} did not match any of the required schemas"
  else
    message = "The property '#{build_fragment(fragments)}' of type #{data.class} matched more than one of the required schemas"
  end

  validation_error(processor, message, fragments, current_schema, self, options[:record_errors]) if message
end