Sha256: ec61363ed995928908130357d2167143e13fea685083d3baf655cffd1fb6eab0
Contents?: true
Size: 785 Bytes
Versions: 1
Compression:
Stored size: 785 Bytes
Contents
# frozen_string_literal: true require 'active_model' require 'type_validator/version' class TypeValidator < ActiveModel::EachValidator VERSION = TYPE_VALIDATOR_VERSION INVALID_DEFINITION = ArgumentError.new( 'invalid type definition. Options to define one: `:is_a` or `:kind_of`' ) def validate_each(record, attribute, value) types = Array(options[:is_a] || options[:kind_of]).flatten allow_nil = options[:allow_nil] raise INVALID_DEFINITION if types.empty? return if (allow_nil && value.nil?) || types.any? { |type| value.is_a?(type) } message = "must be a kind of: #{types.map(&:name).join(', ')}" if options[:strict] raise TypeError, "#{attribute} #{message}" else record.errors.add(attribute, message) end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
type_validator-0.1.0 | lib/type_validator.rb |