Class: Schash::Schema::Rule::OneOfTypes

Inherits:
Base
  • Object
show all
Defined in:
lib/schash/schema/rule/one_of_types.rb

Instance Method Summary collapse

Methods inherited from Base

#optional?

Constructor Details

#initialize(*klasses) ⇒ OneOfTypes

Returns a new instance of OneOfTypes.



5
6
7
# File 'lib/schash/schema/rule/one_of_types.rb', line 5

def initialize(*klasses)
  @klasses = klasses
end

Instance Method Details

#validate(target, position = []) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/schash/schema/rule/one_of_types.rb', line 9

def validate(target, position = [])
  match = @klasses.any? do |klass|
    target.is_a?(klass)
  end

  if match
    []
  else
    [Error.new(position, "is not any of #{@klasses.map(&:to_s).join(', ')}")]
  end
end