activevalidators 3.2.0 → 3.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +0 -0
- data.tar.gz.sig +0 -0
- data/ChangeLog.md +15 -0
- data/README.md +58 -54
- data/activevalidators.gemspec +3 -2
- data/checksums/3.2.0.sha512 +1 -0
- data/lib/active_validators/active_model/validations/credit_card_validator.rb +35 -30
- data/lib/active_validators/active_model/validations/postal_code_validator.rb +1 -3
- data/lib/active_validators/active_model/validations/url_validator.rb +1 -1
- data/test/validations/credit_card_test.rb +38 -26
- data/test/validations/postal_code_test.rb +8 -1
- metadata +20 -5
- metadata.gz.sig +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bef3e7e3c0958acd7cb1fb5f3ac80431c844f365
|
4
|
+
data.tar.gz: 798635cee273b20606c518811cc375d6c6876510
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 82c4ce02b0cda960f42f83c33ebf4a63c73666db63f5942015c47f633d2ad7d4b4395f43d6e4954816753a0c39b3995def9aba486c25e04594dbf64b92e8fc5d
|
7
|
+
data.tar.gz: 75482c5f814921806c2c5b8c8229a1554c4bb2fb54b54a47db5729e55f6f9ac338e398771aebdae6d069e9c5579ec8bac1b459fb2a7f0db414b74924e66f5631
|
checksums.yaml.gz.sig
CHANGED
Binary file
|
data.tar.gz.sig
CHANGED
Binary file
|
data/ChangeLog.md
CHANGED
@@ -1,5 +1,20 @@
|
|
1
1
|
# UNRELEASED
|
2
2
|
|
3
|
+
# 3.3.0
|
4
|
+
|
5
|
+
## DEPRECATION
|
6
|
+
|
7
|
+
* Credit Cards: `carte_blanche` is going away, please use `dinners` instead
|
8
|
+
|
9
|
+
## CHANGES
|
10
|
+
|
11
|
+
* Postal code validator: Make country case-insensitive for postal codes
|
12
|
+
* URL validator: Remove obsolete usage of URI.regexp
|
13
|
+
* CC validator: Use the `credit_card_validations` gem for CC validations
|
14
|
+
* README: Details about activating AV w/ Rails
|
15
|
+
* Spec: Links to dependency gems for convenience; sorting ; code :sparkles:.
|
16
|
+
* General: Loosen up the dependency on countries
|
17
|
+
|
3
18
|
# 3.2.0
|
4
19
|
|
5
20
|
## MAJOR CHANGES
|
data/README.md
CHANGED
@@ -43,79 +43,83 @@ want to use as ActiveValidators doesn't force you to use them all :
|
|
43
43
|
`ActiveValidators.activate` can be called as many times as one wants. It's only
|
44
44
|
a syntactic sugar on top a normal Ruby `require`.
|
45
45
|
|
46
|
+
In a standard Ruby on Rails application, this line goes either in an initializer
|
47
|
+
(`config/initializers/active_validators_activation.rb` for example), or directly
|
48
|
+
within `config/application` right inside your `MyApp::Application` class definition.
|
49
|
+
|
46
50
|
## Usage
|
47
51
|
|
48
52
|
In your models, the gem provides new validators like `email`, or `url`:
|
49
53
|
|
50
54
|
```ruby
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
55
|
+
class User
|
56
|
+
validates :company_siren, :siren => true
|
57
|
+
validates :email_address, :email => true # == :email => { :strict => false }
|
58
|
+
validates :link_url, :url => true # Could be combined with `allow_blank: true`
|
59
|
+
validates :password, :password => { :strength => :medium }
|
60
|
+
validates :postal_code, :postal_code => { :country => :us }
|
61
|
+
validates :twitter, :twitter => true
|
62
|
+
validates :twitter_at, :twitter => { :format => :username_with_at }
|
63
|
+
validates :twitter_url, :twitter => { :format => :url }
|
64
|
+
validates :user_phone, :phone => true
|
65
|
+
end
|
66
|
+
|
67
|
+
class Identification
|
68
|
+
validates :nino, :nino => true
|
69
|
+
validates :sin, :sin => true
|
70
|
+
validates :ssn, :ssn => true
|
71
|
+
end
|
72
|
+
|
73
|
+
class Article
|
74
|
+
validates :slug, :slug => true
|
75
|
+
validates :expiration_date, :date => {
|
76
|
+
:after => lambda { Time.now },
|
77
|
+
:before => lambda { Time.now + 1.year }
|
78
|
+
}
|
79
|
+
end
|
80
|
+
|
81
|
+
class Device
|
82
|
+
validates :ipv4, :ip => { :format => :v4 }
|
83
|
+
validates :ipv6, :ip => { :format => :v6 }
|
84
|
+
end
|
85
|
+
|
86
|
+
class Account
|
87
|
+
validates :any_card, :credit_card => true
|
88
|
+
validates :visa_card, :credit_card => { :type => :visa }
|
89
|
+
validates :credit_card, :credit_card => { :type => :any }
|
90
|
+
validates :supported_card, :credit_card => { :type => [:visa, :master_card, :amex] }
|
91
|
+
end
|
92
|
+
|
93
|
+
class Order
|
94
|
+
validates :tracking_num, :tracking_number => { :carrier => :ups }
|
95
|
+
end
|
96
|
+
|
97
|
+
class Product
|
98
|
+
validates :code, :barcode => { :format => :ean13 }
|
99
|
+
end
|
96
100
|
```
|
97
101
|
|
98
102
|
Exhaustive list of supported validators and their implementation:
|
99
103
|
|
100
|
-
* `
|
101
|
-
* `
|
102
|
-
* `
|
104
|
+
* `barcode` : based on known formats (:ean13 only for now)
|
105
|
+
* `credit_card` : based on the [`credit_card_validations`](https://github.com/Fivell/credit_card_validations) gem
|
106
|
+
* `date` : based on the [`date_validator`](https://github.com/codegram/date_validator) gem
|
107
|
+
* `email` : based on the [`mail`](https://github.com/mikel/mail) gem
|
108
|
+
* `hex_color` : based on a regular expression
|
103
109
|
* `ip` : based on `Resolv::IPv[4|6]::Regex`
|
104
110
|
* `nino` : National Insurance number (only for UK). Please note that this validation will not accept temporary (such as 63T12345) or administrative numbers (with prefixes like OO, CR, FY, MW, NC, PP, PY, PZ).
|
105
111
|
* `password` : based on a set of regular expressions
|
106
112
|
* `phone` : based on a set of predefined masks
|
107
113
|
* `postal_code`: based on a set of predefined masks
|
108
|
-
* `
|
109
|
-
* `
|
114
|
+
* `regexp` : uses Ruby's [`Regexp.compile`](http://www.ruby-doc.org/core-2.1.1/Regexp.html#method-c-new) method
|
115
|
+
* `respond_to` : generic Ruby `respond_to`
|
116
|
+
* `siren` : [SIREN](http://fr.wikipedia.org/wiki/SIREN) company numbers in France
|
110
117
|
* `slug` : based on `ActiveSupport::String#parameterize`
|
111
|
-
* `sin` : Social Insurance Number (only for Canada). You also
|
118
|
+
* `sin` : Social Insurance Number (only for Canada). You may also allow permanent resident cards (such cards start with '9'): `:sin => {:country => :canada, :country_options => {allow_permanent_residents: true}}`
|
112
119
|
* `ssn` : Social Security Number (only for USA).
|
113
120
|
* `tracking_number`: based on a set of predefined masks
|
114
121
|
* `twitter` : based on a regular expression
|
115
122
|
* `url` : based on a regular expression
|
116
|
-
* `barcode` : based on known formats (:ean13 only for now)
|
117
|
-
* `hex_color` : based on a regular expression
|
118
|
-
* `regexp` : uses Ruby's [`Regexp.compile`](http://www.ruby-doc.org/core-2.1.1/Regexp.html#method-c-new) method
|
119
123
|
|
120
124
|
## Todo
|
121
125
|
|
data/activevalidators.gemspec
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
# -*- encoding: utf-8 -*-
|
2
2
|
Gem::Specification.new do |s|
|
3
3
|
s.name = 'activevalidators'
|
4
|
-
s.version = '3.
|
4
|
+
s.version = '3.3.0'
|
5
5
|
s.platform = Gem::Platform::RUBY
|
6
6
|
s.authors = ['Franck Verrot', 'Paco Guzmán', 'Oriol Gual', 'Garrett Bjerkhoel', 'Renato Riccieri Santos Zannon', 'Brian Moseley', 'Serj L aka Loremaster']
|
7
7
|
s.email = ['franck@verrot.fr']
|
@@ -15,9 +15,10 @@ Gem::Specification.new do |s|
|
|
15
15
|
s.add_dependency 'rake' , '>= 0.8.7'
|
16
16
|
s.add_dependency 'activemodel' , '>= 3.0.0'
|
17
17
|
s.add_dependency 'phony' , '>= 1.9.0'
|
18
|
-
s.add_dependency 'countries' , '
|
18
|
+
s.add_dependency 'countries' , '>= 0.9.3'
|
19
19
|
s.add_dependency 'mail'
|
20
20
|
s.add_dependency 'date_validator'
|
21
|
+
s.add_dependency 'credit_card_validations', '~> 2.0.2'
|
21
22
|
|
22
23
|
s.files = `git ls-files`.split("\n")
|
23
24
|
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
@@ -0,0 +1 @@
|
|
1
|
+
282e0d989bc5987b9ec98124ffeca9be5708389674a6f7bea23050f4bf1451054cb49d705e63f4a7e0d2654df14bdfd737d39c9653c74d3685777a525268cb49
|
@@ -1,53 +1,58 @@
|
|
1
|
-
require '
|
2
|
-
|
1
|
+
require 'credit_card_validations'
|
3
2
|
module ActiveModel
|
4
3
|
module Validations
|
5
4
|
|
6
5
|
class CreditCardValidator < EachValidator
|
7
6
|
def validate_each(record, attribute, value)
|
8
|
-
|
9
|
-
|
7
|
+
brand = options.fetch(:type, :any)
|
8
|
+
brands = (brand == :any ? [] : Array.wrap(brand))
|
9
|
+
record.errors.add(attribute) if value.blank? || !ActiveCreditCardBrand.new(value).valid?(*brands)
|
10
10
|
end
|
11
11
|
|
12
|
-
|
13
|
-
value.tr('- ','')
|
14
|
-
end
|
12
|
+
class ActiveCreditCardBrand
|
15
13
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
14
|
+
DEPRECATED_BRANDS = [
|
15
|
+
:en_route, # belongs to Diners Club since 1992 obsolete
|
16
|
+
:carte_blanche # belongs to Diners Club ,was finally phased out by 2005
|
17
|
+
]
|
18
|
+
|
19
|
+
BRANDS_ALIASES = {
|
20
|
+
master_card: :mastercard,
|
21
|
+
diners_club: :diners,
|
22
|
+
en_route: :diners,
|
23
|
+
carte_blanche: :diners
|
24
|
+
}
|
24
25
|
|
25
|
-
def
|
26
|
-
|
26
|
+
def initialize(number)
|
27
|
+
@number = number
|
27
28
|
end
|
28
29
|
|
29
|
-
|
30
|
-
|
30
|
+
def valid?(*brands)
|
31
|
+
deprecated_brands(brands).each do |brand|
|
32
|
+
ActiveSupport::Deprecation.warn("support for #{brand} will be removed in future versions, please use #{BRANDS_ALIASES[brand]} instead")
|
33
|
+
end
|
34
|
+
detector.valid?(*normalize_brands(brands))
|
31
35
|
end
|
32
36
|
|
33
|
-
|
34
|
-
|
37
|
+
private
|
38
|
+
|
39
|
+
def detector
|
40
|
+
CreditCardValidations::Detector.new(@number)
|
35
41
|
end
|
36
42
|
|
37
|
-
def
|
38
|
-
|
43
|
+
def deprecated_brands(brands)
|
44
|
+
DEPRECATED_BRANDS & brands
|
39
45
|
end
|
40
46
|
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
def self.#{card_type}?(number)
|
45
|
-
LuhnChecker.valid?(number)
|
47
|
+
def normalize_brands(brands = [])
|
48
|
+
brands.uniq.each_with_index do |brand, index|
|
49
|
+
brands[index] = BRANDS_ALIASES[brand].present? ? BRANDS_ALIASES[brand] : brand
|
46
50
|
end
|
47
|
-
|
51
|
+
brands
|
48
52
|
end
|
53
|
+
|
49
54
|
end
|
50
|
-
end
|
51
55
|
|
56
|
+
end
|
52
57
|
end
|
53
58
|
end
|
@@ -10,7 +10,7 @@ module ActiveModel
|
|
10
10
|
country = 'us'
|
11
11
|
end
|
12
12
|
end
|
13
|
-
@formats = PostalCodeValidator.known_formats[country.to_s]
|
13
|
+
@formats = PostalCodeValidator.known_formats[country.to_s.downcase]
|
14
14
|
raise "No known postal code formats for country #{country}" unless @formats
|
15
15
|
record.errors.add(attribute) if value.blank? || !matches_any?
|
16
16
|
end
|
@@ -21,7 +21,6 @@ module ActiveModel
|
|
21
21
|
'ar' => ['####', '@####@@@'],
|
22
22
|
'at' => ['####'],
|
23
23
|
'au' => ['####'],
|
24
|
-
'bg' => ['####'],
|
25
24
|
'be' => ['####'],
|
26
25
|
'bg' => ['####'],
|
27
26
|
'br' => ['#####-###', '########'],
|
@@ -70,7 +69,6 @@ module ActiveModel
|
|
70
69
|
'sm' => ['4789#', '#'],
|
71
70
|
'th' => ['#####'],
|
72
71
|
'tr' => ['#####'],
|
73
|
-
'tr' => ['#####'],
|
74
72
|
'us' => ['#####', '#####-####'],
|
75
73
|
'wf' => ['#####'],
|
76
74
|
'za' => ['####']
|
@@ -3,31 +3,31 @@ ActiveValidators.activate(:credit_card)
|
|
3
3
|
|
4
4
|
describe "Credit Card Validation" do
|
5
5
|
# Here are some valid credit cards
|
6
|
-
VALID_CARDS =
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
6
|
+
VALID_CARDS =
|
7
|
+
{
|
8
|
+
#American Express
|
9
|
+
:amex => '3400 0000 0000 009',
|
10
|
+
#Carte Blanche
|
11
|
+
:carte_blanche => '3800 0000 0000 06',
|
12
|
+
#Discover
|
13
|
+
:discover => '6011 0000 0000 0004',
|
14
|
+
#Diners Club
|
15
|
+
:diners_club => '3852 0000 0232 37',
|
16
|
+
#JCB
|
17
|
+
:jcb => '3530 1113 3330 0000',
|
18
|
+
#MasterCard
|
19
|
+
:master_card => '5500 0000 0000 0004',
|
20
|
+
|
21
|
+
:mastercard => '5500 0000 0000 0004',
|
22
|
+
#Solo
|
23
|
+
:solo => '6334 0000 0000 0004',
|
24
|
+
#maestro
|
25
|
+
:maestro => '6759 6498 2643 8453',
|
26
|
+
#Visa
|
27
|
+
:visa => '4111 1111 1111 1111',
|
28
|
+
#Laser
|
29
|
+
:laser => '6304 1000 0000 0008'
|
30
|
+
}
|
31
31
|
|
32
32
|
VALID_CARDS.each_pair do |card, number|
|
33
33
|
describe "it accepts #{card} cards" do
|
@@ -46,6 +46,17 @@ describe "Credit Card Validation" do
|
|
46
46
|
end
|
47
47
|
end
|
48
48
|
|
49
|
+
describe "using multiple card types" do
|
50
|
+
it "accepts card if one of type valid" do
|
51
|
+
subject = build_card_record({:card => VALID_CARDS[:amex]}, {:type => [:visa, :master_card, :amex]})
|
52
|
+
assert card_is_valid?(subject)
|
53
|
+
end
|
54
|
+
|
55
|
+
it "rejects card if none of type valid" do
|
56
|
+
subject = build_card_record({:card => VALID_CARDS[:solo]}, {:type => [:visa, :master_card, :amex]})
|
57
|
+
assert card_is_invalid?(subject)
|
58
|
+
end
|
59
|
+
end
|
49
60
|
describe "for invalid cards" do
|
50
61
|
it "rejects invalid cards and generates an error message of type invalid" do
|
51
62
|
subject = build_card_record :card => '99999'
|
@@ -55,7 +66,7 @@ describe "Credit Card Validation" do
|
|
55
66
|
|
56
67
|
def build_card_record(attrs = {}, validator = {:type => :any})
|
57
68
|
TestRecord.reset_callbacks(:validate)
|
58
|
-
TestRecord.validates :card,
|
69
|
+
TestRecord.validates :card, :credit_card => validator
|
59
70
|
TestRecord.new attrs
|
60
71
|
end
|
61
72
|
|
@@ -69,4 +80,5 @@ describe "Credit Card Validation" do
|
|
69
80
|
subject.errors.size.must_equal 1
|
70
81
|
subject.errors[:card].include?(subject.errors.generate_message(:card, :invalid)).must_equal true
|
71
82
|
end
|
83
|
+
|
72
84
|
end
|
@@ -27,12 +27,19 @@ describe "Postal Code Validation" do
|
|
27
27
|
ActiveModel::Validations::PostalCodeValidator.known_formats.each do |country, formats|
|
28
28
|
describe "when given a :#{country} country parameter" do
|
29
29
|
formats.each do |format|
|
30
|
-
it "should validate format of postal code with #{format}" do
|
30
|
+
it "should validate format of lowercase postal code with #{format}" do
|
31
31
|
subject = build_postal_code_record :country => country
|
32
32
|
subject.postal_code = ActiveValidators::OneNineShims::OneNineString.new(format).gsub(/[@#]/, '@' => 'A', '#' => '9')
|
33
33
|
subject.valid?.must_equal true
|
34
34
|
subject.errors.size.must_equal 0
|
35
35
|
end
|
36
|
+
|
37
|
+
it "should validate format of upcase postal code with #{format}" do
|
38
|
+
subject = build_postal_code_record :country => country.upcase
|
39
|
+
subject.postal_code = ActiveValidators::OneNineShims::OneNineString.new(format).gsub(/[@#]/, '@' => 'A', '#' => '9')
|
40
|
+
subject.valid?.must_equal true
|
41
|
+
subject.errors.size.must_equal 0
|
42
|
+
end
|
36
43
|
end
|
37
44
|
end
|
38
45
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: activevalidators
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 3.
|
4
|
+
version: 3.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Franck Verrot
|
@@ -36,7 +36,7 @@ cert_chain:
|
|
36
36
|
UeH0jxnbT6lYw622u74Z7Dd6iQfaOy1h+iJxnCQglf70rs9bS665Nr0QvvrbW8Hz
|
37
37
|
Vr/YT3S8RkdBsIdM
|
38
38
|
-----END CERTIFICATE-----
|
39
|
-
date:
|
39
|
+
date: 2015-03-17 00:00:00.000000000 Z
|
40
40
|
dependencies:
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: bundler
|
@@ -112,14 +112,14 @@ dependencies:
|
|
112
112
|
name: countries
|
113
113
|
requirement: !ruby/object:Gem::Requirement
|
114
114
|
requirements:
|
115
|
-
- - "
|
115
|
+
- - ">="
|
116
116
|
- !ruby/object:Gem::Version
|
117
117
|
version: 0.9.3
|
118
118
|
type: :runtime
|
119
119
|
prerelease: false
|
120
120
|
version_requirements: !ruby/object:Gem::Requirement
|
121
121
|
requirements:
|
122
|
-
- - "
|
122
|
+
- - ">="
|
123
123
|
- !ruby/object:Gem::Version
|
124
124
|
version: 0.9.3
|
125
125
|
- !ruby/object:Gem::Dependency
|
@@ -150,6 +150,20 @@ dependencies:
|
|
150
150
|
- - ">="
|
151
151
|
- !ruby/object:Gem::Version
|
152
152
|
version: '0'
|
153
|
+
- !ruby/object:Gem::Dependency
|
154
|
+
name: credit_card_validations
|
155
|
+
requirement: !ruby/object:Gem::Requirement
|
156
|
+
requirements:
|
157
|
+
- - "~>"
|
158
|
+
- !ruby/object:Gem::Version
|
159
|
+
version: 2.0.2
|
160
|
+
type: :runtime
|
161
|
+
prerelease: false
|
162
|
+
version_requirements: !ruby/object:Gem::Requirement
|
163
|
+
requirements:
|
164
|
+
- - "~>"
|
165
|
+
- !ruby/object:Gem::Version
|
166
|
+
version: 2.0.2
|
153
167
|
description: ActiveValidators is a collection of ActiveModel/ActiveRecord validations
|
154
168
|
email:
|
155
169
|
- franck@verrot.fr
|
@@ -167,6 +181,7 @@ files:
|
|
167
181
|
- activevalidators.gemspec
|
168
182
|
- certs/franckverrot.pem
|
169
183
|
- checksums/3.1.2.sha512
|
184
|
+
- checksums/3.2.0.sha512
|
170
185
|
- lib/active_validators/active_model/validations/barcode_validator.rb
|
171
186
|
- lib/active_validators/active_model/validations/credit_card_validator.rb
|
172
187
|
- lib/active_validators/active_model/validations/date_validator.rb
|
@@ -229,7 +244,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
229
244
|
version: '0'
|
230
245
|
requirements: []
|
231
246
|
rubyforge_project:
|
232
|
-
rubygems_version: 2.
|
247
|
+
rubygems_version: 2.4.5
|
233
248
|
signing_key:
|
234
249
|
specification_version: 4
|
235
250
|
summary: Collection of ActiveModel/ActiveRecord validations
|
metadata.gz.sig
CHANGED
Binary file
|