class AMA::Entity::Mapper::Handler::Attribute::Validator
Default validator for single attribute
Constants
- INSTANCE
Private Class Methods
handler_factory(validator, fallback)
click to toggle source
@param [Validator] validator @param [Validator] fallback @return [Proc]
# File lib/ama-entity-mapper/handler/attribute/validator.rb, line 86 def handler_factory(validator, fallback) lambda do |val, attr, ctx| begin validator.validate(val, attr, ctx) do |v, a, c| fallback.validate(v, a, c) end rescue StandardError => e raise_if_internal(e) message = "Unexpected error from validator #{validator}" signature = '(value, attribute, context)' options = { parent: e, context: ctx, signature: signature } compliance_error(message, **options) end end end
wrap(validator)
click to toggle source
@param [Validator] validator @return [Validator]
# 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
Public Instance Methods
validate(value, attribute, _context)
click to toggle source
@param [Object] value Attribute
value @param [AMA::Entity::Mapper::Type::Attribute] attribute @param [AMA::Entity::Mapper::Context] _context @return [Array<String>] Single violation, list of violations
# 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
Private Instance Methods
illegal_nil?(value, attribute)
click to toggle source
@param [Object] value Attribute
value @param [AMA::Entity::Mapper::Type::Attribute] attribute @return [TrueClass, FalseClass]
# File lib/ama-entity-mapper/handler/attribute/validator.rb, line 43 def illegal_nil?(value, attribute) return false unless value.nil? && !attribute.nullable attribute.types.none? { |type| type.instance?(value) } end
illegal_value?(value, attribute)
click to toggle source
@param [Object] value Attribute
value @param [AMA::Entity::Mapper::Type::Attribute] attribute @return [TrueClass, FalseClass]
# File lib/ama-entity-mapper/handler/attribute/validator.rb, line 60 def illegal_value?(value, attribute) return false if value == attribute.default return false if attribute.values.empty? || attribute.values.nil? !attribute.values.include?(value) end
invalid_type?(value, attribute)
click to toggle source
@param [Object] value Attribute
value @param [AMA::Entity::Mapper::Type::Attribute] attribute @return [TrueClass, FalseClass]
# File lib/ama-entity-mapper/handler/attribute/validator.rb, line 51 def invalid_type?(value, attribute) attribute.types.all? do |type| !type.respond_to?(:instance?) || !type.instance?(value) end end
validate_internal(value, attribute)
click to toggle source
# File lib/ama-entity-mapper/handler/attribute/validator.rb, line 26 def validate_internal(value, attribute) if illegal_nil?(value, attribute) return "Attribute #{attribute} could not be nil" end if invalid_type?(value, attribute) return "Provided value doesn't conform to " \ "any of attribute #{attribute} types " \ "(#{attribute.types.map(&:to_def).join(', ')})" end return unless illegal_value?(value, attribute) "Provided value doesn't match default value (#{value})" \ " or any of allowed values (#{attribute.values})" end