oddlyzen / valle

Built-in limit validations for your ActiveRecord model

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Valle Build Status Code Climate

Valle automatically sets minimum and maximum values for the fields of your ActiveRecord model(s), so you shouldn't worry, that string length or ID value will exceed the permissible DB limit for this type of field.

For example, maximum length of the string in PostgreSQL is 255. We will setup the following validator for you, so you don't need to write it by hands.

validates :field_name, length: { maximum: 255 }

Note: If you do not do this (and usually you are) and try to enter 2147483648 into the field with type: integer (see Numeric types section of PostgreSQL docs), you will get 500 error.

Example:

PG::Error: ERROR:  value "2147483648" is out of range for type integer
: SELECT  "users".* FROM "users"  WHERE "users"."id" = $1 LIMIT 1

Supported Rails

  • 3.x
  • 4.x

Supported ActiveRecord field types

  • :primary_key
  • :integer
  • :string
  • :text

Installation

Add this line to your application's Gemfile:

gem 'valle'

And then execute:

$ bundle

Or install it yourself as:

$ gem install valle

Usage

By default, this gem adds validators to all your ActiveRecord models. This means that, basically, you don't need to tweak it.

However, you could tell him directly what models it should take into account by adding config/initializers/valle.rb:

Valle.configure do |config|
  config.models = %w(User, Post)
end

Also, you should be able to turn it off temporary by setting enabled option to false.

Valle.configure do |config|
  config.enabled = false
end

Disabling gem for some attributes

There are cases where you need to skip validation for a particular attribute (see #4). For example, CarrierWave stores his images temporarily in attributes, so calling save on them will fail because of LengthValidator (255 chars maximum). You can disable this gem for such fields using attributes option.

Valle.configure do |config|
  config.attributes = {
    'User' => %w(id name) # Model => Array of attributes to validate
  }
end

Alternatives

There is a similar gem, called validates_lengths_from_database. It solves only one part - applicable to strings. This gem is designed to work with all possible field types.

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Added some feature')
  4. Run test suite (rake test_suite)
  5. Push to the branch (git push origin my-new-feature)
  6. Create new Pull Request

Credits

Team members:

Thank you to all our amazing contributors!

License

Valle is released under the MIT License.

About

Built-in limit validations for your ActiveRecord model

License:MIT License


Languages

Language:Ruby 100.0%