class AMA::Entity::Mapper::Handler::Entity::Validator

Default entity validator

Constants

INSTANCE

Public Class Methods

wrap(validator) click to toggle source

@param [Validator] validator @return [Validator]

# File lib/ama-entity-mapper/handler/entity/validator.rb, line 30
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

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/entity/validator.rb, line 45
def handler_factory(validator, fallback)
  lambda do |entity, type, ctx|
    begin
      validator.validate(entity, type, ctx) do |e, t, c|
        fallback.validate(e, t, c)
      end
    rescue StandardError => e
      raise_if_internal(e)
      message = "Unexpected error from validator #{validator}"
      signature = '(entity, type, context)'
      options = { parent: e, context: ctx, signature: signature }
      compliance_error(message, **options)
    end
  end
end

Public Instance Methods

validate(entity, type, _context) click to toggle source

@param [Object] entity @param [Mapper::Type] type @param [Mapper::Context] _context @return [Array<Array<Attribute, String, Segment>] List of

violations, combined with attribute and segment
# File lib/ama-entity-mapper/handler/entity/validator.rb, line 20
def validate(entity, type, _context)
  return [] if type.instance?(entity)
  ["Provided object is not an instance of #{type.type}"]
end