Module: Moon::DataModel::TypeValidators::Base

Includes:
MessageFormat
Included in:
Null, Soft
Defined in:
lib/data_model/type_validators/base.rb

Overview

Base module for all TypeValidators

Instance Method Summary collapse

Methods included from MessageFormat

#format_err

Instance Method Details

#check_type(type, value, options = {}) ⇒ Boolean

Checks the value against the provided type

Parameters:

  • type (Type)
  • value (Object)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :quiet (Boolean)

    suppress raise errors

Returns:

  • (Boolean)

Raises:


38
39
40
41
42
43
# File 'lib/data_model/type_validators/base.rb', line 38

def check_type(type, value, options = {})
  ensure_obj_is_a_type type
  passed, msg = do_check type, value, options
  return passed, msg if passed || options[:quiet]
  raise TypeValidationError, msg
end

#matches_any_type?(types, value, options = {}) ⇒ Boolean

Checks if the given value matches any of the provided types

Parameters:

  • types (Array<Type>)
  • value (Object)
  • options (Hash) (defaults to: {})

Returns:

  • (Boolean)

51
52
53
54
55
56
57
# File 'lib/data_model/type_validators/base.rb', line 51

def matches_any_type?(types, value, options = {})
  opts = { quiet: true, allow_nil: false }.merge(options)
  types.any? do |t|
    b, _ = *check_type(t, value, opts)
    b
  end
end