Simple Range validator

Validate fields of type Range. Also contains simple validators for Array values and types respectively ;)

Install

gem ‘simple_range_validator’

Example usage

Here using Mongoid’s built in Range field support ;)

class Search
  include Mongoid::Document

  field :cost, type: Range

  validates :cost, range:  {within: 0..40000 }
end

Search.new cost: (0..2000)

And here using factory_girl gem:

FactoryGirl.define do
  sequence(:cost_range) { [ (0..2000), (2000..4000), (4000..8000) ].sample }

  factory :search do
    cost  { FactoryGirl.generate(:cost_range) }
  end
end

Create a Search instance via factory (requires Factory Girl macros enabled):

create :search

Notes

Also check out github.com/chrisb87/range_validator

Could make sense to integrate both of these gems ;)

Array Validator

class Search
  include Mongoid::Document

  field :color, type: Array

  # you can also use only: as the option
  validates :color, array:  {in: ['red', 'blue', 'green'] }
end

Array Type Validator

class Search
  include Mongoid::Document

  field :color, type: Array

  # you can also use in: as the option
  validates :color, array_types:  {only: [String, Fixnum] }
end

TimeSpan Validator

class Search
  include Mongoid::Document

  field :period, type: Timespan

  # you can also use in: as the option
  validates :period, timespan:  {from: 1.day.ago, to: 14.days.from_now }
end

Simple as pie ;)

Contributing to range_validator

Copyright © 2012 Kristian Mandrup. See LICENSE.txt for further details.