Module: Modelish::Validations

Included in:
Base
Defined in:
lib/modelish/validations.rb

Overview

Mixin for validated properties

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#valid?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/modelish/validations.rb', line 36

def valid?
  validate.empty?
end

#validateHash<Symbol,Array>

Validates all properties based on configured validators.

Returns:

  • (Hash<Symbol,Array>)

    map of errors where key is the property name and value is the list of errors

See Also:



17
18
19
20
21
22
23
24
25
26
# File 'lib/modelish/validations.rb', line 17

def validate
  errors = {}

  call_validators do |name, message|
    errors[name] ||= []
    errors[name] << to_error(message)
  end

  errors
end

#validate!Object

Validates all properties based on configured validators.

Raises:

  • ArgumentError when any property fails validation



31
32
33
34
# File 'lib/modelish/validations.rb', line 31

def validate!
  errors = validate
  raise errors.first[1].first unless errors.empty?
end