activevalidators 1.2.0 → 1.2.1

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.
data/.gitignore CHANGED
@@ -1,2 +1,3 @@
1
1
  *.swp
2
2
  *.gem
3
+ *.rbc
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- activevalidators (1.2.0)
4
+ activevalidators (1.2.1)
5
5
  activemodel (>= 3.0.0)
6
6
  activerecord (>= 3.0.0)
7
7
  date_validator (= 0.5.9)
@@ -46,6 +46,7 @@ GEM
46
46
  tzinfo (0.3.23)
47
47
 
48
48
  PLATFORMS
49
+ java
49
50
  ruby
50
51
 
51
52
  DEPENDENCIES
@@ -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.2.0'
7
+ s.version = '1.2.1'
8
8
  s.platform = Gem::Platform::RUBY
9
9
  s.authors = ["Franck Verrot"]
10
10
  s.email = ["franck@verrot.fr"]
@@ -4,14 +4,12 @@ module ActiveModel
4
4
  class EmailValidator < EachValidator
5
5
  def validate_each(record,attribute,value)
6
6
  begin
7
- m = Mail::Address.new(value)
8
- r = m.domain && m.address == value
9
- t = m.__send__(:tree)
10
- r &&= (t.domain.dot_atom_text.elements.size > 1)
11
- rescue Exception => e
12
- r = false
7
+ address = Mail::Address.new(value)
8
+ valid = address.domain && value.include?(address.address)
9
+ rescue Mail::Field::ParseError
10
+ valid = false
13
11
  end
14
- record.errors.add(attribute) unless r
12
+ record.errors.add(attribute) unless valid
15
13
  end
16
14
  end
17
15
  end
@@ -1,13 +1,39 @@
1
1
  module ActiveModel
2
2
  module Validations
3
- require 'resolv'
4
3
  class IpValidator < EachValidator
5
4
  def validate_each(record, attribute, value)
6
- r = Resolv.const_get("IP#{options[:format]}")
7
- raise "Unknown IP validator format #{options[:format].inspect}" if r.nil?
8
- record.errors.add(attribute) unless !(r::Regex !~ value)
5
+ record.errors.add(attribute) unless regex.match(value)
9
6
  end
10
7
 
8
+ def check_validity!
9
+ raise ArgumentError, "Unknown IP validator format #{options[:format].inspect}" unless [:v4, :v6].include? options[:format]
10
+ end
11
+
12
+ private
13
+ def regex
14
+ case options[:format]
15
+ when :v4
16
+ ipv4_regex
17
+ when :v6
18
+ ipv6_regex
19
+ end
20
+ end
21
+
22
+ def ipv4_regex
23
+ # Extracted from ruby 1.9.2
24
+ regex256 =
25
+ /0
26
+ |1(?:[0-9][0-9]?)?
27
+ |2(?:[0-4][0-9]?|5[0-5]?|[6-9])?
28
+ |[3-9][0-9]?/x
29
+ /\A(#{regex256})\.(#{regex256})\.(#{regex256})\.(#{regex256})\z/
30
+ end
31
+
32
+ def ipv6_regex
33
+ require 'resolv'
34
+ Resolv::IPv6::Regex
35
+ end
36
+
11
37
  end
12
38
  end
13
39
  end
@@ -6,3 +6,8 @@ end
6
6
 
7
7
  RSpec.configure do |config|
8
8
  end
9
+
10
+ class TestRecord
11
+ include ActiveModel::Validations
12
+ attr_accessor :ip, :url, :slug, :responder, :global_condition, :local_condition, :phone, :email, :card
13
+ end
@@ -0,0 +1,62 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'spec_helper.rb')
2
+
3
+ describe "Credit Card Validation" do
4
+ before(:each) do
5
+ TestRecord.reset_callbacks(:validate)
6
+ TestRecord.validates :card, :credit_card => { :type => :any }
7
+ end
8
+
9
+ subject { TestRecord.new }
10
+
11
+ # Here are some valid credit cards
12
+ VALID_CARDS =
13
+ {
14
+ #American Express
15
+ :amex => '3400 0000 0000 009',
16
+ #Carte Blanche
17
+ :carte_blanche => '3000 0000 0000 04',
18
+ #Discover
19
+ :discover => '6011 0000 0000 0004',
20
+ #Diners Club
21
+ :diners_club => '3852 0000 0232 37',
22
+ #enRoute
23
+ :en_route => '2014 0000 0000 009',
24
+ #JCB
25
+ :jcb => '2131 0000 0000 0008',
26
+ #MasterCard
27
+ :master_card => '5500 0000 0000 0004',
28
+ #Solo
29
+ :solo => '6334 0000 0000 0004',
30
+ #Switch
31
+ :switch => '4903 0100 0000 0009',
32
+ #Visa
33
+ :visa => '4111 1111 1111 1111',
34
+ #Laser
35
+ :laser => '6304 1000 0000 0008'
36
+ }
37
+
38
+ VALID_CARDS.each_pair do |card, number|
39
+ it "accepts #{card} valid cards" do
40
+ subject.card = number
41
+ subject.should be_valid
42
+ subject.should have(0).errors
43
+ end
44
+ end
45
+
46
+ describe "for invalid cards" do
47
+
48
+ before :each do
49
+ subject.card = '99999'
50
+ end
51
+
52
+ it "rejects invalid cards" do
53
+ subject.should_not be_valid
54
+ subject.should have(1).error
55
+ end
56
+
57
+ it "generates an error message of type invalid" do
58
+ subject.should_not be_valid
59
+ subject.errors[:card].should include subject.errors.generate_message(:card, :invalid)
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,44 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'spec_helper.rb')
2
+
3
+ describe "Email Validation" do
4
+ before(:each) do
5
+ TestRecord.reset_callbacks(:validate)
6
+ TestRecord.validates :email, :email => true
7
+ end
8
+
9
+ subject { TestRecord.new }
10
+
11
+ it "accepts valid emails" do
12
+ subject.email = 'franck@verrot.fr'
13
+ subject.should be_valid
14
+ subject.should have(0).errors
15
+ end
16
+
17
+ it "accepts complete emails" do
18
+ subject.email = 'Mikel Lindsaar (My email address) <mikel@test.lindsaar.net>'
19
+ subject.should be_valid
20
+ subject.should have(0).errors
21
+ end
22
+
23
+ describe "for invalid emails" do
24
+ before :each do
25
+ subject.email = 'franck.fr'
26
+ end
27
+
28
+ it "rejects invalid emails" do
29
+ subject.should_not be_valid
30
+ subject.should have(1).error
31
+ end
32
+
33
+ it 'rejects local emails' do
34
+ subject.email = 'franck'
35
+ subject.should_not be_valid
36
+ subject.should have(1).error
37
+ end
38
+
39
+ it "generates an error message of type invalid" do
40
+ subject.should_not be_valid
41
+ subject.errors[:email].should include subject.errors.generate_message(:email, :invalid)
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,73 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'spec_helper.rb')
2
+
3
+ describe "IP Validation" do
4
+ before(:each) do
5
+ TestRecord.reset_callbacks(:validate)
6
+ end
7
+
8
+ subject { TestRecord.new }
9
+
10
+ describe "IPv4 Validation" do
11
+ before :each do
12
+ TestRecord.validates :ip, :ip => { :format => :v4 }
13
+ end
14
+
15
+ it "accepts valid IPs" do
16
+ subject.ip = '192.168.1.1'
17
+ subject.should be_valid
18
+ subject.should have(0).errors
19
+ end
20
+
21
+ context "for invalid IPs" do
22
+ before :each do
23
+ subject.ip = '267.34.56.3'
24
+ end
25
+
26
+ it "rejects invalid IPs" do
27
+ subject.should_not be_valid
28
+ subject.should have(1).error
29
+ end
30
+
31
+ it "generates an error message of type invalid" do
32
+ subject.should_not be_valid
33
+ subject.errors[:ip].should include subject.errors.generate_message(:ip, :invalid)
34
+ end
35
+ end
36
+ end
37
+
38
+ describe "IPv6 Validation" do
39
+ before :each do
40
+ TestRecord.validates :ip, :ip => { :format => :v6 }
41
+ end
42
+
43
+ it "accepts valid IPs" do
44
+ subject.ip = '::1'
45
+ subject.should be_valid
46
+ subject.should have(0).errors
47
+ end
48
+
49
+ context "for invalid IPs" do
50
+ before :each do
51
+ subject.ip = '192.168.1.1'
52
+ end
53
+
54
+ it "rejects invalid IPs" do
55
+ subject.should_not be_valid
56
+ subject.should have(1).error
57
+ end
58
+
59
+ it "generates an error message of type invalid" do
60
+ subject.should_not be_valid
61
+ subject.errors[:ip].should include subject.errors.generate_message(:ip, :invalid)
62
+ end
63
+ end
64
+ end
65
+
66
+ it "checks validity of the arguments" do
67
+ [3, "foo", 1..6].each do |wrong_argument|
68
+ expect {
69
+ TestRecord.validates :ip, :ip => { :format => wrong_argument }
70
+ }.to raise_error(ArgumentError, "Unknown IP validator format #{wrong_argument.inspect}")
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,57 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'spec_helper.rb')
2
+
3
+ describe "Phone Validation" do
4
+
5
+ before(:each) do
6
+ TestRecord.reset_callbacks(:validate)
7
+ TestRecord.validates :phone, :phone => true
8
+ end
9
+
10
+ subject { TestRecord.new }
11
+
12
+ it 'should validate format of phone with ###-###-####' do
13
+ subject.phone = '999-999-9999'
14
+ subject.should be_valid
15
+ subject.should have(0).errors
16
+ end
17
+
18
+ it 'should validate format of phone with ##########' do
19
+ subject.phone = '9999999999'
20
+ subject.should be_valid
21
+ subject.should have(0).errors
22
+ end
23
+
24
+ it 'should validate format of phone with ###.###.####' do
25
+ subject.phone = '999.999.9999'
26
+ subject.should be_valid
27
+ subject.should have(0).errors
28
+ end
29
+
30
+ it 'should validate format of phone with ### ### ####' do
31
+ subject.phone = '999 999 9999'
32
+ subject.should be_valid
33
+ subject.should have(0).errors
34
+ end
35
+
36
+ it 'should validate format of phone with (###) ###-####' do
37
+ subject.phone = '(999) 999-9999'
38
+ subject.should be_valid
39
+ subject.should have(0).errors
40
+ end
41
+
42
+ describe "for invalid formats" do
43
+ before :each do
44
+ subject.phone = '999'
45
+ end
46
+
47
+ it "rejects invalid formats" do
48
+ subject.should_not be_valid
49
+ subject.should have(1).error
50
+ end
51
+
52
+ it "generates an error message of type invalid" do
53
+ subject.should_not be_valid
54
+ subject.errors[:phone].should include subject.errors.generate_message(:phone, :invalid)
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,37 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'spec_helper.rb')
2
+
3
+ describe "Respond To Validation" do
4
+ before(:each) do
5
+ TestRecord.reset_callbacks(:validate)
6
+ TestRecord.validates :responder, :respond_to => { :call => true, :if => :local_condition }, :if => :global_condition
7
+ end
8
+
9
+ subject { TestRecord.new }
10
+
11
+ it "respond_to?" do
12
+ subject.responder = lambda {}
13
+ subject.global_condition = true
14
+ subject.local_condition = true
15
+
16
+ subject.should be_valid
17
+ subject.should have(0).errors
18
+ end
19
+
20
+ describe "when does not respond_to?" do
21
+ before :each do
22
+ subject.responder = 42
23
+ subject.global_condition = true
24
+ subject.local_condition = true
25
+ end
26
+
27
+ it "rejects the responder" do
28
+ subject.should_not be_valid
29
+ subject.should have(1).error
30
+ end
31
+
32
+ it "generates an error message of type invalid" do
33
+ subject.should_not be_valid
34
+ subject.errors[:responder].should include subject.errors.generate_message(:responder, :invalid)
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,32 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'spec_helper.rb')
2
+
3
+ describe "Slug Validation" do
4
+ before(:each) do
5
+ TestRecord.reset_callbacks(:validate)
6
+ TestRecord.validates :slug, :slug => true
7
+ end
8
+
9
+ subject { TestRecord.new }
10
+
11
+ it "accepts valid slugs" do
12
+ subject.slug = '1234567890-foo-bar-bar'
13
+ subject.should be_valid
14
+ subject.should have(0).errors
15
+ end
16
+
17
+ describe "for invalid slugs" do
18
+ before :each do
19
+ subject.slug = '@#$%^'
20
+ end
21
+
22
+ it "rejects invalid slugs" do
23
+ subject.should_not be_valid
24
+ subject.should have(1).error
25
+ end
26
+
27
+ it "generates an error message of type invalid" do
28
+ subject.should_not be_valid
29
+ subject.errors[:slug].should include subject.errors.generate_message(:slug, :invalid)
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,38 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'spec_helper.rb')
2
+
3
+ describe "Url Validation" do
4
+ before(:each) do
5
+ TestRecord.reset_callbacks(:validate)
6
+ TestRecord.validates :url, :url => true
7
+ end
8
+
9
+ subject { TestRecord.new }
10
+
11
+ it "accepts valid urls" do
12
+ subject.url = 'http://www.verrot.fr'
13
+ subject.should be_valid
14
+ subject.should have(0).errors
15
+ end
16
+
17
+ it "accepts valid SSL urls" do
18
+ subject.url = 'https://www.verrot.fr'
19
+ subject.should be_valid
20
+ subject.should have(0).errors
21
+ end
22
+
23
+ describe "for invalid urls" do
24
+ before :each do
25
+ subject.url = 'http://^^^^.fr'
26
+ end
27
+
28
+ it "rejects invalid urls" do
29
+ subject.should_not be_valid
30
+ subject.should have(1).error
31
+ end
32
+
33
+ it "generates an error message of type invalid" do
34
+ subject.should_not be_valid
35
+ subject.errors[:url].should include subject.errors.generate_message(:url, :invalid)
36
+ end
37
+ end
38
+ end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 1
7
7
  - 2
8
- - 0
9
- version: 1.2.0
8
+ - 1
9
+ version: 1.2.1
10
10
  platform: ruby
11
11
  authors:
12
12
  - Franck Verrot
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-12-04 00:00:00 +01:00
17
+ date: 2010-12-05 00:00:00 +01:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -154,22 +154,14 @@ files:
154
154
  - lib/active_model/validations/slug_validator.rb
155
155
  - lib/active_model/validations/url_validator.rb
156
156
  - lib/activevalidators.rb
157
- - spec/models/credit_card_validator_model.rb
158
- - spec/models/email_validator_model.rb
159
- - spec/models/ip_validator_model.rb
160
- - spec/models/phone_validator_model.rb
161
- - spec/models/respond_to_validator_model.rb
162
- - spec/models/slug_validator_model.rb
163
- - spec/models/url_validator_model.rb
164
157
  - spec/spec_helper.rb
165
- - spec/specs/credit_card_spec.rb
166
- - spec/specs/date_validator_spec.rb
167
- - spec/specs/email_spec.rb
168
- - spec/specs/ip_spec.rb
169
- - spec/specs/phone_spec.rb
170
- - spec/specs/respond_to_spec.rb
171
- - spec/specs/slug_spec.rb
172
- - spec/specs/url_spec.rb
158
+ - spec/validations/credit_card_spec.rb
159
+ - spec/validations/email_spec.rb
160
+ - spec/validations/ip_spec.rb
161
+ - spec/validations/phone_spec.rb
162
+ - spec/validations/respond_to_spec.rb
163
+ - spec/validations/slug_spec.rb
164
+ - spec/validations/url_spec.rb
173
165
  has_rdoc: true
174
166
  homepage: http://github.com/cesario/activevalidators
175
167
  licenses: []
@@ -203,19 +195,11 @@ signing_key:
203
195
  specification_version: 3
204
196
  summary: Collection of ActiveModel/ActiveRecord validations
205
197
  test_files:
206
- - spec/models/credit_card_validator_model.rb
207
- - spec/models/email_validator_model.rb
208
- - spec/models/ip_validator_model.rb
209
- - spec/models/phone_validator_model.rb
210
- - spec/models/respond_to_validator_model.rb
211
- - spec/models/slug_validator_model.rb
212
- - spec/models/url_validator_model.rb
213
198
  - spec/spec_helper.rb
214
- - spec/specs/credit_card_spec.rb
215
- - spec/specs/date_validator_spec.rb
216
- - spec/specs/email_spec.rb
217
- - spec/specs/ip_spec.rb
218
- - spec/specs/phone_spec.rb
219
- - spec/specs/respond_to_spec.rb
220
- - spec/specs/slug_spec.rb
221
- - spec/specs/url_spec.rb
199
+ - spec/validations/credit_card_spec.rb
200
+ - spec/validations/email_spec.rb
201
+ - spec/validations/ip_spec.rb
202
+ - spec/validations/phone_spec.rb
203
+ - spec/validations/respond_to_spec.rb
204
+ - spec/validations/slug_spec.rb
205
+ - spec/validations/url_spec.rb
@@ -1,7 +0,0 @@
1
- module Models
2
- class CreditCardValidatorModel
3
- include ActiveModel::Validations
4
- attr_accessor :card
5
- validates :card, :credit_card => { :type => :any }, :if => :card
6
- end
7
- end
@@ -1,7 +0,0 @@
1
- module Models
2
- class EmailValidatorModel
3
- include ActiveModel::Validations
4
- attr_accessor :email
5
- validates :email, :email => true
6
- end
7
- end
@@ -1,8 +0,0 @@
1
- module Models
2
- class IpValidatorModel
3
- include ActiveModel::Validations
4
- attr_accessor :ipv4, :ipv6
5
- validates :ipv4, :ip => { :format => :v4 }, :if => :ipv4
6
- validates :ipv6, :ip => { :format => :v6 }, :if => :ipv6
7
- end
8
- end
@@ -1,7 +0,0 @@
1
- module Models
2
- class PhoneValidatorModel
3
- include ActiveModel::Validations
4
- attr_accessor :phone
5
- validates :phone, :phone => true
6
- end
7
- end
@@ -1,7 +0,0 @@
1
- module Models
2
- class RespondToValidatorModel
3
- include ActiveModel::Validations
4
- attr_accessor :responder, :global_condition, :local_condition
5
- validates :responder, :respond_to => { :call => true, :if => :local_condition }, :if => :global_condition
6
- end
7
- end
@@ -1,7 +0,0 @@
1
- module Models
2
- class SlugValidatorModel
3
- include ActiveModel::Validations
4
- attr_accessor :slug
5
- validates :slug, :slug => true
6
- end
7
- end
@@ -1,7 +0,0 @@
1
- module Models
2
- class UrlValidatorModel
3
- include ActiveModel::Validations
4
- attr_accessor :url
5
- validates :url, :url => true
6
- end
7
- end
@@ -1,56 +0,0 @@
1
- require File.join(File.dirname(__FILE__), '..', 'spec_helper.rb')
2
-
3
- # Here are some valid credit cards
4
- CARDS = {
5
- #American Express
6
- :amex => '3400 0000 0000 009',
7
- #Carte Blanche
8
- :carte_blanche => '3000 0000 0000 04',
9
- #Discover
10
- :discover => '6011 0000 0000 0004',
11
- #Diners Club
12
- :diners_club => '3852 0000 0232 37',
13
- #enRoute
14
- :en_route => '2014 0000 0000 009',
15
- #JCB
16
- :jcb => '2131 0000 0000 0008',
17
- #MasterCard
18
- :master_card => '5500 0000 0000 0004',
19
- #Solo
20
- :solo => '6334 0000 0000 0004',
21
- #Switch
22
- :switch => '4903 0100 0000 0009',
23
- #Visa
24
- :visa => '4111 1111 1111 1111',
25
- #Laser
26
- :laser => '6304 1000 0000 0008'
27
- }
28
-
29
- describe "Credit Card Validation" do
30
- CARDS.each_pair do |card, number|
31
- it "accepts #{card} valid cards" do
32
- model = Models::CreditCardValidatorModel.new
33
- model.card = number
34
- model.valid?.should be(true)
35
- model.should have(0).errors
36
- end
37
- end
38
-
39
- describe "for invalid cards" do
40
- let(:model) do
41
- Models::CreditCardValidatorModel.new.tap do |m|
42
- m.card = '99999'
43
- end
44
- end
45
-
46
- it "rejects invalid cards" do
47
- model.valid?.should be(false)
48
- model.should have(1).errors
49
- end
50
-
51
- it "generates an error message of type invalid" do
52
- model.valid?.should be(false)
53
- model.errors[:card].should == [model.errors.generate_message(:card, :invalid)]
54
- end
55
- end
56
- end
@@ -1,124 +0,0 @@
1
- #Copied from DateValidator v0.5.9
2
- #The translation stuff has been removed though
3
- require File.join(File.dirname(__FILE__), '..', 'spec_helper.rb')
4
-
5
- require 'active_support/time' # For testing Date and TimeWithZone objects
6
-
7
- class TestRecord
8
- include ActiveModel::Validations
9
- attr_accessor :expiration_date
10
-
11
- def initialize(expiration_date)
12
- @expiration_date = expiration_date
13
- end
14
- end
15
-
16
- describe "DateValidator" do
17
-
18
- before(:each) do
19
- TestRecord.reset_callbacks(:validate)
20
- end
21
-
22
- it "checks validity of the arguments" do
23
- [3, "foo", 1..6].each do |wrong_argument|
24
- expect {
25
- TestRecord.validates :expiration_date, :date => {:before => wrong_argument}
26
- }.to raise_error(ArgumentError, ":before must be a time, a date, a time_with_zone, a symbol or a proc")
27
- end
28
- end
29
-
30
- it "complains if provided with no options" do
31
- TestRecord.validates :expiration_date, :date => {:before => Time.now}
32
- model = TestRecord.new(nil)
33
- model.should_not be_valid
34
- end
35
-
36
- [:valid,:invalid].each do |should_be|
37
-
38
- _context = should_be == :valid ? 'when value validates correctly' : 'when value does not match validation requirements'
39
-
40
- context _context do
41
-
42
- [:after, :before, :after_or_equal_to, :before_or_equal_to].each do |check|
43
-
44
- now = Time.now.to_datetime
45
-
46
- model_date = case check
47
- when :after then should_be == :valid ? now + 21000 : now - 1
48
- when :before then should_be == :valid ? now - 21000 : now + 1
49
- when :after_or_equal_to then should_be == :valid ? now : now - 21000
50
- when :before_or_equal_to then should_be == :valid ? now : now + 21000
51
- end
52
-
53
- # it "ensures that an attribute is #{should_be} when #{should_be == :valid ? 'respecting' : 'offending' } the #{check} check" do
54
- #TestRecord.validates :expiration_date, :date => {:"#{check}" => Time.now}
55
- #model = TestRecord.new(model_date)
56
- #should_be == :valid ? model.should(be_valid, "an attribute should be valid when respecting the #{check} check") : model.should_not(be_valid, "an attribute should be invalidwhen offending the #{check} check")
57
- #end
58
-
59
- if _context == 'when value does not match validation requirements'
60
-
61
- it "yields a default error message indicating that value must be #{check} validation requirements" do
62
- TestRecord.validates :expiration_date, :date => {:"#{check}" => Time.now}
63
- model = TestRecord.new(model_date)
64
- model.should_not be_valid
65
- end
66
-
67
- end
68
-
69
- end
70
-
71
- if _context == 'when value does not match validation requirements'
72
-
73
- it "allows for a custom validation message" do
74
- TestRecord.validates :expiration_date, :date => {:before_or_equal_to => Time.now, :message => 'must be after Christmas'}
75
- model = TestRecord.new(Time.now + 21000)
76
- model.should_not be_valid
77
- end
78
-
79
- end
80
-
81
- end
82
-
83
- end
84
-
85
- extra_types = [:proc, :symbol]
86
- extra_types.push(:date) if defined?(Date) and defined?(DateTime)
87
- extra_types.push(:time_with_zone) if defined?(ActiveSupport::TimeWithZone)
88
-
89
- extra_types.each do |type|
90
- it "accepts a #{type} as an argument to a check" do
91
- case type
92
- when :proc then
93
- expect {
94
- TestRecord.validates :expiration_date, :date => {:after => Proc.new{Time.now + 21000}}
95
- }.to_not raise_error
96
- when :symbol then
97
- expect {
98
- TestRecord.send(:define_method, :min_date, lambda { Time.now + 21000 })
99
- TestRecord.validates :expiration_date, :date => {:after => :min_date}
100
- }.to_not raise_error
101
- when :date then
102
- expect {
103
- TestRecord.validates :expiration_date, :date => {:after => Time.now.to_date}
104
- }.to_not raise_error
105
- when :time_with_zone then
106
- expect {
107
- Time.zone = "Hawaii"
108
- TestRecord.validates :expiration_date, :date => {:before => Time.zone.parse((Time.now + 21000).to_s)}
109
- }.to_not raise_error
110
- end
111
- end
112
- end
113
-
114
- it "gracefully handles an unexpected result from a proc argument evaluation" do
115
- TestRecord.validates :expiration_date, :date => {:after => Proc.new{ nil }}
116
- TestRecord.new(Time.now).should_not be_valid
117
- end
118
-
119
- it "gracefully handles an unexpected result from a symbol argument evaluation" do
120
- TestRecord.send(:define_method, :min_date, lambda { nil })
121
- TestRecord.validates :expiration_date, :date => {:after => :min_date}
122
- TestRecord.new(Time.now).should_not be_valid
123
- end
124
- end
@@ -1,28 +0,0 @@
1
- require File.join(File.dirname(__FILE__), '..', 'spec_helper.rb')
2
-
3
- describe "Email Validation" do
4
- it "accepts valid emails" do
5
- model = Models::EmailValidatorModel.new
6
- model.email = 'franck@verrot.fr'
7
- model.valid?.should be(true)
8
- model.should have(0).errors
9
- end
10
-
11
- describe "for invalid emails" do
12
- let(:model) do
13
- Models::EmailValidatorModel.new.tap do |m|
14
- m.email = 'franck.fr'
15
- end
16
- end
17
-
18
- it "rejects invalid emails" do
19
- model.valid?.should be(false)
20
- model.should have(1).errors
21
- end
22
-
23
- it "generates an error message of type invalid" do
24
- model.valid?.should be(false)
25
- model.errors[:email].should == [model.errors.generate_message(:email, :invalid)]
26
- end
27
- end
28
- end
@@ -1,57 +0,0 @@
1
- require File.join(File.dirname(__FILE__), '..', 'spec_helper.rb')
2
-
3
- describe "IP Validation" do
4
- describe "IPv4 Validation" do
5
- it "accepts valid IPs" do
6
- model = Models::IpValidatorModel.new
7
- model.ipv4 = '192.168.1.1'
8
- model.valid?.should be(true)
9
- model.should have(0).errors
10
- end
11
-
12
- describe "for invalid IPs" do
13
- let(:model) do
14
- Models::IpValidatorModel.new.tap do |m|
15
- m.ipv4 = '267.34.56.3'
16
- end
17
- end
18
-
19
- it "rejects invalid IPs" do
20
- model.valid?.should be(false)
21
- model.should have(1).errors
22
- end
23
-
24
- it "generates an error message of type invalid" do
25
- model.valid?.should be(false)
26
- model.errors[:ipv4].should == [model.errors.generate_message(:ipv4, :invalid)]
27
- end
28
- end
29
- end
30
-
31
- describe "IPv6 Validation" do
32
- it "accepts valid IPs" do
33
- model = Models::IpValidatorModel.new
34
- model.ipv6 = '::1'
35
- model.valid?.should be(true)
36
- model.should have(0).errors
37
- end
38
-
39
- describe "for invalid IPs" do
40
- let(:model) do
41
- Models::IpValidatorModel.new.tap do |m|
42
- m.ipv6 = '192.168.1.1'
43
- end
44
- end
45
-
46
- it "rejects invalid IPs" do
47
- model.valid?.should be(false)
48
- model.should have(1).errors
49
- end
50
-
51
- it "generates an error message of type invalid" do
52
- model.valid?.should be(false)
53
- model.errors[:ipv6].should == [model.errors.generate_message(:ipv6, :invalid)]
54
- end
55
- end
56
- end
57
- end
@@ -1,57 +0,0 @@
1
- require File.join(File.dirname(__FILE__), '..', 'spec_helper.rb')
2
-
3
- describe "Phone Validation" do
4
-
5
- it 'should validate format of phone with ###-###-####' do
6
- model = Models::PhoneValidatorModel.new
7
- model.phone = '999-999-9999'
8
- model.valid?.should be(true)
9
- model.should have(0).errors
10
- end
11
-
12
- it 'should validate format of phone with ##########' do
13
- model = Models::PhoneValidatorModel.new
14
- model.phone = '9999999999'
15
- model.valid?.should be(true)
16
- model.should have(0).errors
17
- end
18
-
19
- it 'should validate format of phone with ###.###.####' do
20
- model = Models::PhoneValidatorModel.new
21
- model.phone = '999.999.9999'
22
- model.valid?.should be(true)
23
- model.should have(0).errors
24
- end
25
-
26
- it 'should validate format of phone with ### ### ####' do
27
- model = Models::PhoneValidatorModel.new
28
- model.phone = '999 999 9999'
29
- model.valid?.should be(true)
30
- model.should have(0).errors
31
- end
32
-
33
- it 'should validate format of phone with (###) ###-####' do
34
- model = Models::PhoneValidatorModel.new
35
- model.phone = '(999) 999-9999'
36
- model.valid?.should be(true)
37
- model.should have(0).errors
38
- end
39
-
40
- describe "for invalid formats" do
41
- let(:model) do
42
- Models::PhoneValidatorModel.new.tap do |m|
43
- m.phone = '999'
44
- end
45
- end
46
-
47
- it "rejects invalid formats" do
48
- model.valid?.should be(false)
49
- model.should have(1).errors
50
- end
51
-
52
- it "generates an error message of type invalid" do
53
- model.valid?.should be(false)
54
- model.errors[:phone].should == [model.errors.generate_message(:phone, :invalid)]
55
- end
56
- end
57
- end
@@ -1,33 +0,0 @@
1
- require File.join(File.dirname(__FILE__), '..', 'spec_helper.rb')
2
-
3
- describe "Respond To Validation" do
4
- it "respond_to?" do
5
- model = Models::RespondToValidatorModel.new
6
- model.responder = lambda {}
7
- model.global_condition = true
8
- model.local_condition = true
9
-
10
- model.valid?.should be(true)
11
- model.should have(0).errors
12
- end
13
-
14
- describe "when does not respond_to?" do
15
- let(:model) do
16
- Models::RespondToValidatorModel.new.tap do |m|
17
- m.responder = 42
18
- m.global_condition = true
19
- m.local_condition = true
20
- end
21
- end
22
-
23
- it "rejects the responder" do
24
- model.valid?.should be(false)
25
- model.should have(1).errors
26
- end
27
-
28
- it "generates an error message of type invalid" do
29
- model.valid?.should be(false)
30
- model.errors[:responder].should == [model.errors.generate_message(:responder, :invalid)]
31
- end
32
- end
33
- end
@@ -1,28 +0,0 @@
1
- require File.join(File.dirname(__FILE__), '..', 'spec_helper.rb')
2
-
3
- describe "Slug Validation" do
4
- it "accepts valid slugs" do
5
- model = Models::SlugValidatorModel.new
6
- model.slug = '1234567890-foo-bar-bar'
7
- model.valid?.should be(true)
8
- model.should have(0).errors
9
- end
10
-
11
- describe "for invalid slugs" do
12
- let(:model) do
13
- Models::SlugValidatorModel.new.tap do |m|
14
- m.slug = '@#$%^'
15
- end
16
- end
17
-
18
- it "rejects invalid slugs" do
19
- model.valid?.should be(false)
20
- model.should have(1).errors
21
- end
22
-
23
- it "generates an error message of type invalid" do
24
- model.valid?.should be(false)
25
- model.errors[:slug].should == [model.errors.generate_message(:slug, :invalid)]
26
- end
27
- end
28
- end
@@ -1,28 +0,0 @@
1
- require File.join(File.dirname(__FILE__), '..', 'spec_helper.rb')
2
-
3
- describe "Url Validation" do
4
- it "accepts valid urls" do
5
- model = Models::UrlValidatorModel.new
6
- model.url = 'http://www.verrot.fr'
7
- model.valid?.should be(true)
8
- model.should have(0).errors
9
- end
10
-
11
- describe "for invalid emails" do
12
- let(:model) do
13
- Models::UrlValidatorModel.new.tap do |m|
14
- m.url = 'http://^^^^.fr'
15
- end
16
- end
17
-
18
- it "rejects invalid emails" do
19
- model.valid?.should be(false)
20
- model.should have(1).errors
21
- end
22
-
23
- it "generates an error message of type invalid" do
24
- model.valid?.should be(false)
25
- model.errors[:url].should == [model.errors.generate_message(:url, :invalid)]
26
- end
27
- end
28
- end