validates_telephone 0.2.1 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.travis.yml +4 -0
- data/LICENSE +20 -0
- data/README.rdoc +28 -23
- data/lib/validates_telephone/formatter.rb +21 -0
- data/lib/validates_telephone/regex.rb +15 -0
- data/lib/validates_telephone/remarkable/require_a_valid_telephone_matcher.rb +43 -0
- data/lib/validates_telephone/shoulda-matchers/require_a_valid_telephone_matcher.rb +47 -0
- data/lib/validates_telephone/telephone_validator.rb +7 -0
- data/lib/validates_telephone/validator.rb +18 -0
- data/lib/validates_telephone/version.rb +1 -1
- data/lib/validates_telephone.rb +10 -10
- data/spec/fake_app/db/create_users.rb +4 -6
- data/spec/fake_app/user.rb +3 -5
- data/spec/spec_helper.rb +1 -2
- data/spec/validates_telephone/formatter_spec.rb +49 -0
- data/spec/validates_telephone/regex_spec.rb +51 -0
- data/spec/validates_telephone/remarkable/require_a_valid_telephone_matcher_spec.rb +40 -0
- data/spec/validates_telephone/shoulda-matchers/require_a_valid_telephone_matcher_spec.rb +40 -0
- data/spec/validates_telephone/validator_spec.rb +145 -0
- data/spec/validates_telephone_spec.rb +75 -14
- data/validates_telephone.gemspec +0 -2
- metadata +60 -29
- data/lib/validates_telephone/remarkable/validate_as_br_telephone_matcher.rb +0 -34
- data/lib/validates_telephone/remarkable/validate_as_usa_telephone_matcher.rb +0 -34
- data/lib/validates_telephone/shoulda-matchers/validate_as_br_telephone_matcher.rb +0 -45
- data/lib/validates_telephone/shoulda-matchers/validate_as_usa_telephone_matcher.rb +0 -45
- data/lib/validates_telephone/telephone.rb +0 -34
- data/spec/fake_app/admin.rb +0 -2
- data/spec/fake_app/db/create_admins.rb +0 -11
- data/spec/remarkable/validate_as_br_telephone_matcher_spec.rb +0 -26
- data/spec/remarkable/validate_as_usa_telephone_matcher_spec.rb +0 -26
- data/spec/shoulda-matchers/validate_as_br_telephone_matcher_spec.rb +0 -26
- data/spec/shoulda-matchers/validate_as_usa_telephone_matcher_spec.rb +0 -26
- data/spec/validates_telephone/telephone_spec.rb +0 -103
data/.travis.yml
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
(The MIT License)
|
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 NONINFRINGEMENT.
|
17
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
18
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
19
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
20
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
CHANGED
@@ -2,42 +2,47 @@
|
|
2
2
|
|
3
3
|
Rails gem to validate Telephone.
|
4
4
|
|
5
|
-
|
5
|
+
## Installation
|
6
6
|
|
7
|
-
|
7
|
+
Add this line to your application's Gemfile:
|
8
8
|
|
9
|
-
|
9
|
+
gem 'validates_telephone'
|
10
10
|
|
11
|
-
|
11
|
+
And then execute:
|
12
12
|
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install validates_telephone
|
18
|
+
|
19
|
+
== Basic Usage
|
20
|
+
|
21
|
+
It will accept a valid telephone from Brasil or United States by default:
|
22
|
+
|
23
|
+
```ruby
|
13
24
|
class User < ActiveRecord::Base
|
14
25
|
validates :telephone, :telephone => true
|
15
26
|
end
|
27
|
+
```
|
16
28
|
|
17
|
-
In case you want it to be a valid telephone for
|
29
|
+
In case you want it to be a valid telephone for just one place at a time, pass the locale:
|
18
30
|
|
31
|
+
```ruby
|
19
32
|
class User < ActiveRecord::Base
|
20
|
-
validates :
|
33
|
+
validates :br_telephone, :telephone => { :locale => :br }
|
34
|
+
validates :usa_telephone, :telephone => { :locale => :usa }
|
21
35
|
end
|
36
|
+
```
|
22
37
|
|
23
38
|
== Test
|
24
39
|
|
25
|
-
This gem has matchers for shoulda-matchers and remarkable.
|
26
|
-
|
27
|
-
If you want the matchers for Brazil and you are using shoulda-matchers, add this line to your spec_helper.rb :
|
28
|
-
require "validates_telephone/shoulda-matchers/validate_as_br_telephone_matcher"
|
29
|
-
If you want the matchers for Brazil and you are using remarkable, add this line to your spec_helper.rb :
|
30
|
-
require "validates_telephone/remarkable/validate_as_br_telephone_matcher"
|
31
|
-
|
32
|
-
If you want the matchers for United States and you are using shoulda-matchers, add this line to your spec_helper.rb :
|
33
|
-
require "validates_telephone/shoulda-matchers/validate_as_usa_telephone_matcher"
|
34
|
-
If you want the matchers for United States and you are using remarkable, add this line to your spec_helper.rb :
|
35
|
-
require "validates_telephone/remarkable/validate_as_usa_telephone_matcher"
|
36
|
-
|
37
|
-
=== How?
|
38
|
-
|
39
|
-
You should use validates_as_telephone(:attribute) just like any other shoulda or remarkable matcher.
|
40
|
+
This gem has builtin matchers for shoulda-matchers and remarkable.
|
40
41
|
|
41
|
-
==
|
42
|
+
== Contributing
|
42
43
|
|
43
|
-
|
44
|
+
1. Fork it
|
45
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
46
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
47
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
48
|
+
5. Create new Pull Request
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module ValidatesTelephone
|
2
|
+
module Formatter
|
3
|
+
def self.br(number)
|
4
|
+
return nil if number.nil?
|
5
|
+
number.gsub(/[\(\)-]/, "") =~ /^(\d{2})(\d{4})(\d{4})$/
|
6
|
+
return number if $1.nil?
|
7
|
+
"(#{$1})#{$2}-#{$3}"
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.usa(number)
|
11
|
+
return nil if number.nil?
|
12
|
+
number.gsub(/[\(\)-]/, "") =~ /^(\d{3})(\d{3})(\d{4})$/
|
13
|
+
return number if $1.nil?
|
14
|
+
"(#{$1})#{$2}-#{$3}"
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.any(number)
|
18
|
+
number
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module ValidatesTelephone
|
2
|
+
module Regex
|
3
|
+
def self.br(number)
|
4
|
+
number =~ /^(\(?\d{2}\)?)(\d{4})-?(\d{4})$/
|
5
|
+
end
|
6
|
+
|
7
|
+
def self.usa(number)
|
8
|
+
number =~ /^(\(?[^2-9]\d{2}\)?)(\d{3})-?(\d{4})$/
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.any(number)
|
12
|
+
number =~ /^(\(\d{2}\))(\d{4})-(\d{4})|(\([^2-9]\d{2}\))(\d{3})-(\d{4})$/
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'remarkable/active_model'
|
2
|
+
|
3
|
+
module Remarkable
|
4
|
+
module ActiveModel
|
5
|
+
module Matchers
|
6
|
+
class RequireAValidTelephoneMatcher < Remarkable::ActiveModel::Base
|
7
|
+
arguments :telephone
|
8
|
+
|
9
|
+
optional :locale
|
10
|
+
|
11
|
+
collection_assertions :telephone_valid?, :telephone_invalid?
|
12
|
+
|
13
|
+
protected
|
14
|
+
|
15
|
+
def telephone_valid?
|
16
|
+
return allows_value_of("(111)222-3333") if @options[:locale] == :usa
|
17
|
+
return allows_value_of("(11)2222-3333") if @options[:locale] == :br
|
18
|
+
allows_value_of("(111)222-3333") and allows_value_of("(11)2222-3333")
|
19
|
+
end
|
20
|
+
|
21
|
+
def telephone_invalid?
|
22
|
+
return (disallows_value_of("123456") and disallows_value_of("(11)2222-3333")) if @options[:locale] == :usa
|
23
|
+
return (disallows_value_of("123456") and disallows_value_of("(111)222-3333")) if @options[:locale] == :br
|
24
|
+
disallows_value_of("123456")
|
25
|
+
end
|
26
|
+
|
27
|
+
def allows_value_of(telephone)
|
28
|
+
@subject.telephone = telephone
|
29
|
+
@subject.valid?.errors[:telephone].should be_blank
|
30
|
+
end
|
31
|
+
|
32
|
+
def disallows_value_of(telephone)
|
33
|
+
@subject.telephone = telephone
|
34
|
+
@subject.valid?.errors[:telephone].should == ['is invalid']
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def require_a_valid_telephone(*args, &block)
|
39
|
+
RequireAValidTelephoneMatcher.new(*args, &block).spec(self)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require "shoulda-matchers"
|
2
|
+
|
3
|
+
module Shoulda
|
4
|
+
module Matchers
|
5
|
+
module ActiveModel
|
6
|
+
def require_a_valid_telephone(attr = :telephone)
|
7
|
+
RequireAValidTelephoneMatcher.new(attr)
|
8
|
+
end
|
9
|
+
|
10
|
+
class RequireAValidTelephoneMatcher< ValidationMatcher
|
11
|
+
def initialize(attribute)
|
12
|
+
@attribute = attribute
|
13
|
+
@options = {}
|
14
|
+
end
|
15
|
+
|
16
|
+
def from(locale)
|
17
|
+
@options[:locale] = locale
|
18
|
+
self
|
19
|
+
end
|
20
|
+
|
21
|
+
def description
|
22
|
+
"require #{@attribute} to be a valid telephone number"
|
23
|
+
end
|
24
|
+
|
25
|
+
def matches?(subject)
|
26
|
+
super(subject)
|
27
|
+
|
28
|
+
disallows_invalid_value and allows_valid_value
|
29
|
+
end
|
30
|
+
|
31
|
+
private
|
32
|
+
|
33
|
+
def disallows_invalid_value
|
34
|
+
return (disallows_value_of("123456") and disallows_value_of("(11)2222-3333")) if @options[:locale] == :usa
|
35
|
+
return (disallows_value_of("123456") and disallows_value_of("(111)222-3333")) if @options[:locale] == :br
|
36
|
+
disallows_value_of("123456")
|
37
|
+
end
|
38
|
+
|
39
|
+
def allows_valid_value
|
40
|
+
return allows_value_of("(111)222-3333") if @options[:locale] == :usa
|
41
|
+
return allows_value_of("(11)2222-3333") if @options[:locale] == :br
|
42
|
+
allows_value_of("(111)222-3333") and allows_value_of("(11)2222-3333")
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,7 @@
|
|
1
|
+
class TelephoneValidator < ActiveModel::EachValidator
|
2
|
+
def validate_each(record, attribute, value)
|
3
|
+
key = :"activerecord.errors.models.#{record.class.name.downcase}.attributes.#{attribute.to_s}.invalid"
|
4
|
+
locale = (options[:locale] or :any)
|
5
|
+
record.errors[attribute] << I18n.t(key, :default => :"activerecord.errors.messages.invalid") unless ValidatesTelephone::Validator.new(value, locale).valid?
|
6
|
+
end
|
7
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module ValidatesTelephone
|
2
|
+
class Validator
|
3
|
+
def initialize(number, locale = :any)
|
4
|
+
@number = number
|
5
|
+
@match = ValidatesTelephone::Regex.send(locale, @number)
|
6
|
+
@number = ValidatesTelephone::Formatter.send(locale, @number)
|
7
|
+
end
|
8
|
+
|
9
|
+
def valid?
|
10
|
+
return true if @number.nil?
|
11
|
+
@match
|
12
|
+
end
|
13
|
+
|
14
|
+
def number
|
15
|
+
@number
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
data/lib/validates_telephone.rb
CHANGED
@@ -1,12 +1,12 @@
|
|
1
|
-
|
2
|
-
require "validates_telephone/
|
1
|
+
#Validator
|
2
|
+
require "validates_telephone/telephone_validator"
|
3
3
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
4
|
+
#Rspec Matchers
|
5
|
+
require "validates_telephone/shoulda-matchers/require_a_valid_telephone_matcher" if defined?(::Shoulda)
|
6
|
+
require "validates_telephone/remarkable/require_a_valid_telephone_matcher" if defined?(::Remarkable)
|
7
|
+
|
8
|
+
module ValidatesTelephone
|
9
|
+
autoload :Validator, "validates_telephone/validator"
|
10
|
+
autoload :Regex, "validates_telephone/regex"
|
11
|
+
autoload :Formatter, "validates_telephone/formatter"
|
12
12
|
end
|
@@ -1,15 +1,13 @@
|
|
1
1
|
class CreateUsers < ActiveRecord::Migration
|
2
2
|
def self.up
|
3
|
-
create_table :
|
4
|
-
u.string :telephone
|
5
|
-
end
|
6
|
-
create_table :en_users do |u|
|
3
|
+
create_table :users do |u|
|
7
4
|
u.string :telephone
|
5
|
+
u.string :br_telephone
|
6
|
+
u.string :usa_telephone
|
8
7
|
end
|
9
8
|
end
|
10
9
|
|
11
10
|
def self.down
|
12
|
-
drop_table :
|
13
|
-
drop_table :en_users
|
11
|
+
drop_table :users
|
14
12
|
end
|
15
13
|
end
|
data/spec/fake_app/user.rb
CHANGED
@@ -1,7 +1,5 @@
|
|
1
|
-
class
|
1
|
+
class User < ActiveRecord::Base
|
2
2
|
validates :telephone, :telephone => true
|
3
|
-
|
4
|
-
|
5
|
-
class EnUser < ActiveRecord::Base
|
6
|
-
validates :telephone, :telephone => { :locale => :usa }
|
3
|
+
validates :br_telephone, :telephone => { :locale => :br }
|
4
|
+
validates :usa_telephone, :telephone => { :locale => :usa }
|
7
5
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe ValidatesTelephone::Formatter do
|
4
|
+
context "#br" do
|
5
|
+
context "with a nil value" do
|
6
|
+
it "should return nil" do
|
7
|
+
ValidatesTelephone::Formatter.br(nil).should be_nil
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
context "with a valid number" do
|
12
|
+
it "should return it formatted" do
|
13
|
+
ValidatesTelephone::Formatter.br("1111111111").should == "(11)1111-1111"
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
context "with an invalid number" do
|
18
|
+
it "should return as it was" do
|
19
|
+
ValidatesTelephone::Formatter.br("11111111").should == "11111111"
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
context "#usa" do
|
25
|
+
context "with a nil value" do
|
26
|
+
it "should return nil" do
|
27
|
+
ValidatesTelephone::Formatter.usa(nil).should be_nil
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
context "with a valid number" do
|
32
|
+
it "should return it formatted" do
|
33
|
+
ValidatesTelephone::Formatter.usa("1111111111").should == "(111)111-1111"
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
context "with an invalid number" do
|
38
|
+
it "should return as it was" do
|
39
|
+
ValidatesTelephone::Formatter.usa("11111111").should == "11111111"
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
context "#any" do
|
45
|
+
it "should always return the number as it was" do
|
46
|
+
ValidatesTelephone::Formatter.any("11111").should == "11111"
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe ValidatesTelephone::Regex do
|
4
|
+
context "#br" do
|
5
|
+
context "with a valid number" do
|
6
|
+
it "should return true" do
|
7
|
+
ValidatesTelephone::Regex.br("(11)1111-1111").should be_true
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
context "with an invalid number" do
|
12
|
+
it "should return false" do
|
13
|
+
ValidatesTelephone::Regex.br("11111111").should be_false
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
context "#usa" do
|
19
|
+
context "with a valid number" do
|
20
|
+
it "should return true" do
|
21
|
+
ValidatesTelephone::Regex.usa("(111)111-1111").should == be_true
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
context "with an invalid number" do
|
26
|
+
it "should return false" do
|
27
|
+
ValidatesTelephone::Regex.usa("11111111").should be_false
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
context "#any" do
|
33
|
+
context "with a valid number from United States" do
|
34
|
+
it "should return true" do
|
35
|
+
ValidatesTelephone::Regex.any("(111)111-1111").should == be_true
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
context "with a valid number from Brazil" do
|
40
|
+
it "should return true" do
|
41
|
+
ValidatesTelephone::Regex.any("(11)1111-1111").should == be_true
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
context "with an invalid number" do
|
46
|
+
it "should return nil" do
|
47
|
+
ValidatesTelephone::Regex.any("11111111").should be_false
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'remarkable/active_model'
|
3
|
+
|
4
|
+
describe Remarkable::ActiveModel::Matchers::RequireAValidTelephoneMatcher do
|
5
|
+
before :each do
|
6
|
+
@user = User.new
|
7
|
+
end
|
8
|
+
|
9
|
+
it "should accept on telephone" do
|
10
|
+
@user.should require_a_valid_telephone(:telephone)
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should accept without a specified attribute" do
|
14
|
+
@user.should require_a_valid_telephone
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should reject on br_telephone" do
|
18
|
+
@user.should_not require_a_valid_telephone(:br_telephone)
|
19
|
+
end
|
20
|
+
|
21
|
+
context "with :br as locale" do
|
22
|
+
it "should accept on br_telephone" do
|
23
|
+
@user.should require_a_valid_telephone(:br_telephone).from(:br)
|
24
|
+
end
|
25
|
+
|
26
|
+
it "should reject on usa_telephone" do
|
27
|
+
@user.should_not require_a_valid_telephone(:usa_telephone).from(:br)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
context "with :usa as locale" do
|
32
|
+
it "should accept on usa_telephone" do
|
33
|
+
@user.should require_a_valid_telephone(:usa_telephone).from(:usa)
|
34
|
+
end
|
35
|
+
|
36
|
+
it "should reject on br_telephone" do
|
37
|
+
@user.should_not require_a_valid_telephone(:br_telephone).from(:usa)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'shoulda-matchers'
|
3
|
+
|
4
|
+
describe Shoulda::Matchers::ActiveModel::RequireAValidTelephoneMatcher do
|
5
|
+
before :each do
|
6
|
+
@user = User.new
|
7
|
+
end
|
8
|
+
|
9
|
+
it "should accept on telephone" do
|
10
|
+
@user.should require_a_valid_telephone(:telephone)
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should accept without a specified attribute" do
|
14
|
+
@user.should require_a_valid_telephone
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should reject on br_telephone" do
|
18
|
+
@user.should_not require_a_valid_telephone(:br_telephone)
|
19
|
+
end
|
20
|
+
|
21
|
+
context "with :br as locale" do
|
22
|
+
it "should accept on br_telephone" do
|
23
|
+
@user.should require_a_valid_telephone(:br_telephone).from(:br)
|
24
|
+
end
|
25
|
+
|
26
|
+
it "should reject on usa_telephone" do
|
27
|
+
@user.should_not require_a_valid_telephone(:usa_telephone).from(:br)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
context "with :usa as locale" do
|
32
|
+
it "should accept on usa_telephone" do
|
33
|
+
@user.should require_a_valid_telephone(:usa_telephone).from(:usa)
|
34
|
+
end
|
35
|
+
|
36
|
+
it "should reject on br_telephone" do
|
37
|
+
@user.should_not require_a_valid_telephone(:br_telephone).from(:usa)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,145 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe ValidatesTelephone::Validator do
|
4
|
+
context "with br as locale" do
|
5
|
+
context "should be invalid with" do
|
6
|
+
it "blank number" do
|
7
|
+
ValidatesTelephone::Validator.new('', :br).should_not be_valid
|
8
|
+
end
|
9
|
+
|
10
|
+
it "123456 as number" do
|
11
|
+
ValidatesTelephone::Validator.new('123456', :br).should_not be_valid
|
12
|
+
end
|
13
|
+
|
14
|
+
it "12345678910 as number" do
|
15
|
+
ValidatesTelephone::Validator.new('12345678910', :br).should_not be_valid
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
context "should be valid with" do
|
20
|
+
it "nil as number" do
|
21
|
+
ValidatesTelephone::Validator.new(nil, :br).should be_valid
|
22
|
+
end
|
23
|
+
|
24
|
+
it "1111111111 as number" do
|
25
|
+
ValidatesTelephone::Validator.new('1111111111', :br).should be_valid
|
26
|
+
end
|
27
|
+
|
28
|
+
it "(11)11111111 as number" do
|
29
|
+
ValidatesTelephone::Validator.new('(11)11111111', :br).should be_valid
|
30
|
+
end
|
31
|
+
|
32
|
+
it "111111-1111 as number" do
|
33
|
+
ValidatesTelephone::Validator.new('111111-1111', :br).should be_valid
|
34
|
+
end
|
35
|
+
|
36
|
+
it "(11)1111-1111 as number" do
|
37
|
+
ValidatesTelephone::Validator.new('(11)1111-1111', :br).should be_valid
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
context "with a valid value" do
|
42
|
+
it "should return it formatted" do
|
43
|
+
ValidatesTelephone::Validator.new('1111111111', :br).number.should == '(11)1111-1111'
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
context "with an invalid value" do
|
48
|
+
it "should return as it was" do
|
49
|
+
ValidatesTelephone::Validator.new('123456', :br).number.should == '123456'
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
context "with usa as locale" do
|
55
|
+
context "should be invalid with" do
|
56
|
+
it "blank number" do
|
57
|
+
ValidatesTelephone::Validator.new('', :usa).should_not be_valid
|
58
|
+
end
|
59
|
+
|
60
|
+
it "123456 as number" do
|
61
|
+
ValidatesTelephone::Validator.new('123456', :usa).should_not be_valid
|
62
|
+
end
|
63
|
+
|
64
|
+
it "12345678910 as number" do
|
65
|
+
ValidatesTelephone::Validator.new('12345678910', :usa).should_not be_valid
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
context "should be valid with" do
|
70
|
+
it "nil as number" do
|
71
|
+
ValidatesTelephone::Validator.new(nil, :usa).should be_valid
|
72
|
+
end
|
73
|
+
|
74
|
+
it "1111111111 as number" do
|
75
|
+
ValidatesTelephone::Validator.new('1111111111', :usa).should be_valid
|
76
|
+
end
|
77
|
+
|
78
|
+
it "(11)11111111 as number" do
|
79
|
+
ValidatesTelephone::Validator.new('(111)1111111', :usa).should be_valid
|
80
|
+
end
|
81
|
+
|
82
|
+
it "111111-1111 as number" do
|
83
|
+
ValidatesTelephone::Validator.new('111111-1111', :usa).should be_valid
|
84
|
+
end
|
85
|
+
|
86
|
+
it "(111)111-1111 as number" do
|
87
|
+
ValidatesTelephone::Validator.new('(111)111-1111', :usa).should be_valid
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
context "with a valid value" do
|
92
|
+
it "should return it formatted" do
|
93
|
+
ValidatesTelephone::Validator.new('1111111111', :usa).number.should == '(111)111-1111'
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
context "with an invalid value" do
|
98
|
+
it "should return as it was" do
|
99
|
+
ValidatesTelephone::Validator.new('123456', :usa).number.should == '123456'
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
context "without locale" do
|
105
|
+
context "should be invalid with" do
|
106
|
+
it "blank number" do
|
107
|
+
ValidatesTelephone::Validator.new('').should_not be_valid
|
108
|
+
end
|
109
|
+
|
110
|
+
it "123456 as number" do
|
111
|
+
ValidatesTelephone::Validator.new('123456').should_not be_valid
|
112
|
+
end
|
113
|
+
|
114
|
+
it "12345678910 as number" do
|
115
|
+
ValidatesTelephone::Validator.new('12345678910').should_not be_valid
|
116
|
+
end
|
117
|
+
|
118
|
+
it "1111111111 as number" do
|
119
|
+
ValidatesTelephone::Validator.new('1111111111').should_not be_valid
|
120
|
+
end
|
121
|
+
|
122
|
+
it "(11)11111111 as number" do
|
123
|
+
ValidatesTelephone::Validator.new('(111)1111111').should_not be_valid
|
124
|
+
end
|
125
|
+
|
126
|
+
it "111111-1111 as number" do
|
127
|
+
ValidatesTelephone::Validator.new('111111-1111').should_not be_valid
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
context "should be valid with" do
|
132
|
+
it "nil as number" do
|
133
|
+
ValidatesTelephone::Validator.new(nil).should be_valid
|
134
|
+
end
|
135
|
+
|
136
|
+
it "(11)1111-1111 as number" do
|
137
|
+
ValidatesTelephone::Validator.new('(11)1111-1111').should be_valid
|
138
|
+
end
|
139
|
+
|
140
|
+
it "(111)111-1111 as number" do
|
141
|
+
ValidatesTelephone::Validator.new('(111)111-1111').should be_valid
|
142
|
+
end
|
143
|
+
end
|
144
|
+
end
|
145
|
+
end
|