Class: Errapi::Validations::Type
- Defined in:
- lib/errapi/validations/type.rb
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ Type
constructor
A new instance of Type.
- #validate(value, context, options = {}) ⇒ Object
Methods inherited from Base
#actual_option_value, #callable_option_type_error, #callable_option_value?, #callable_option_value_error, #exactly_one_option?
Constructor Details
#initialize(options = {}) ⇒ Type
Returns a new instance of Type.
4 5 6 7 8 9 10 11 12 13 14 15 |
# File 'lib/errapi/validations/type.rb', line 4 def initialize = {} unless key = exactly_one_option?(OPTIONS, ) raise ArgumentError, "One option among :instance_of, :kind_of, :is_a or :is_an must be supplied (but only one)." end if key == :instance_of @instance_of = check_types! [key] raise ArgumentError, "Type aliases cannot be used with the :instance_of option. Use :kind_of, :is_a or :is_an." if [key].kind_of? Symbol else @kind_of = check_types! [key] end end |
Instance Method Details
#validate(value, context, options = {}) ⇒ Object
17 18 19 20 21 22 23 |
# File 'lib/errapi/validations/type.rb', line 17 def validate value, context, = {} if @instance_of && @instance_of.none?{ |type| value.instance_of? type } context.add_error reason: :wrong_type, check_value: @instance_of, checked_value: value.class elsif @kind_of && @kind_of.none?{ |type| value.kind_of? type } context.add_error reason: :wrong_type, check_value: @kind_of, checked_value: value.class end end |