Sha256: 03379aff0eb9dcc40e49876472e805fe692be4d3b2feee64b90e43779908e381

Contents?: true

Size: 1.6 KB

Versions: 22

Compression:

Stored size: 1.6 KB

Contents

module JSON
  class Schema
    class AnyOfAttribute < Attribute
      def self.validate(current_schema, data, fragments, processor, validator, options = {})
        # Create an array to hold errors that are generated during validation
        errors = []
        valid = false

        current_schema.schema['anyOf'].each do |element|
          schema = JSON::Schema.new(element,current_schema.uri,validator)

          # We're going to add a little cruft here to try and maintain any validation errors that occur in the anyOf
          # We'll handle this by keeping an error count before and after validation, extracting those errors and pushing them onto a union error
          pre_validation_error_count = validation_errors(processor).count

          begin
            schema.validate(data,fragments,processor,options)
            valid = true
          rescue ValidationError
            # We don't care that these schemas don't validate - we only care that one validated
          end

          diff = validation_errors(processor).count - pre_validation_error_count
          valid = false if diff > 0
          while diff > 0
            diff = diff - 1
            errors.push(validation_errors(processor).pop)
          end

          break if valid
        end

        if !valid
          message = "The property '#{build_fragment(fragments)}' of type #{data.class} did not match one or more of the required schemas"
          validation_error(processor, message, fragments, current_schema, self, options[:record_errors])
          validation_errors(processor).last.sub_errors = errors
        end
      end
    end
  end
end

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
json-schema-2.2.5 lib/json-schema/attributes/anyof.rb
json-schema-2.2.4 lib/json-schema/attributes/anyof.rb
json-schema-2.2.3 lib/json-schema/attributes/anyof.rb
json-schema-2.2.2 lib/json-schema/attributes/anyof.rb
json-schema-2.2.1 lib/json-schema/attributes/anyof.rb
json-schema-2.2.0 lib/json-schema/attributes/anyof.rb
json-schema-2.1.9 lib/json-schema/attributes/anyof.rb
json-schema-2.1.8 lib/json-schema/attributes/anyof.rb
json-schema-2.1.7 lib/json-schema/attributes/anyof.rb
json-schema-2.1.6 lib/json-schema/attributes/anyof.rb
json-schema-2.1.5 lib/json-schema/attributes/anyof.rb
json-schema-2.1.4 lib/json-schema/attributes/anyof.rb
json-schema-2.1.3 lib/json-schema/attributes/anyof.rb
json-schema-2.1.2 lib/json-schema/attributes/anyof.rb
json-schema-2.1.1 lib/json-schema/attributes/anyof.rb
json-schema-2.1.0 lib/json-schema/attributes/anyof.rb
json-schema-2.0.5 lib/json-schema/attributes/anyof.rb
json-schema-2.0.4 lib/json-schema/attributes/anyof.rb
json-schema-2.0.3 lib/json-schema/attributes/anyof.rb
json-schema-2.0.2 lib/json-schema/attributes/anyof.rb