GithubHelp home page GithubHelp logo

valle's Introduction

Valle Build Status Code Climate

Valle automatically creates validations for the minimum and maximum values of fields in your ActiveRecord model(s). No more worrying that string lengths or ID values will exceed the permissible DB limits!

For example, the maximum length of the string type in PostgreSQL is 255. Valle creates the following validator for you, so you no longer need to write it by hand:

validates :field_name, length: { maximum: 255 }

Note: If you do not do this (and usually you are) and try to enter 2147483648 into a field of type integer (see the Numeric types section of PostgreSQL docs), you will get a 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

Rails versions currently supported

  • 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:

$ gem install valle

Usage

By default, this gem adds validators to all your ActiveRecord models. If that is the behavior you want, you don't need to tweak it.

However, you can specify what models to take into account by adding the file config/initializers/valle.rb containing:

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

Also, you can disable it temporarily by setting the enabled configuration option to false.

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

Disabling Valle on specific attributes

There are cases where you need to skip validation for a particular attribute (see #4). For example, CarrierWave stores images temporarily in attributes, so calling save on them will fail because of its LengthValidator (255 characters maximum). You can disable Valle for such fields using the attributes configuration 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 of the problem โ€” applicable to strings. Valle, however, 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.

valle's People

Contributors

melekes avatar akirill0v avatar radanisk avatar andrew8xx8 avatar marcusg avatar mokevnin avatar plugin73 avatar oddlyzen avatar qblake avatar

Watchers

Johan Kok avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.