Sha256: febf867fc363d13aab2379300bfc45d1d6af85228b7218f114628f157fce12ef

Contents?: true

Size: 728 Bytes

Versions: 1

Compression:

Stored size: 728 Bytes

Contents

class JSONModel::Validations::ArrayValidator < ActiveModel::EachValidator
  def validate_each(record, attribute, value)
    unless value.is_a?(Array)
      record.errors.add(attribute, :not_an_array)
      return
    end
    
    if options[:type]
      record.errors.add(attribute, :invalid_item_type) unless value.all? do |item|
        item.is_a?(JSONModel::Types[options[:type]])
      end
    end
    
    if options[:max_items]
      record.errors.add(attribute, :too_many_items, :count => options[:max_items]) if value.size > options[:max_items]
    end
    
    if options[:min_items]
      record.errors.add(attribute, :too_few_items, :count => options[:min_items]) if value.size < options[:min_items]
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
jsonmodel-0.0.1 lib/jsonmodel/validations/array_validator.rb