Sha256: ebb27fd9de473b260cef3d58c810c44935267b922abe8b517e0c3fd343ee2658

Contents?: true

Size: 648 Bytes

Versions: 1

Compression:

Stored size: 648 Bytes

Contents

# frozen_string_literal: true

require 'active_model'

class TypeValidator < ActiveModel::EachValidator
  def validate_each(record, attribute, value)
    strategy = fetch_strategy(options)

    raise Error::InvalidDefinition.new(attribute) unless strategy

    return unless error = strategy.invalid?(record, attribute, value, options)

    raise TypeError, "#{attribute} #{error}" if options[:strict]

    record.errors.add(attribute, error)
  end

  def fetch_strategy(options)
    KindOf if options.key?(:is_a) || options.key?(:kind_of)
  end
end

require 'type_validator/error'
require 'type_validator/kind_of'
require 'type_validator/version'

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
type_validator-0.2.0 lib/type_validator.rb