Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/kaize/validates
https://github.com/kaize/validates
Last synced: about 2 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/kaize/validates
- Owner: kaize
- License: mit
- Created: 2011-11-16T16:19:04.000Z (about 13 years ago)
- Default Branch: master
- Last Pushed: 2017-01-13T11:02:51.000Z (almost 8 years ago)
- Last Synced: 2024-09-18T01:35:40.188Z (3 months ago)
- Language: Ruby
- Homepage:
- Size: 66.4 KB
- Stars: 153
- Watchers: 21
- Forks: 27
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
- awesome-rails-gem - Validates - Validates provides collection of useful custom validators for Rails applications, including: (Active Record / Omniauth)
README
# Validates [![Gem Version](https://badge.fury.io/rb/validates.png)](http://badge.fury.io/rb/validates) [![Build Status](https://travis-ci.org/kaize/validates.png)](https://travis-ci.org/kaize/validates) [![Code Climate](https://codeclimate.com/github/kaize/validates.png)](https://codeclimate.com/github/kaize/validates)
Collection of useful custom validators for Rails applications, including:
- AbsolutePathValidator
- AssociationLengthValidator
- ColorValidator
- EanValidator ([EAN-8 & EAN-13](http://en.wikipedia.org/wiki/International_Article_Number_(EAN)))
- EmailValidator
- IpValidator
- MoneyValidator
- SlugValidator
- UriComponentValidator
- UrlValidator**Note** InnValidator and other Russian specific validators could be found at [validates_russian](https://github.com/asiniy/validates_russian) gem
## Installation
Add this line to your application's Gemfile:
``` ruby
gem 'validates'
```Or install it yourself as:
``` bash
$ gem install 'validates'
```## Usage
For most of the validators you just want to add this line to your model:
``` ruby
validates :attribute, : true
```
where `` is an underscored, lowercase form from the validator's name (see the examples section below).### AssociationLengthValidator
Because this is the successor of ActiveModel::Validations::LengthValidator
validator, it inherits all the options of the latter, such as `:is`, `:minimum`,
`:maximum`, etc. Another option, which you may be interested in is `:select` option,
which allows you to filter the collection of the associated objects.## Examples
``` ruby
class User < ActiveRecord::Base
validates :email, :email => true
validates :site, :url => true, :allow_blank => true
endclass Company < ActiveRecord::Base
# note AssociationLengthValidator is inherited from ActiveModel::Validations::LengthValidator
# http://api.rubyonrails.org/classes/ActiveModel/Validations/LengthValidator.html
# so you can easily use standard options like :is, :minimum, :maximum, etc.validates :employees,
:association_length => {
:minimum => 1,
:select => ->(employee) { employee.name.in? ["Mike", "John"] }
}validates :employees, :association_length => { :minimum => 1, :select => :employees_filter }
def employees_filter(employees)
employees.select { |employee| employee.name.in? ["Mike", "John"] }
end
endclass Page < ActiveRecord::Base
validates :slug, :slug => true
endclass Content < ActiveRecord::Base
# Validates URI component.
# URI component must be of the following type:
# :ABS_URI, :REL_URI, :URI_REF, :ABS_URI_REF, :REL_URI_REF, :ESCAPED, :UNSAFE, :SCHEME,
# :USERINFO, :HOST, :PORT, :OPAQUE, :REGISTRY, :ABS_PATH, :REL_PATH, :QUERY or :FRAGMENT.
# These types are provided URI library. For more info see URI::DEFAULT_PARSER.regexp.validates :path, :uri_component => { :component => :ABS_PATH }
end```
## Contributing
Please see [CONTRIBUTING.md](CONTRIBUTING.md) for details.
## Credits
Originally written by Mikhail Stolbov. Maintained by kaize.
Thank you to all our amazing [contributors](http://github.com/kaize/validates/contributors)!
## License
validates is Copyright © 2012-2014 Mikhail Stolbov and kaize. It is free
software, and may be redistributed under the terms specified in the LICENSE
file.