File: oneof.rb

package info (click to toggle)
ruby-json-schema 2.7.0-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 748 kB
  • ctags: 656
  • sloc: ruby: 5,380; makefile: 6
file content (56 lines) | stat: -rw-r--r-- 1,918 bytes parent folder | download
1
2
3
4
5
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
require 'json-schema/attribute'

module JSON
  class Schema
    class OneOfAttribute < Attribute
      def self.validate(current_schema, data, fragments, processor, validator, options = {})
        errors = Hash.new { |hsh, k| hsh[k] = [] }

        validation_error_count = 0
        one_of = current_schema.schema['oneOf']

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

        valid = false

        one_of.each_with_index do |element, schema_index|
          schema = JSON::Schema.new(element,current_schema.uri,validator)
          pre_validation_error_count = validation_errors(processor).count
          begin
            schema.validate(data,fragments,processor,options)
            success_data = data.is_a?(Hash) ? data.clone : data
            valid = true
          rescue ValidationError
            valid = false
          end

          diff = validation_errors(processor).count - pre_validation_error_count
          valid = false if diff > 0
          validation_error_count += 1 if !valid
          while diff > 0
            diff = diff - 1
            errors["oneOf ##{schema_index}"].push(validation_errors(processor).pop)
          end
          data = original_data
        end



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

        if validation_error_count == 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
        validation_errors(processor).last.sub_errors = errors if message
      end
    end
  end
end