activevalidators 1.4.0 → 1.5.0
Sign up to get free protection for your applications and to get access to all the features.
data/README.md
CHANGED
@@ -42,6 +42,10 @@ In your models, the gem provides new validators like `email`, or `url`:
|
|
42
42
|
validates :credit_card, :credit_card => { :type => :any }
|
43
43
|
end
|
44
44
|
|
45
|
+
class Order
|
46
|
+
validates :tracking_num, :tracking_number => { :carrier => :ups }
|
47
|
+
end
|
48
|
+
|
45
49
|
|
46
50
|
Exhaustive list of supported validators and their implementation:
|
47
51
|
|
@@ -55,6 +59,7 @@ Exhaustive list of supported validators and their implementation:
|
|
55
59
|
* `date` : based on the `DateValidator` gem
|
56
60
|
* `password` : based on a set of regular expressions
|
57
61
|
* `postal_code`: based on a set of predefined masks
|
62
|
+
* `tracking_number`: based on a set of predefined masks
|
58
63
|
|
59
64
|
Todo
|
60
65
|
----
|
data/activevalidators.gemspec
CHANGED
@@ -4,7 +4,7 @@ $:.unshift lib unless $:.include?(lib)
|
|
4
4
|
|
5
5
|
Gem::Specification.new do |s|
|
6
6
|
s.name = "activevalidators"
|
7
|
-
s.version = '1.
|
7
|
+
s.version = '1.5.0'
|
8
8
|
s.platform = Gem::Platform::RUBY
|
9
9
|
s.authors = ["Franck Verrot", "Paco Guzmán", "Oriol Gual", "Garrett Bjerkhoel", "Renato Riccieri Santos Zannon", "Brian Moseley"]
|
10
10
|
s.email = ["franck@verrot.fr"]
|
@@ -0,0 +1,32 @@
|
|
1
|
+
module ActiveModel
|
2
|
+
module Validations
|
3
|
+
class TrackingNumberValidator < EachValidator
|
4
|
+
def validate_each(record, attribute, value)
|
5
|
+
@value = value
|
6
|
+
carrier = options[:carrier]
|
7
|
+
raise "Carrier option required" unless carrier
|
8
|
+
@formats = TrackingNumberValidator.known_formats[carrier]
|
9
|
+
raise "No known tracking number formats for carrier #{carrier}" unless @formats
|
10
|
+
record.errors.add(attribute) unless matches_any?
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.known_formats
|
14
|
+
@@known_formats ||= {
|
15
|
+
# see https://www.ups.com/content/us/en/tracking/help/tracking/tnh.html
|
16
|
+
:ups => ['1Z################', '############', 'T##########', '#########'],
|
17
|
+
}
|
18
|
+
end
|
19
|
+
|
20
|
+
def matches_any?
|
21
|
+
false if @formats.nil? or not @formats.respond_to?(:detect)
|
22
|
+
@formats.detect { |format| @value.match(TrackingNumberValidator.regexp_from format) }
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def self.regexp_from(format)
|
28
|
+
Regexp.new "^"+(Regexp.escape format).gsub('\#','\d')+"$"
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
data/lib/activevalidators.rb
CHANGED
@@ -9,7 +9,8 @@ module ActiveModel
|
|
9
9
|
extend ActiveSupport::Autoload
|
10
10
|
|
11
11
|
def self.activevalidators
|
12
|
-
['Email','Url','RespondTo','Phone','Slug','Ip','CreditCard','Date','Password','Twitter','PostalCode'
|
12
|
+
['Email','Url','RespondTo','Phone','Slug','Ip','CreditCard','Date','Password','Twitter','PostalCode',
|
13
|
+
'TrackingNumber']
|
13
14
|
end
|
14
15
|
|
15
16
|
#Eager autoload the library's validators into AR::Validations
|
data/spec/spec_helper.rb
CHANGED
@@ -0,0 +1,66 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), '..', 'spec_helper.rb')
|
2
|
+
|
3
|
+
describe "Tracking Number Validation" do
|
4
|
+
|
5
|
+
subject { TestRecord.new }
|
6
|
+
|
7
|
+
context "when no carrier parameter is given" do
|
8
|
+
before(:each) do
|
9
|
+
TestRecord.reset_callbacks(:validate)
|
10
|
+
TestRecord.validates :tracking_number, :tracking_number => true
|
11
|
+
end
|
12
|
+
|
13
|
+
it "raises an exception" do
|
14
|
+
lambda { subject.valid? }.should raise_error
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
context "when given a ups carrier parameter" do
|
19
|
+
before(:each) do
|
20
|
+
TestRecord.reset_callbacks(:validate)
|
21
|
+
TestRecord.validates :tracking_number, :tracking_number => {:carrier => :ups}
|
22
|
+
end
|
23
|
+
|
24
|
+
it 'should validate format of tracking number with 1Z################' do
|
25
|
+
subject.tracking_number = '1Z9999999999999999'
|
26
|
+
subject.should be_valid
|
27
|
+
subject.should have(0).errors
|
28
|
+
end
|
29
|
+
|
30
|
+
it 'should validate format of tracking number with ############' do
|
31
|
+
subject.tracking_number = '999999999999'
|
32
|
+
subject.should be_valid
|
33
|
+
subject.should have(0).errors
|
34
|
+
end
|
35
|
+
|
36
|
+
it 'should validate format of tracking number with T##########' do
|
37
|
+
subject.tracking_number = 'T9999999999'
|
38
|
+
subject.should be_valid
|
39
|
+
subject.should have(0).errors
|
40
|
+
end
|
41
|
+
|
42
|
+
it 'should validate format of tracking number with #########' do
|
43
|
+
subject.tracking_number = '999999999'
|
44
|
+
subject.should be_valid
|
45
|
+
subject.should have(0).errors
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
describe "for invalid formats" do
|
50
|
+
before :each do
|
51
|
+
TestRecord.reset_callbacks(:validate)
|
52
|
+
TestRecord.validates :tracking_number, :tracking_number => {:carrier => :ups}
|
53
|
+
subject.tracking_number = '999'
|
54
|
+
end
|
55
|
+
|
56
|
+
it "rejects invalid formats" do
|
57
|
+
subject.should_not be_valid
|
58
|
+
subject.should have(1).error
|
59
|
+
end
|
60
|
+
|
61
|
+
it "generates an error message of type invalid" do
|
62
|
+
subject.should_not be_valid
|
63
|
+
subject.errors[:tracking_number].should include subject.errors.generate_message(:tracking_number, :invalid)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: activevalidators
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 3
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 1
|
8
|
-
-
|
8
|
+
- 5
|
9
9
|
- 0
|
10
|
-
version: 1.
|
10
|
+
version: 1.5.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Franck Verrot
|
@@ -20,7 +20,7 @@ autorequire:
|
|
20
20
|
bindir: bin
|
21
21
|
cert_chain: []
|
22
22
|
|
23
|
-
date: 2011-
|
23
|
+
date: 2011-05-13 00:00:00 +02:00
|
24
24
|
default_executable:
|
25
25
|
dependencies:
|
26
26
|
- !ruby/object:Gem::Dependency
|
@@ -169,6 +169,7 @@ files:
|
|
169
169
|
- lib/active_model/validations/postal_code_validator.rb
|
170
170
|
- lib/active_model/validations/respond_to_validator.rb
|
171
171
|
- lib/active_model/validations/slug_validator.rb
|
172
|
+
- lib/active_model/validations/tracking_number_validator.rb
|
172
173
|
- lib/active_model/validations/twitter_validator.rb
|
173
174
|
- lib/active_model/validations/url_validator.rb
|
174
175
|
- lib/activevalidators.rb
|
@@ -182,6 +183,7 @@ files:
|
|
182
183
|
- spec/validations/postal_code_spec.rb
|
183
184
|
- spec/validations/respond_to_spec.rb
|
184
185
|
- spec/validations/slug_spec.rb
|
186
|
+
- spec/validations/tracking_number_spec.rb
|
185
187
|
- spec/validations/twitter_spec.rb
|
186
188
|
- spec/validations/url_spec.rb
|
187
189
|
has_rdoc: true
|
@@ -214,7 +216,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
214
216
|
requirements: []
|
215
217
|
|
216
218
|
rubyforge_project:
|
217
|
-
rubygems_version: 1.6.
|
219
|
+
rubygems_version: 1.6.2
|
218
220
|
signing_key:
|
219
221
|
specification_version: 3
|
220
222
|
summary: Collection of ActiveModel/ActiveRecord validations
|
@@ -229,5 +231,6 @@ test_files:
|
|
229
231
|
- spec/validations/postal_code_spec.rb
|
230
232
|
- spec/validations/respond_to_spec.rb
|
231
233
|
- spec/validations/slug_spec.rb
|
234
|
+
- spec/validations/tracking_number_spec.rb
|
232
235
|
- spec/validations/twitter_spec.rb
|
233
236
|
- spec/validations/url_spec.rb
|