franckverrot / activevalidators Goto Github PK
View Code? Open in Web Editor NEWCollection of ActiveModel/ActiveRecord validators
Home Page: https://rubygems.org/gems/activevalidators
License: MIT License
Collection of ActiveModel/ActiveRecord validators
Home Page: https://rubygems.org/gems/activevalidators
License: MIT License
ActiveValidators supports phone numbers. We could use the value part of the validator hash as mentioned here to write something like:
validates :phone, :phone => { :country => :france }
validates :phone, :phone => { :country => :uk }
validates :phone, :phone => { :country => :usa }
...
It would be nice to have host validator if for example you want to make sure the url belongs to youtube.com.
Thanks!
An error should not be thrown if the field is blank. This should be handled with :presence validator.
A value of 000 000 000 will currently pass validation as it is a valid Luhn number but not a valid SIN.
I just noticed that http://mywebsite.
passes url validation
If the validation does not check for the card type we should not pass a hash
validates :card, :credit_card => true
instead of
validates :card, :credit_card => { :type => :any }
Validators could look like
validates :ccn, :credit_card => { :type => :visa }
Per country?
YARD to the rescue
User Model
class User < ActiveRecord::Base
devise :invitable, :database_authenticatable, :registerable, :confirmable,
:recoverable, :rememberable, :trackable, :validatable
# other code...
validates :email, email: true
end
User Spec
describe User do
before(:each) { @user = User.new }
# Validations
it { expect(@user).to validate_presence_of :username }
it { expect(@user).to validate_uniqueness_of :username }
it { expect(@user).to validate_presence_of :full_name }
it { expect(@user).to validate_presence_of :role }
it { expect(@user).to ensure_length_of(:password).is_at_least(8) } #FAILS
end
Call Stack
Failure/Error: it { expect(@user).to ensure_length_of(:password).is_at_least(8) }
TypeError:
can't dup NilClass
# /home/nadeem/.rvm/gems/ruby-2.1.2@orbitapp/gems/shoulda-matchers-2.7.0/lib/shoulda/matchers/active_model/helpers.rb:8:in `dup'
# /home/nadeem/.rvm/gems/ruby-2.1.2@orbitapp/gems/shoulda-matchers-2.7.0/lib/shoulda/matchers/active_model/helpers.rb:8:in `block in pretty_error_messages'
# /home/nadeem/.rvm/gems/ruby-2.1.2@orbitapp/gems/activemodel-4.1.5/lib/active_model/errors.rb:162:in `block (2 levels) in each'
# /home/nadeem/.rvm/gems/ruby-2.1.2@orbitapp/gems/activemodel-4.1.5/lib/active_model/errors.rb:162:in `each'
# /home/nadeem/.rvm/gems/ruby-2.1.2@orbitapp/gems/activemodel-4.1.5/lib/active_model/errors.rb:162:in `block in each'
# /home/nadeem/.rvm/gems/ruby-2.1.2@orbitapp/gems/activemodel-4.1.5/lib/active_model/errors.rb:161:in `each_key'
# /home/nadeem/.rvm/gems/ruby-2.1.2@orbitapp/gems/activemodel-4.1.5/lib/active_model/errors.rb:161:in `each'
# /home/nadeem/.rvm/gems/ruby-2.1.2@orbitapp/gems/shoulda-matchers-2.7.0/lib/shoulda/matchers/active_model/helpers.rb:7:in `map'
# /home/nadeem/.rvm/gems/ruby-2.1.2@orbitapp/gems/shoulda-matchers-2.7.0/lib/shoulda/matchers/active_model/helpers.rb:7:in `pretty_error_messages'
# /home/nadeem/.rvm/gems/ruby-2.1.2@orbitapp/gems/shoulda-matchers-2.7.0/lib/shoulda/matchers/active_model/validation_message_finder.rb:34:in `messages_description'
# /home/nadeem/.rvm/gems/ruby-2.1.2@orbitapp/gems/shoulda-matchers-2.7.0/lib/shoulda/matchers/active_model/allow_value_matcher.rb:293:in `error_description'
# /home/nadeem/.rvm/gems/ruby-2.1.2@orbitapp/gems/shoulda-matchers-2.7.0/lib/shoulda/matchers/active_model/allow_value_matcher.rb:232:in `failure_message_when_negated'
# /home/nadeem/.rvm/gems/ruby-2.1.2@orbitapp/gems/shoulda-matchers-2.7.0/lib/shoulda/matchers/active_model/validation_matcher.rb:44:in `allows_value_of'
# /home/nadeem/.rvm/gems/ruby-2.1.2@orbitapp/gems/shoulda-matchers-2.7.0/lib/shoulda/matchers/active_model/ensure_length_of_matcher.rb:336:in `allows_length_of?'
# /home/nadeem/.rvm/gems/ruby-2.1.2@orbitapp/gems/shoulda-matchers-2.7.0/lib/shoulda/matchers/active_model/ensure_length_of_matcher.rb:321:in `allows_minimum_length?'
# /home/nadeem/.rvm/gems/ruby-2.1.2@orbitapp/gems/shoulda-matchers-2.7.0/lib/shoulda/matchers/active_model/ensure_length_of_matcher.rb:295:in `lower_bound_matches?'
# /home/nadeem/.rvm/gems/ruby-2.1.2@orbitapp/gems/shoulda-matchers-2.7.0/lib/shoulda/matchers/active_model/ensure_length_of_matcher.rb:271:in `matches?'
# ./spec/models/user_spec.rb:12:in `block (2 levels) in <top (required)>'
All tests are passing except the ensure_length_of
and its for password
not email
To fix this. when i am instantiating the user in the test I have to provide an email
before(:each) { @user = User.new(email: '[email protected]') }
Thanks for this repo, it's a great validation resource!
I just have on suggestion. It would be great to make it possible to decide whether a url is allowed to have a tld or not.
http://localhost
is a valid url for sure, but most user inputs should force a tld to prevent typos like http://mywebsitecom
Or is there already an option which I didn't notice?
validates :object, :image => true # any type if image
validates :object, :image => { :format => :png, :max_dimensions => :400x200, :max_size => 2.megabytes }
etc.
idea stolen from Django #3 CSV validation
Idea stolen from Django #1: slug validation
Email validator assumes that [email protected], anything
is valid email.
Could be based onto
Josh French's Rakismet
or
(https://github.com/ysbaddaden/ruby-akismet)
I tried to use email and phone validations and found out that message option is required.
In docs it said that gem can be used like this:
validates :email, presence: true, email: true
In this case messages will be nil
, so model would not be valid, but there will be no message with explanation why.
Can be fixed like this:
validates :email, presence: true, email: { message: 'not valid' }
It's kinda redundant in my opinion.
Mby we need to add default messages for validators?
I think credit card validation is out of date, also it have not all needed features.
Is there a chance to merge activevalidators with https://github.com/Fivell/credit_card_validations gem? Or I can create pull request with some kind of wrapper .
Luhn algorithm, the implementation will look like the credit card one I guess...
validates :ssn, :social_security => { :type => :us_national_provider_identifier }
validates :ssn, :social_security => { :type => :securite_sociale_francaise }
You forgot to delete require 'phony' from 2nd line of activevalidators-2.0.2/lib/activevalidators.rb:2:in `require' so you get a cannot load such file -- phony error
The current URL validation relies on URI.parse, from the standard library, which only works with ASCII.
The following valid URLs are therefore rejected: 'http://example.com/caractère-accentué'
, 'http://ουτοπία.δπθ.gr'
.
The Addressable gem correctly parses these URLs, and could be used as a drop-in replacement instead.
Would you be interested in a PR?
validates :birth_date, :date => :past
validates :died_on, :date => :future
something like that :)
As mentioned in this thread #34 and in this commit 0acaa0e, the email validator should accept an option.
I reverted the aforementioned PR to make sure it's implemented in a single commit. @cfabianski, if you wanna pair in Lyon next week or the week after, feel free to contact me directly :)
Loading the country gem -- regardless of whether that validation is actually used -- causes the loading of a class named Country, which -- as in the case of our project -- clashes with an existing class named Country.
validator: password => { :strength => :hard }
Hi,
when I call ActiveValidators.activate(:date), it loads 'activevalidators/lib/active_model/validations/date_validator', which requires 'date_validator' (the gem), which requires 'date_validator/lib/date_validator', which then requires 'active_model/validations/date_validator'. This should load the file from date_validator gem, but it actually tries to load it from activevalidators gem (which was loaded in the first place), so this is a noop and the ActiveModel::Validations::DateValidator isn't defined.
This means date_validator stops working when activevalidators is higher in the $LOAD_PATH. This could be solved if date_validator/lib/active_model was moved to date_validator/lib/date_validator/active_model and required with the date_validator prefix, but this means changing the depended upon gem, which I guess is a no go. Is there any other way around this problem?
in lib/activevalidators.rb, there is an array of strings, and it refers to "date". There is a loop which builds up the above string and requires the file. But the file is itself missing. In rails 3.2, this throws an error (though I am not sure why require should raise an error).
http:bla passes url validation
Hi,
I'm using ActiveValidator v4.0.0 in a project using Rails v5.0.0.1, ruby v2.3.1. I added the following to config/application.rb
(inside the Class Application
block):
ActiveValidators.activate(:email, :phone, :postal_code, :date)
But when I add validates :delivery_address_postcode, postal_code: { country: :fr }
to my model and save data or run tests I get the following error:
NoMethodError:
undefined method `match' for 86637:Fixnum
Did you mean? catch
Any idea why that happens? Is there something else to configure?
Rails 5.0.2 && ActiveValidators v3.0.0
We will use that method to make sure we don't pass unknown params.
Idea stolen from Django #2: IPv4 and IPv6 validation
see http://en.wikipedia.org/wiki/North_American_Numbering_Plan for details but areacode can't start with 1 at the US.
I would use https://github.com/floere/phony as a base for the phone validator
Using Ruby 2.3.1, Rails 4.2.6, ActiveValidators 4.0.0
I have a PressRelease
model that has a datetime
field, released_at
. When using ActiveAdmin's datepicker, my date validation (that looks like this : )
class PressRelease < ActiveRecord::Base
validates :released_at, presence: true, date: { before: lambda { Time.now }, after: lambda { 50.years.ago } }
end
breaks and throws an ArgumentError : wrong number of arguments (given 1, expected 0)
.
I also use the URL validation, no problem with this one.
Full trace :
trace.txt
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.