Class: ValueDisjunctionValidation

Inherits:
Validation show all
Defined in:
lib/json_patterns.rb

Constant Summary

Constants included from Inspectable

Inspectable::INSPECTING_KEY

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Validation

#as_object_members, #expects_an_object?, memoized_new_from_pattern, new_from_pattern, #validate_from_root

Methods included from HashInitialized

#initialize

Methods included from DeepEquality

#==

Methods included from Inspectable

#inspect

Instance Attribute Details

#alternativesObject

Returns the value of attribute alternatives.



385
386
387
# File 'lib/json_patterns.rb', line 385

def alternatives
  @alternatives
end

Instance Method Details

#shallow_describeObject



411
412
413
# File 'lib/json_patterns.rb', line 411

def shallow_describe
  set_union(@alternatives.map { |v| v.shallow_describe })
end

#shallow_match?(data) ⇒ Boolean

Returns:



406
407
408
409
# File 'lib/json_patterns.rb', line 406

def shallow_match?(data)
  matching = @alternatives.select { |v| v.shallow_match? data }
  return matching.length == 1
end

#to_sObject



415
416
417
# File 'lib/json_patterns.rb', line 415

def to_s
  '(' + (@alternatives.map { |v| v.to_s }).join(' | ') + ')'
end

#validate(path, data) ⇒ Object



387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
# File 'lib/json_patterns.rb', line 387

def validate(path, data)
  matching = @alternatives.select { |v| v.shallow_match? data }
  if matching.length == 0
    return [ValidationUnexpected.new(
      path: path,
      found: shallow_value(data),
      expected: shallow_describe,
    )]
  elsif matching.length == 1
    return matching[0].validate(path, data)
  else
    return [ValidationAmbiguity.new(
      path: path,
      found: shallow_value(data),
      overlapping_patterns: matching.flat_map { |v| v.shallow_describe.to_a },
    )]
  end
end