Class: AMA::Entity::Mapper::Handler::Attribute::Validator

Inherits:
Object
  • Object
show all
Extended by:
Mixin::Reflection
Defined in:
lib/ama-entity-mapper/handler/attribute/validator.rb

Overview

Default validator for single attribute

Constant Summary collapse

INSTANCE =
new

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Mixin::Reflection

install_object_method, method_object, object_variable, object_variable_exists, object_variables, set_object_attribute

Methods included from Mixin::Errors

#compliance_error, #mapping_error, #raise_if_internal, #validation_error

Class Method Details

.wrap(validator) ⇒ Validator

Parameters:

Returns:



71
72
73
74
75
76
77
78
79
# File 'lib/ama-entity-mapper/handler/attribute/validator.rb', line 71

def wrap(validator)
  handler = handler_factory(validator, INSTANCE)
  description = "Safety wrapper for #{validator}"
  wrapper = method_object(:validate, to_s: description, &handler)
  wrapper.singleton_class.instance_eval do
    include Mixin::Errors
  end
  wrapper
end

Instance Method Details

#validate(value, attribute, _context) ⇒ Array<String>

Returns Single violation, list of violations.

Parameters:

Returns:

  • (Array<String>)

    Single violation, list of violations



19
20
21
22
# File 'lib/ama-entity-mapper/handler/attribute/validator.rb', line 19

def validate(value, attribute, _context)
  violation = validate_internal(value, attribute)
  violation.nil? ? [] : [violation]
end