Sha256: dcd88343ee2ce5ccbef5c3bc116644fe57819de2fef7df4123296ac9f7d2ea17

Contents?: true

Size: 1.89 KB

Versions: 7

Compression:

Stored size: 1.89 KB

Contents

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 #{type_of_data(data)} did not match any of the required schemas"
        else
          message = "The property '#{build_fragment(fragments)}' of type #{type_of_data(data)} 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

Version data entries

7 entries across 6 versions & 3 rubygems

Version Path
json-schema-ouidou-2.9.1 lib/json-schema/attributes/oneof.rb
json-schema-ouidou-2.9.0 lib/json-schema/attributes/oneof.rb
json-schema-3.0.0 lib/json-schema/attributes/oneof.rb
mountapi-0.11.1 vendor/bundle/ruby/2.7.0/bundler/gems/json-schema-2253a5ee6679/lib/json-schema/attributes/oneof.rb
mountapi-0.11.1 vendor/json-schema/lib/json-schema/attributes/oneof.rb
json-schema-2.8.1 lib/json-schema/attributes/oneof.rb
json-schema-2.8.0 lib/json-schema/attributes/oneof.rb