Class: JSON::Schema::AnyOfAttribute
- Defined in:
- lib/json-schema/attributes/anyof.rb
Constant Summary
Constants inherited from Attribute
JSON::Schema::Attribute::TYPE_CLASS_MAPPINGS
Class Method Summary collapse
Methods inherited from Attribute
build_fragment, data_valid_for_type?, type_of_data, 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 41 42 43 44 |
# File 'lib/json-schema/attributes/anyof.rb', line 6 def self.validate(current_schema, data, fragments, processor, validator, = {}) # Create a hash to hold errors that are generated during validation errors = Hash.new { |hsh, k| hsh[k] = [] } valid = false original_data = data.is_a?(Hash) ? data.clone : data current_schema.schema['anyOf'].each_with_index do |element, schema_index| 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, ) 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["anyOf ##{schema_index}"].push(validation_errors(processor).pop) end break if valid data = original_data end if !valid = "The property '#{build_fragment(fragments)}' of type #{type_of_data(data)} did not match one or more of the required schemas" validation_error(processor, , fragments, current_schema, self, [:record_errors]) validation_errors(processor).last.sub_errors = errors end end |