rails_validations 1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/MIT-LICENSE +20 -0
- data/README.markdown +10 -0
- data/Rakefile +32 -0
- data/config/locales/en.yml +29 -0
- data/config/locales/nl.yml +29 -0
- data/lib/rails_validations.rb +12 -0
- data/lib/validators/date_validator.rb +56 -0
- data/lib/validators/domain_validator.rb +22 -0
- data/lib/validators/email_validator.rb +21 -0
- data/lib/validators/iban_validator.rb +17 -0
- data/lib/validators/phone_validator.rb +10 -0
- data/lib/validators/postal_code_validator.rb +14 -0
- data/lib/validators/presence_of_validator.rb +29 -0
- data/spec/spec_helper.rb +69 -0
- data/spec/validations/date_spec.rb +66 -0
- data/spec/validations/domain_spec.rb +56 -0
- data/spec/validations/email_spec.rb +55 -0
- data/spec/validations/iban_spec.rb +35 -0
- data/spec/validations/phone_spec.rb +41 -0
- data/spec/validations/postal_code_spec.rb +39 -0
- metadata +127 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: df7a1d3faea070ed50a78256af355989bc2f7703
|
4
|
+
data.tar.gz: 5f0817c861fc63425c5d1f2bf2b45fc50a7c6588
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a3ec178acd0a6744f6b57c8e2e4d276180b96aa45c71a7a2c2640a1ebcdb38df1cb196534583b449f58bb423d11b89062a4f4a78be3dc81c8be75e612d11f8bd
|
7
|
+
data.tar.gz: f5dedfc45b7943e5b820b56822ddd5686b5dbc5d9f3598156c195ee9e06cda3b18ddd9af6274611426a8571b373e5577a4c8e651073d2a8d96d2ce29a4b9a3e4
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2014 Martin Tournoij
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.markdown
ADDED
data/Rakefile
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
begin
|
2
|
+
require 'bundler/setup'
|
3
|
+
rescue LoadError
|
4
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
5
|
+
end
|
6
|
+
|
7
|
+
require 'rdoc/task'
|
8
|
+
|
9
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
10
|
+
rdoc.rdoc_dir = 'rdoc'
|
11
|
+
rdoc.title = 'RailsValidations'
|
12
|
+
rdoc.options << '--line-numbers'
|
13
|
+
rdoc.rdoc_files.include('README.markdown')
|
14
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
15
|
+
end
|
16
|
+
|
17
|
+
|
18
|
+
|
19
|
+
|
20
|
+
Bundler::GemHelper.install_tasks
|
21
|
+
|
22
|
+
require 'rake/testtask'
|
23
|
+
|
24
|
+
Rake::TestTask.new(:test) do |t|
|
25
|
+
t.libs << 'lib'
|
26
|
+
t.libs << 'test'
|
27
|
+
t.pattern = 'test/**/*_test.rb'
|
28
|
+
t.verbose = false
|
29
|
+
end
|
30
|
+
|
31
|
+
|
32
|
+
task default: :test
|
@@ -0,0 +1,29 @@
|
|
1
|
+
en:
|
2
|
+
rails_validations:
|
3
|
+
email:
|
4
|
+
invalid: not a valid email address
|
5
|
+
|
6
|
+
date:
|
7
|
+
invalid: not a valid date
|
8
|
+
after: must be after %{date}
|
9
|
+
after_or_equal_to: must be after or equal to %{date}
|
10
|
+
equal_to: must be equal to %{date}
|
11
|
+
before: must be before %{date}
|
12
|
+
before_or_equal_to: must be before or equal to %{date}
|
13
|
+
|
14
|
+
iban:
|
15
|
+
invalid: not a valid IBAN account number
|
16
|
+
too_short: has to have more than 5 characters
|
17
|
+
bad_chars: can only have letter and numbers
|
18
|
+
bad_check_digits: checksum didn't match
|
19
|
+
unknown_country_code: unknown country code
|
20
|
+
bad_length: bad length
|
21
|
+
bad_format: invalid format
|
22
|
+
|
23
|
+
phone:
|
24
|
+
invalid: not a valid phone number
|
25
|
+
|
26
|
+
domain:
|
27
|
+
invalid: not a valid domain
|
28
|
+
max_domain_parts: only %{max} domain parts are allowed
|
29
|
+
min_domain_parts: needs to have at least %{min} domain parts
|
@@ -0,0 +1,29 @@
|
|
1
|
+
nl:
|
2
|
+
rails_validations:
|
3
|
+
email:
|
4
|
+
invalid: geen geldig E-mailaddress
|
5
|
+
|
6
|
+
date:
|
7
|
+
invalid: geen geldige datum
|
8
|
+
after: moet na %{date} zijn
|
9
|
+
after_or_equal_to: moet na of op %{date} zijn
|
10
|
+
equal_to: moet gelijk zijn aan %{date}
|
11
|
+
before: moet voor %{date} zijn
|
12
|
+
before_or_equal_to: moet voor of gelijk zijn aan %{date}
|
13
|
+
|
14
|
+
iban:
|
15
|
+
invalid: geen geldig IBAN rekeningnummer
|
16
|
+
too_short: moet meer dan 5 karakters bevatten
|
17
|
+
bad_chars: kan alleen letters en nummers bevatten
|
18
|
+
bad_check_digits: checksum klopt niet
|
19
|
+
unknown_country_code: ongeldige landcode
|
20
|
+
bad_length: verkeerde lengte
|
21
|
+
bad_format: verkeerd formaat
|
22
|
+
|
23
|
+
phone:
|
24
|
+
invalid: geen geldig telefoonnummer
|
25
|
+
|
26
|
+
domain:
|
27
|
+
invalid: geen geldig domain
|
28
|
+
max_domain_parts: er zijn %{max} domeinstukken toegestaan
|
29
|
+
min_domain_parts: er moeten minimaal %{min} domainstukken zijn
|
@@ -0,0 +1,12 @@
|
|
1
|
+
require 'validators/date_validator'
|
2
|
+
require 'validators/domain_validator'
|
3
|
+
require 'validators/email_validator'
|
4
|
+
require 'validators/iban_validator'
|
5
|
+
require 'validators/phone_validator'
|
6
|
+
require 'validators/postal_code_validator'
|
7
|
+
#require 'validators/presence_of_validator'
|
8
|
+
|
9
|
+
I18n.load_path += Dir["#{File.dirname(__FILE__)}/../config/locales/*.yml"]
|
10
|
+
|
11
|
+
module RailsValidations
|
12
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
class DateValidator < ActiveModel::EachValidator
|
2
|
+
CHECKS = {
|
3
|
+
# These keys make the most sense for dates
|
4
|
+
after: :>,
|
5
|
+
after_or_equal_to: :>=,
|
6
|
+
equal_to: :==,
|
7
|
+
before: :<,
|
8
|
+
before_or_equal_to: :<=,
|
9
|
+
|
10
|
+
# For compatibility with numericality
|
11
|
+
#greater_than: :>,
|
12
|
+
#greater_than_or_equal_to: :>=,
|
13
|
+
#less_than: :<,
|
14
|
+
#less_than_or_equal_to: :<=,
|
15
|
+
}.freeze
|
16
|
+
|
17
|
+
|
18
|
+
def validate_each record, attribute, raw_value
|
19
|
+
# TODO: Do we need to do anything with timezones? Figure it out (rails has
|
20
|
+
# ActiveSupport::TimeWithZone)...
|
21
|
+
value = if raw_value.is_a? Fixnum
|
22
|
+
time.at(raw_value).to_date
|
23
|
+
elsif raw_value.respond_to? :to_date
|
24
|
+
raw_value.to_date
|
25
|
+
else
|
26
|
+
record.errors.add attribute, I18n.t('rails_validations.date.invalid')
|
27
|
+
end
|
28
|
+
|
29
|
+
#elsif raw_value.is_a?(Symbol) || raw_value.is_a?(String)
|
30
|
+
# # Assume another (date) attribute
|
31
|
+
# record.send(raw_value).to_date
|
32
|
+
|
33
|
+
unless value
|
34
|
+
record.errors.add attribute, I18n.t('rails_validations.date.invalid')
|
35
|
+
return
|
36
|
+
end
|
37
|
+
|
38
|
+
options.slice(*CHECKS.keys).each do |option, raw_option_value|
|
39
|
+
option_value = if raw_option_value.respond_to? :call
|
40
|
+
raw_option_value.call record
|
41
|
+
elsif raw_option_value.is_a? Symbol
|
42
|
+
record.send raw_option_value
|
43
|
+
elsif raw_option_value.is_a? Fixnum
|
44
|
+
time.at(raw_option_value).to_date
|
45
|
+
elsif raw_option_value.respond_to? :to_date
|
46
|
+
raw_option_value.to_date
|
47
|
+
else
|
48
|
+
raise ArgumentError
|
49
|
+
end
|
50
|
+
|
51
|
+
unless value.send CHECKS[option], option_value
|
52
|
+
record.errors.add attribute, I18n.t("rails_validations.date.#{option}", date: I18n.l(option_value))
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
class DomainValidator < ActiveModel::EachValidator
|
2
|
+
#\A[a-zA-Z\d-]{,63}\z
|
3
|
+
REGEXP = /
|
4
|
+
\A[\p{L}\d-]{1,63}\z
|
5
|
+
/ix.freeze
|
6
|
+
|
7
|
+
def validate_each record, attribute, value
|
8
|
+
parts = value.split '.'
|
9
|
+
|
10
|
+
parts.each do |part|
|
11
|
+
record.errors.add attribute, (options[:message] || I18n.t('rails_validations.domain.invalid')) unless part =~ REGEXP
|
12
|
+
end
|
13
|
+
|
14
|
+
if options[:max_domain_parts].present? && parts.length > options[:max_domain_parts]
|
15
|
+
record.errors.add attribute, (options[:message] || I18n.t('rails_validations.domain.max_domain_parts', max: options[:max_domain_parts]))
|
16
|
+
end
|
17
|
+
|
18
|
+
if options[:min_domain_parts].present? && options[:min_domain_parts] > parts.length
|
19
|
+
record.errors.add attribute, (options[:message] || I18n.t('rails_validations.domain.min_domain_parts', min: options[:min_domain_parts]))
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# Validate email, also works with UTF-8/IDN.
|
2
|
+
#
|
3
|
+
# The validation is intentionally simply, you can never be sure an email is
|
4
|
+
# valid anyway (a user can mistype gmail as gmaill, for example).
|
5
|
+
#
|
6
|
+
# - user part: one or more characters except @ or whitespace.
|
7
|
+
# - domain part: 2 or more character except @ or whitespace.
|
8
|
+
# - tld part: 2 or more word characters *or* -
|
9
|
+
class EmailValidator < ActiveModel::EachValidator
|
10
|
+
REGEXP = /\A
|
11
|
+
[^@\s]+
|
12
|
+
@
|
13
|
+
[^@\s.]+
|
14
|
+
\.
|
15
|
+
[\p{L}0-9\-]{2,}
|
16
|
+
\z/ix.freeze
|
17
|
+
|
18
|
+
def validate_each record, attribute, value
|
19
|
+
record.errors.add attribute, (options[:message] || I18n.t('rails_validations.email.invalid')) unless value =~ REGEXP
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
class IbanValidator < ActiveModel::EachValidator
|
2
|
+
|
3
|
+
def validate_each record, attribute, value
|
4
|
+
require 'iban-tools'
|
5
|
+
|
6
|
+
return if value.blank?
|
7
|
+
|
8
|
+
errors = IBANTools::IBAN.new(value).validation_errors
|
9
|
+
return if errors.blank?
|
10
|
+
|
11
|
+
if options[:message]
|
12
|
+
record.errors.add attribute, options[:message]
|
13
|
+
else
|
14
|
+
errors.each { |e| record.errors.add attribute, I18n.t("rails_validations.iban.#{e}") }
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
class PhoneValidator < ActiveModel::EachValidator
|
2
|
+
REGEXP = /\A
|
3
|
+
(\(?\+\d+\)?)? # optional country code
|
4
|
+
[0-9 \-.()]{4,20}
|
5
|
+
\z/x.freeze
|
6
|
+
|
7
|
+
def validate_each record, attribute, value
|
8
|
+
record.errors.add attribute, (options[:message] || I18n.t('rails_validations.invalid_phone')) unless value =~ REGEXP
|
9
|
+
end
|
10
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
class PostalCodeValidator < ActiveModel::EachValidator
|
2
|
+
# keys are ISO-3366-1 alpha2
|
3
|
+
REGEXP = {
|
4
|
+
nl: /\A\d{4}\s*?\w{2}\z/,
|
5
|
+
}.freeze
|
6
|
+
|
7
|
+
|
8
|
+
def validate_each record, attribute, value
|
9
|
+
key = (options[:country] || I18n.locale).downcase.to_sym
|
10
|
+
key = :gb if key == :uk # Since it's easy to get this wrong
|
11
|
+
|
12
|
+
record.errors.add attribute, (options[:message] || I18n.t('rails_validations.postal_code.invalid')) unless value =~ REGEXP[key]
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# Test the presence of attributes from a belongs_to.
|
2
|
+
#
|
3
|
+
# For example, doing this in the User model:
|
4
|
+
#
|
5
|
+
# belongs_to :person
|
6
|
+
# validates :person, presence_of: :initials
|
7
|
+
#
|
8
|
+
# would be the same as your User model containing:
|
9
|
+
#
|
10
|
+
# has_one :user, inverse_of: :person
|
11
|
+
# has_one :admin, inverse_of: :person
|
12
|
+
#
|
13
|
+
# validates :initials, if: :user
|
14
|
+
#
|
15
|
+
# TODO: Unfinished, copied from K4K
|
16
|
+
class PresenceOfValidator < ActiveModel::EachValidator
|
17
|
+
def validate_each record, attribute, value
|
18
|
+
errortext = if options[:with].to_s.pluralize == options[:with].to_s
|
19
|
+
I18n.t('errors.messages.empty_plural')
|
20
|
+
else
|
21
|
+
I18n.t('errors.messages.empty')
|
22
|
+
end
|
23
|
+
|
24
|
+
if record.send(attribute) && record.send(attribute).send(options[:with]).blank?
|
25
|
+
record.send(attribute).errors[options[:with]] << errortext
|
26
|
+
record.errors["#{attribute}.#{options[:with]}"] << errortext
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
ENV["RAILS_ENV"] ||= 'test'
|
2
|
+
require 'rubygems'
|
3
|
+
require 'bundler/setup'
|
4
|
+
|
5
|
+
require 'action_controller'
|
6
|
+
require 'action_dispatch'
|
7
|
+
require 'action_pack'
|
8
|
+
require 'action_view'
|
9
|
+
require 'active_model'
|
10
|
+
require 'active_record'
|
11
|
+
require 'active_support'
|
12
|
+
|
13
|
+
#require 'rspec/rails'
|
14
|
+
|
15
|
+
# Requires supporting files with custom matchers and macros, etc,
|
16
|
+
# in ./support/ and its subdirectories in alphabetic order.
|
17
|
+
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].sort.each { |f| require f }
|
18
|
+
|
19
|
+
Dir["#{File.dirname(__FILE__)}/../lib/**/*.rb"].sort.each { |f| require f }
|
20
|
+
|
21
|
+
I18n.enforce_available_locales = false if I18n.respond_to?(:enforce_available_locales)
|
22
|
+
|
23
|
+
I18n.load_path += Dir["#{File.dirname(__FILE__)}/../config/locales/*.yml"]
|
24
|
+
|
25
|
+
|
26
|
+
module ValidationsSpecHelper
|
27
|
+
include ActiveSupport
|
28
|
+
include ActionPack
|
29
|
+
include ActionView::Context if defined?(ActionView::Context)
|
30
|
+
include ActionController::RecordIdentifier if defined?(ActionController::RecordIdentifier)
|
31
|
+
include ActionView::RecordIdentifier if defined?(ActionView::RecordIdentifier)
|
32
|
+
|
33
|
+
|
34
|
+
class Record
|
35
|
+
extend ActiveModel::Naming # if defined?(ActiveModel::Naming)
|
36
|
+
include ActiveModel::Conversion# if defined?(ActiveModel::Conversion)
|
37
|
+
include ActiveModel::Validations# if defined?(ActiveModel::Validations)
|
38
|
+
|
39
|
+
|
40
|
+
def initialize attr={}
|
41
|
+
attr.each { |k, v| send("#{k}=", v) }
|
42
|
+
super()
|
43
|
+
end
|
44
|
+
|
45
|
+
|
46
|
+
def v; @value end
|
47
|
+
def v= v; @value = v end
|
48
|
+
end
|
49
|
+
|
50
|
+
|
51
|
+
def model v
|
52
|
+
described_class.new v: v
|
53
|
+
end
|
54
|
+
|
55
|
+
|
56
|
+
def model_errors v
|
57
|
+
m = model v
|
58
|
+
m.valid?
|
59
|
+
return m.errors[:v]
|
60
|
+
end
|
61
|
+
|
62
|
+
|
63
|
+
def with_validation validate_string
|
64
|
+
described_class.clear_validators!
|
65
|
+
described_class.class_eval "validates :v, #{validate_string}"
|
66
|
+
|
67
|
+
yield
|
68
|
+
end
|
69
|
+
end
|
@@ -0,0 +1,66 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'active_support/core_ext/date/calculations'
|
3
|
+
|
4
|
+
module ValidationsSpecHelper
|
5
|
+
class Date < ValidationsSpecHelper::Record
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
|
10
|
+
describe ValidationsSpecHelper::Date do
|
11
|
+
include ValidationsSpecHelper
|
12
|
+
|
13
|
+
|
14
|
+
it 'works with true' do
|
15
|
+
with_validation 'date: true' do
|
16
|
+
expect(model(Date.today).valid?).to eq(true)
|
17
|
+
expect(model(Time.now).valid?).to eq(true)
|
18
|
+
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
|
23
|
+
it 'works with :after' do
|
24
|
+
with_validation 'date: { after: ::Date.today }' do
|
25
|
+
expect(model(Date.today).valid?).to eq(false)
|
26
|
+
expect(model(Date.today.advance days: 1).valid?).to eq(true)
|
27
|
+
expect(model(Date.today.advance days: -1).valid?).to eq(false)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
|
32
|
+
it 'works with :after_or_equal_to:' do
|
33
|
+
with_validation 'date: { after_or_equal_to: ::Date.today }' do
|
34
|
+
expect(model(Date.today).valid?).to eq(true)
|
35
|
+
expect(model(Date.today.advance days: 1).valid?).to eq(true)
|
36
|
+
expect(model(Date.today.advance days: -1).valid?).to eq(false)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
|
41
|
+
it 'works with :equal_to' do
|
42
|
+
with_validation 'date: { equal_to: ::Date.today }' do
|
43
|
+
expect(model(Date.today).valid?).to eq(true)
|
44
|
+
expect(model(Date.today.advance days: 1).valid?).to eq(false)
|
45
|
+
expect(model(Date.today.advance days: -1).valid?).to eq(false)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
|
50
|
+
it 'works with :before' do
|
51
|
+
with_validation 'date: { before: ::Date.today }' do
|
52
|
+
expect(model(Date.today).valid?).to eq(false)
|
53
|
+
expect(model(Date.today.advance days: 1).valid?).to eq(false)
|
54
|
+
expect(model(Date.today.advance days: -1).valid?).to eq(true)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
|
59
|
+
it 'works with :before_or_equal_to' do
|
60
|
+
with_validation 'date: { before_or_equal_to: ::Date.today }' do
|
61
|
+
expect(model(Date.today).valid?).to eq(true)
|
62
|
+
expect(model(Date.today.advance days: 1).valid?).to eq(false)
|
63
|
+
expect(model(Date.today.advance days: -1).valid?).to eq(true)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module ValidationsSpecHelper
|
4
|
+
class ValidationsSpecHelper::Domain < ValidationsSpecHelper::Record
|
5
|
+
end
|
6
|
+
end
|
7
|
+
|
8
|
+
|
9
|
+
describe ValidationsSpecHelper::Domain do
|
10
|
+
include ValidationsSpecHelper
|
11
|
+
|
12
|
+
it 'gives an error on invalid domains' do
|
13
|
+
with_validation 'domain: true' do
|
14
|
+
%w{
|
15
|
+
li\ co.nl
|
16
|
+
li_co.nl
|
17
|
+
lico@nl
|
18
|
+
}.each do |v|
|
19
|
+
expect(model(v).valid?).to eq(false)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
|
25
|
+
it 'works for valid domains' do
|
26
|
+
with_validation 'domain: true' do
|
27
|
+
%w{
|
28
|
+
lico
|
29
|
+
lico.nl
|
30
|
+
www.lico.nl
|
31
|
+
hello.world.www.lico.nl
|
32
|
+
ﻢﻔﺗﻮﺣ.ﺬﺑﺎﺑﺓ
|
33
|
+
}.each do |v|
|
34
|
+
expect(model(v).valid?).to eq(true)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
|
40
|
+
it 'works with :max_domain_parts' do
|
41
|
+
with_validation 'domain: { max_domain_parts: 3 }' do
|
42
|
+
expect(model('not.many').valid?).to eq(true)
|
43
|
+
expect(model('not.too.many').valid?).to eq(true)
|
44
|
+
expect(model('too.many.domain.parts').valid?).to eq(false)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
|
49
|
+
it 'works with :min' do
|
50
|
+
with_validation 'domain: { min_domain_parts: 3 }' do
|
51
|
+
expect(model('too.few').valid?).to eq(false)
|
52
|
+
expect(model('just.enough.parts').valid?).to eq(true)
|
53
|
+
expect(model('more.than.enough.parts').valid?).to eq(true)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module ValidationsSpecHelper
|
4
|
+
class Email < ValidationsSpecHelper::Record
|
5
|
+
end
|
6
|
+
end
|
7
|
+
|
8
|
+
|
9
|
+
describe ValidationsSpecHelper::Email do
|
10
|
+
include ValidationsSpecHelper
|
11
|
+
|
12
|
+
|
13
|
+
it 'gives an error on invalid emails' do
|
14
|
+
with_validation 'email: true' do
|
15
|
+
%w{
|
16
|
+
not\ valid
|
17
|
+
invalid@example.
|
18
|
+
invalid@@example.com
|
19
|
+
invalid@example..com
|
20
|
+
@example.com
|
21
|
+
invalid@.com
|
22
|
+
invalid@example.c
|
23
|
+
in\ valid@example.com
|
24
|
+
invalid@exam\ ple.com
|
25
|
+
}.each do |v|
|
26
|
+
expect(model(v).valid?).to eq(false)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
|
32
|
+
it 'works for valid emails' do
|
33
|
+
with_validation 'email: true' do
|
34
|
+
%w{
|
35
|
+
valid@example.com
|
36
|
+
valid.address@example.com
|
37
|
+
valid-example@example.com
|
38
|
+
valid+address@example.com
|
39
|
+
valid@example.anewtld
|
40
|
+
مفتوح.ذبابة@إرهاب.شبكة
|
41
|
+
}.each do |v|
|
42
|
+
expect(model(v).valid?).to eq(true)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
|
48
|
+
it 'accepts a custom error message' do
|
49
|
+
with_validation 'email: { message: "foobar" }' do
|
50
|
+
m = described_class.new v: 'not even remotely valid'
|
51
|
+
m.valid?
|
52
|
+
expect(m.errors[:v].first).to eq('foobar')
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module ValidationsSpecHelper
|
4
|
+
class Iban < ValidationsSpecHelper::Record
|
5
|
+
end
|
6
|
+
end
|
7
|
+
|
8
|
+
|
9
|
+
describe ValidationsSpecHelper::Iban do
|
10
|
+
include ValidationsSpecHelper
|
11
|
+
|
12
|
+
|
13
|
+
it 'accepts a valid IBAN' do
|
14
|
+
with_validation 'iban: true' do
|
15
|
+
%w{
|
16
|
+
NL65AEGO0721647952
|
17
|
+
NL43ABNA0841376913
|
18
|
+
}.each do |v|
|
19
|
+
expect(model(v).valid?).to eq(true)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
|
25
|
+
it 'gives an error on an invalid IBAN' do
|
26
|
+
with_validation 'iban: true' do
|
27
|
+
%w{
|
28
|
+
NL65AEGO0721647951
|
29
|
+
not valid
|
30
|
+
}.each do |v|
|
31
|
+
expect(model(v).valid?).to eq(false)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module ValidationsSpecHelper
|
4
|
+
class Phone < ValidationsSpecHelper::Record
|
5
|
+
end
|
6
|
+
end
|
7
|
+
|
8
|
+
|
9
|
+
describe ValidationsSpecHelper::Phone do
|
10
|
+
include ValidationsSpecHelper
|
11
|
+
|
12
|
+
|
13
|
+
it 'allows valid phone numbers' do
|
14
|
+
with_validation 'phone: true' do
|
15
|
+
%w{
|
16
|
+
06\ 5155\ 2300
|
17
|
+
+031\ 06\ 5155\ 2300
|
18
|
+
+31\ 06\ 5155\ 2300
|
19
|
+
(+31)\ 06-51552300
|
20
|
+
(+1)\ 06.51.55.23.00
|
21
|
+
}.each do |v|
|
22
|
+
expect(model(v).valid?).to eq(true)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
|
28
|
+
it 'gives an error on invalid phone numbers' do
|
29
|
+
with_validation 'phone: true' do
|
30
|
+
%w{
|
31
|
+
06-abc
|
32
|
+
{06}51559000
|
33
|
+
123123*123123
|
34
|
+
123
|
35
|
+
132131313213213123123213213131231242312554
|
36
|
+
}.each do |v|
|
37
|
+
expect(model(v).valid?).to eq(false)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module ValidationsSpecHelper
|
4
|
+
class PostalCode < ValidationsSpecHelper::Record
|
5
|
+
end
|
6
|
+
end
|
7
|
+
|
8
|
+
|
9
|
+
describe ValidationsSpecHelper::PostalCode do
|
10
|
+
include ValidationsSpecHelper
|
11
|
+
|
12
|
+
|
13
|
+
it 'works for nl codes' do
|
14
|
+
with_validation 'postal_code: { country: :nl }' do
|
15
|
+
%w{
|
16
|
+
5600aa
|
17
|
+
1000Aa
|
18
|
+
9999\ aa
|
19
|
+
1234\ AA
|
20
|
+
}.each do |v|
|
21
|
+
expect(model(v).valid?).to eq(true)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
|
27
|
+
it 'gives an error for nl codes' do
|
28
|
+
with_validation 'postal_code: { country: :nl }' do
|
29
|
+
%w{
|
30
|
+
560aa
|
31
|
+
10000Aa
|
32
|
+
9999\ a
|
33
|
+
1234\ AAA
|
34
|
+
}.each do |v|
|
35
|
+
expect(model(v).valid?).to eq(false)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
metadata
ADDED
@@ -0,0 +1,127 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rails_validations
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: '1.0'
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Martin Tournoij
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-09-05 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: actionpack
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '4.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '4.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: activerecord
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '4.0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '4.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: iban-tools
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec-rails
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.0'
|
69
|
+
description: Extra validations for rails
|
70
|
+
email:
|
71
|
+
- martin@lico.nl
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- MIT-LICENSE
|
77
|
+
- README.markdown
|
78
|
+
- Rakefile
|
79
|
+
- config/locales/en.yml
|
80
|
+
- config/locales/nl.yml
|
81
|
+
- lib/rails_validations.rb
|
82
|
+
- lib/validators/date_validator.rb
|
83
|
+
- lib/validators/domain_validator.rb
|
84
|
+
- lib/validators/email_validator.rb
|
85
|
+
- lib/validators/iban_validator.rb
|
86
|
+
- lib/validators/phone_validator.rb
|
87
|
+
- lib/validators/postal_code_validator.rb
|
88
|
+
- lib/validators/presence_of_validator.rb
|
89
|
+
- spec/spec_helper.rb
|
90
|
+
- spec/validations/date_spec.rb
|
91
|
+
- spec/validations/domain_spec.rb
|
92
|
+
- spec/validations/email_spec.rb
|
93
|
+
- spec/validations/iban_spec.rb
|
94
|
+
- spec/validations/phone_spec.rb
|
95
|
+
- spec/validations/postal_code_spec.rb
|
96
|
+
homepage: https://github.com/bluerail/rails_validations
|
97
|
+
licenses:
|
98
|
+
- MIT
|
99
|
+
metadata: {}
|
100
|
+
post_install_message:
|
101
|
+
rdoc_options: []
|
102
|
+
require_paths:
|
103
|
+
- lib
|
104
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
105
|
+
requirements:
|
106
|
+
- - ">="
|
107
|
+
- !ruby/object:Gem::Version
|
108
|
+
version: '2.0'
|
109
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
110
|
+
requirements:
|
111
|
+
- - ">="
|
112
|
+
- !ruby/object:Gem::Version
|
113
|
+
version: '0'
|
114
|
+
requirements: []
|
115
|
+
rubyforge_project:
|
116
|
+
rubygems_version: 2.2.2
|
117
|
+
signing_key:
|
118
|
+
specification_version: 4
|
119
|
+
summary: Extra validations for rails
|
120
|
+
test_files:
|
121
|
+
- spec/spec_helper.rb
|
122
|
+
- spec/validations/date_spec.rb
|
123
|
+
- spec/validations/email_spec.rb
|
124
|
+
- spec/validations/iban_spec.rb
|
125
|
+
- spec/validations/phone_spec.rb
|
126
|
+
- spec/validations/postal_code_spec.rb
|
127
|
+
- spec/validations/domain_spec.rb
|