Sha256: 4a3338692ddd5759cd294074adfa60fe2e254655ac2b44a69991532790653a18

Contents?: true

Size: 723 Bytes

Versions: 2

Compression:

Stored size: 723 Bytes

Contents

class ArrayValidatorBase < ActiveModel::EachValidator
  def initialize(options)
    options[:allow_nil]   ||= false
    options[:allow_empty] ||= false

    super(options)
  end

  def validate_each(record, attribute, value)
    return if options[:allow_nil] && value.nil?

    unless value.is_a? Array
      record.errors[attribute] << "attribute #{attribute} must be an Array"
      return
    end

    if !options[:allow_empty] and value.empty?
      record.errors[attribute] << "attribute #{attribute} can't be empty"
      return
    end

    custom_validations(record, attribute, value)
  end

  def custom_validations(record, attribute, value)
    raise 'override this method to perform custom validations'
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
active_model_validators_ex-0.3.1 lib/active_model_validators_ex/array_validator_base.rb
active_model_validators_ex-0.3.0 lib/active_model_validators_ex/array_validator_base.rb