Class: Axlsx::DataTypeValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/axlsx/util/validators.rb

Overview

Validate that the class of the value provided is either an instance or the class of the allowed types and that any specified additional validation returns true.

Class Method Summary collapse

Class Method Details

.validate(name, types, v, other = false) ⇒ Boolean

Perform validation

Parameters:

  • name (String)

    The name of what is being validated. This is included in the error message

  • types (Array, Class)

    A single class or array of classes that the value is validated against.

  • other (Block) (defaults to: false)

    Any block that must evaluate to true for the value to be valid

Returns:

  • (Boolean)

    true if validation succeeds.

Raises:

  • (ArugumentError)

    Raised if the class of the value provided is not in the specified array of types or the block passed returns false

See Also:



57
58
59
60
61
62
63
64
65
66
# File 'lib/axlsx/util/validators.rb', line 57

def self.validate(name, types, v, other = false)
  if other.is_a?(Proc) && !other.call(v)
    raise ArgumentError, format(ERR_TYPE, v.inspect, name, types.inspect)
  end

  v_class = v.is_a?(Class) ? v : v.class
  return if Array(types).any? { |t| v_class <= t }

  raise ArgumentError, format(ERR_TYPE, v.inspect, name, types.inspect)
end