active_validation 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (93) hide show
  1. checksums.yaml +7 -0
  2. data/.coveralls.yml +1 -0
  3. data/.gitignore +14 -0
  4. data/.rspec +4 -0
  5. data/.travis.yml +13 -0
  6. data/Gemfile +4 -0
  7. data/LICENSE.txt +22 -0
  8. data/README.md +1099 -0
  9. data/Rakefile +1 -0
  10. data/active_validation.gemspec +29 -0
  11. data/config/locales/en.yml +109 -0
  12. data/lib/active_validation/matchers/ensure_valid_alpha_format_of.rb +26 -0
  13. data/lib/active_validation/matchers/ensure_valid_alpha_numeric_format_of.rb +26 -0
  14. data/lib/active_validation/matchers/ensure_valid_base64_format_of.rb +26 -0
  15. data/lib/active_validation/matchers/ensure_valid_boolean_format_of.rb +26 -0
  16. data/lib/active_validation/matchers/ensure_valid_credit_card_format_of.rb +26 -0
  17. data/lib/active_validation/matchers/ensure_valid_currency_format_of.rb +26 -0
  18. data/lib/active_validation/matchers/ensure_valid_cusip_format_of.rb +26 -0
  19. data/lib/active_validation/matchers/ensure_valid_email_format_of.rb +26 -0
  20. data/lib/active_validation/matchers/ensure_valid_equality_matcher_of.rb +40 -0
  21. data/lib/active_validation/matchers/ensure_valid_hex_format_of.rb +26 -0
  22. data/lib/active_validation/matchers/ensure_valid_imei_format_of.rb +26 -0
  23. data/lib/active_validation/matchers/ensure_valid_ip_format_of.rb +26 -0
  24. data/lib/active_validation/matchers/ensure_valid_isbn_format_of.rb +26 -0
  25. data/lib/active_validation/matchers/ensure_valid_isin_format_of.rb +26 -0
  26. data/lib/active_validation/matchers/ensure_valid_latitude_format_of.rb +26 -0
  27. data/lib/active_validation/matchers/ensure_valid_longitude_format_of.rb +26 -0
  28. data/lib/active_validation/matchers/ensure_valid_mac_address_format_of.rb +26 -0
  29. data/lib/active_validation/matchers/ensure_valid_name_format_of.rb +26 -0
  30. data/lib/active_validation/matchers/ensure_valid_password_format_of.rb +26 -0
  31. data/lib/active_validation/matchers/ensure_valid_phone_format_of.rb +26 -0
  32. data/lib/active_validation/matchers/ensure_valid_sedol_format_of.rb +26 -0
  33. data/lib/active_validation/matchers/ensure_valid_slug_format_of.rb +26 -0
  34. data/lib/active_validation/matchers/ensure_valid_ssn_format_of.rb +26 -0
  35. data/lib/active_validation/matchers/ensure_valid_url_format_of.rb +26 -0
  36. data/lib/active_validation/matchers/ensure_valid_username_format_of.rb +26 -0
  37. data/lib/active_validation/matchers/ensure_valid_uuid_format_of.rb +26 -0
  38. data/lib/active_validation/validators/alpha_numeric_validator.rb +30 -0
  39. data/lib/active_validation/validators/alpha_validator.rb +31 -0
  40. data/lib/active_validation/validators/base64_validator.rb +9 -0
  41. data/lib/active_validation/validators/boolean_validator.rb +9 -0
  42. data/lib/active_validation/validators/credit_card_validator.rb +133 -0
  43. data/lib/active_validation/validators/currency_validator.rb +23 -0
  44. data/lib/active_validation/validators/cusip_validator.rb +33 -0
  45. data/lib/active_validation/validators/email_validator.rb +25 -0
  46. data/lib/active_validation/validators/equality_validator.rb +27 -0
  47. data/lib/active_validation/validators/hex_validator.rb +9 -0
  48. data/lib/active_validation/validators/imei_validator.rb +37 -0
  49. data/lib/active_validation/validators/ip_validator.rb +9 -0
  50. data/lib/active_validation/validators/isbn_validator.rb +24 -0
  51. data/lib/active_validation/validators/isin_validator.rb +38 -0
  52. data/lib/active_validation/validators/latitude_validator.rb +9 -0
  53. data/lib/active_validation/validators/longitude_validator.rb +9 -0
  54. data/lib/active_validation/validators/mac_address_validator.rb +24 -0
  55. data/lib/active_validation/validators/name_validator.rb +9 -0
  56. data/lib/active_validation/validators/password_validator.rb +23 -0
  57. data/lib/active_validation/validators/phone_validator.rb +9 -0
  58. data/lib/active_validation/validators/sedol_validator.rb +32 -0
  59. data/lib/active_validation/validators/slug_validator.rb +9 -0
  60. data/lib/active_validation/validators/ssn_validator.rb +9 -0
  61. data/lib/active_validation/validators/url_validator.rb +36 -0
  62. data/lib/active_validation/validators/username_validator.rb +9 -0
  63. data/lib/active_validation/validators/uuid_validator.rb +28 -0
  64. data/lib/active_validation/version.rb +3 -0
  65. data/lib/active_validation.rb +91 -0
  66. data/spec/lib/alpha_numeric_validator_spec.rb +91 -0
  67. data/spec/lib/alpha_validator_spec.rb +182 -0
  68. data/spec/lib/base64_validator_spec.rb +33 -0
  69. data/spec/lib/boolean_validator_spec.rb +35 -0
  70. data/spec/lib/credit_card_validator_spec.rb +686 -0
  71. data/spec/lib/currency_validator_spec.rb +63 -0
  72. data/spec/lib/cusip_validator_spec.rb +27 -0
  73. data/spec/lib/email_validator_spec.rb +109 -0
  74. data/spec/lib/equality_validator_spec.rb +334 -0
  75. data/spec/lib/hex_validator_spec.rb +73 -0
  76. data/spec/lib/imei_validator_spec.rb +41 -0
  77. data/spec/lib/ip_validator_spec.rb +33 -0
  78. data/spec/lib/isbn_validator_spec.rb +41 -0
  79. data/spec/lib/isin_validator_spec.rb +35 -0
  80. data/spec/lib/latitude_validator_spec.rb +31 -0
  81. data/spec/lib/longitude_validator_spec.rb +31 -0
  82. data/spec/lib/mac_address_validator_spec.rb +54 -0
  83. data/spec/lib/name_validator_spec.rb +39 -0
  84. data/spec/lib/password_validator_spec.rb +85 -0
  85. data/spec/lib/phone_validator_spec.rb +42 -0
  86. data/spec/lib/sedol_validator_spec.rb +31 -0
  87. data/spec/lib/slug_validator_spec.rb +41 -0
  88. data/spec/lib/ssn_validator_spec.rb +36 -0
  89. data/spec/lib/url_validator_spec.rb +106 -0
  90. data/spec/lib/username_validator_spec.rb +37 -0
  91. data/spec/lib/uuid_validator_spec.rb +157 -0
  92. data/spec/spec_helper.rb +12 -0
  93. metadata +260 -0
@@ -0,0 +1,41 @@
1
+ require 'spec_helper'
2
+
3
+ describe IsbnValidator do
4
+
5
+ context "has a valid value" do
6
+ let(:klass) do
7
+ Class.new do
8
+ include ActiveModel::Validations
9
+ attr_accessor :isbn, :name
10
+ validates :isbn, isbn: true
11
+ end
12
+ end
13
+
14
+ subject { klass.new }
15
+
16
+ it { should allow_value("9519854894").for(:isbn) }
17
+ it { should allow_value("951 98548 9 4").for(:isbn) }
18
+ it { should allow_value("951-98548-9-4").for(:isbn) }
19
+ it { should allow_value("951-98548 9 4").for(:isbn) }
20
+ it { should allow_value("0-9722051-1-X").for(:isbn) }
21
+ it { should allow_value("0-9722051-1-x").for(:isbn) }
22
+ it { should allow_value("9781590599938").for(:isbn) }
23
+ it { should allow_value("978 159059 9938").for(:isbn) }
24
+ it { should allow_value("978-159059-9938").for(:isbn) }
25
+ it { should allow_value("978-159059 9938").for(:isbn) }
26
+
27
+ it { should_not allow_value('').for(:isbn) }
28
+ it { should_not allow_value(' ').for(:isbn) }
29
+ it { should_not allow_value(nil).for(:isbn) }
30
+ it { should_not allow_value("951-98548-9-p").for(:isbn) }
31
+ it { should_not allow_value("abc123ab3344").for(:isbn) }
32
+ it { should_not allow_value("12345678901234").for(:isbn) }
33
+ it { should_not allow_value("9991a9010599938").for(:isbn) }
34
+ it { should_not allow_value("! \#$%\`|").for(:isbn) }
35
+ it { should_not allow_value("<>@[]\`|").for(:isbn) }
36
+
37
+ it { should ensure_valid_isbn_format_of(:isbn) }
38
+ it { should_not ensure_valid_isbn_format_of(:name) }
39
+ end
40
+
41
+ end
@@ -0,0 +1,35 @@
1
+ require 'spec_helper'
2
+
3
+ describe IsinValidator do
4
+
5
+ context "has a valid value" do
6
+ let(:klass) do
7
+ Class.new do
8
+ include ActiveModel::Validations
9
+ attr_accessor :isin, :name
10
+ validates :isin, isin: true
11
+ end
12
+ end
13
+
14
+ subject { klass.new }
15
+
16
+ it { should allow_value("US0378331005").for(:isin) }
17
+ it { should allow_value("AU0000XVGZA3").for(:isin) }
18
+
19
+ it { should_not allow_value('').for(:isin) }
20
+ it { should_not allow_value(' ').for(:isin) }
21
+ it { should_not allow_value(nil).for(:isin) }
22
+ it { should_not allow_value("US03783310055").for(:isin) }
23
+ #it { should_not allow_value("US0378331004").for(:isin) }
24
+ it { should_not allow_value("US037833100").for(:isin) }
25
+ it { should_not allow_value("US03783315").for(:isin) }
26
+ it { should_not allow_value("AA0000XVGZA3").for(:isin) }
27
+ it { should_not allow_value("120378331004").for(:isin) }
28
+ it { should_not allow_value("! \#$%\`|").for(:isin) }
29
+ it { should_not allow_value("<>@[]\`|").for(:isin) }
30
+
31
+ it { should ensure_valid_isin_format_of(:isin) }
32
+ it { should_not ensure_valid_isin_format_of(:name) }
33
+ end
34
+
35
+ end
@@ -0,0 +1,31 @@
1
+ require 'spec_helper'
2
+
3
+ describe LatitudeValidator do
4
+
5
+ context "has a valid value" do
6
+ let(:klass) do
7
+ Class.new do
8
+ include ActiveModel::Validations
9
+ attr_accessor :lat, :name
10
+ validates :lat, latitude: true
11
+ end
12
+ end
13
+
14
+ subject { klass.new }
15
+
16
+ it { should allow_value(-90).for(:lat) }
17
+ it { should allow_value(90).for(:lat) }
18
+ it { should allow_value(0).for(:lat) }
19
+ it { should allow_value(9.33).for(:lat) }
20
+
21
+ it { should_not allow_value('').for(:lat) }
22
+ it { should_not allow_value(' ').for(:lat) }
23
+ it { should_not allow_value(nil).for(:lat) }
24
+ it { should_not allow_value(-90.1).for(:lat) }
25
+ it { should_not allow_value(90.1).for(:lat) }
26
+
27
+ it { should ensure_valid_latitude_format_of(:lat) }
28
+ it { should_not ensure_valid_latitude_format_of(:name) }
29
+ end
30
+
31
+ end
@@ -0,0 +1,31 @@
1
+ require 'spec_helper'
2
+
3
+ describe LongitudeValidator do
4
+
5
+ context "has a valid value" do
6
+ let(:klass) do
7
+ Class.new do
8
+ include ActiveModel::Validations
9
+ attr_accessor :lon, :name
10
+ validates :lon, longitude: true
11
+ end
12
+ end
13
+
14
+ subject { klass.new }
15
+
16
+ it { should allow_value(-180).for(:lon) }
17
+ it { should allow_value(180).for(:lon) }
18
+ it { should allow_value(0).for(:lon) }
19
+ it { should allow_value(9.33).for(:lon) }
20
+
21
+ it { should_not allow_value('').for(:lon) }
22
+ it { should_not allow_value(' ').for(:lon) }
23
+ it { should_not allow_value(nil).for(:lon) }
24
+ it { should_not allow_value(-181.1).for(:lon) }
25
+ it { should_not allow_value(181.1).for(:lon) }
26
+
27
+ it { should ensure_valid_longitude_format_of(:lon) }
28
+ it { should_not ensure_valid_longitude_format_of(:name) }
29
+ end
30
+
31
+ end
@@ -0,0 +1,54 @@
1
+ require 'spec_helper'
2
+
3
+ describe MacAddressValidator do
4
+
5
+ context "has valid format" do
6
+ let(:klass) do
7
+ Class.new do
8
+ include ActiveModel::Validations
9
+ attr_accessor :mac, :name
10
+ validates :mac, mac_address: true
11
+ end
12
+ end
13
+
14
+ subject { klass.new }
15
+
16
+ # Valid formats
17
+ it { should allow_value("08:00:2b:01:02:03").for(:mac) }
18
+ it { should allow_value("08-00-2b-01-02-03").for(:mac) }
19
+ it { should allow_value("08.00.2b.01.02.03").for(:mac) }
20
+ it { should allow_value("08 00 2b 01 02 03").for(:mac) }
21
+ it { should allow_value("08002b:010203").for(:mac) }
22
+ it { should allow_value("08002b.010203").for(:mac) }
23
+ it { should allow_value("08002b-010203").for(:mac) }
24
+ it { should allow_value("0800.2b01.0203").for(:mac) }
25
+ it { should allow_value("0800-2b01-0203").for(:mac) }
26
+ it { should allow_value("0800 2b01 0203").for(:mac) }
27
+ it { should allow_value("08002b010203").for(:mac) }
28
+
29
+ # Mixed Separators
30
+ it { should_not allow_value("08-00:2b:01:02:03").for(:mac) }
31
+ it { should_not allow_value("08.00:2b:01:02:03").for(:mac) }
32
+ it { should_not allow_value("08 00:2b:01:02:03").for(:mac) }
33
+ it { should_not allow_value("0800-2b01:0203").for(:mac) }
34
+ it { should_not allow_value("0800 2b01:0203").for(:mac) }
35
+
36
+ # Too Short
37
+ it { should_not allow_value("08:00:2b:01:02").for(:mac) }
38
+ it { should_not allow_value("08-00-2b-01-02").for(:mac) }
39
+
40
+ # Too Long
41
+ it { should_not allow_value("08:00:2b:01:02:03:04").for(:mac) }
42
+
43
+ # Non-Hex Characters
44
+ it { should_not allow_value("qq:00:00:00:00:00").for(:mac) }
45
+
46
+ it { should_not allow_value(' ').for(:mac) }
47
+ it { should_not allow_value(nil).for(:mac) }
48
+ it { should_not allow_value("invalid").for(:mac) }
49
+
50
+ it { should ensure_valid_mac_address_format_of(:mac) }
51
+ it { should_not ensure_valid_mac_address_format_of(:name) }
52
+ end
53
+
54
+ end
@@ -0,0 +1,39 @@
1
+ require 'spec_helper'
2
+
3
+ describe NameValidator do
4
+
5
+ context "has a valid value" do
6
+ let(:klass) do
7
+ Class.new do
8
+ include ActiveModel::Validations
9
+ attr_accessor :name, :email
10
+ validates :name, name: true
11
+ end
12
+ end
13
+
14
+ subject { klass.new }
15
+
16
+ it { should allow_value("First Last").for(:name) }
17
+ it { should allow_value("First Last-Name").for(:name) }
18
+ it { should allow_value("First Middle Last").for(:name) }
19
+ it { should allow_value("Sur First Middle Last").for(:name) }
20
+ it { should allow_value("Sur First Middle Last Family").for(:name) }
21
+ it { should allow_value("Sur First Middle Last-Family").for(:name) }
22
+
23
+ it { should_not allow_value('').for(:name) }
24
+ it { should_not allow_value(' ').for(:name) }
25
+ it { should_not allow_value(nil).for(:name) }
26
+ it { should_not allow_value("First").for(:name) }
27
+ it { should_not allow_value("First Last_Name").for(:name) }
28
+ it { should_not allow_value("First1 Last").for(:name) }
29
+ it { should_not allow_value("First 1 Last").for(:name) }
30
+ it { should_not allow_value("Sur. First Middle Last Jr.").for(:name) }
31
+ it { should_not allow_value("Sur First Middle Last Family III").for(:name) }
32
+ it { should_not allow_value("! \#$%\`|").for(:name) }
33
+ it { should_not allow_value("<>@[]\`|").for(:name) }
34
+
35
+ it { should ensure_valid_name_format_of(:name) }
36
+ it { should_not ensure_valid_name_format_of(:email) }
37
+ end
38
+
39
+ end
@@ -0,0 +1,85 @@
1
+ require 'spec_helper'
2
+
3
+ describe PasswordValidator do
4
+
5
+ context "has a valid value" do
6
+ let(:klass) do
7
+ Class.new do
8
+ include ActiveModel::Validations
9
+ attr_accessor :password, :name
10
+ validates :password, password: true
11
+ end
12
+ end
13
+
14
+ subject { klass.new }
15
+
16
+ it { should allow_value("password").for(:password) }
17
+ it { should allow_value("password1234").for(:password) }
18
+ it { should allow_value("pa$$word").for(:password) }
19
+ it { should allow_value("pass-word").for(:password) }
20
+ it { should allow_value("pass_word").for(:password) }
21
+ it { should allow_value("password!").for(:password) }
22
+ it { should allow_value("password@").for(:password) }
23
+ it { should allow_value("password#").for(:password) }
24
+ it { should allow_value("password%").for(:password) }
25
+ it { should allow_value("password^").for(:password) }
26
+ it { should allow_value("password&").for(:password) }
27
+ it { should allow_value("password*").for(:password) }
28
+
29
+ it { should_not allow_value('').for(:password) }
30
+ it { should_not allow_value(' ').for(:password) }
31
+ it { should_not allow_value(nil).for(:password) }
32
+ it { should_not allow_value(" password").for(:password) }
33
+ it { should_not allow_value(" password ").for(:password) }
34
+ it { should_not allow_value("password ").for(:password) }
35
+ it { should_not allow_value("pass word").for(:password) }
36
+ it { should_not allow_value("! \#$%\`|").for(:password) }
37
+ it { should_not allow_value("<>@[]\`|").for(:password) }
38
+
39
+ it { should ensure_valid_password_format_of(:password) }
40
+ it { should_not ensure_valid_password_format_of(:name) }
41
+ end
42
+
43
+ context "Password with :strict option has a valid value" do
44
+ let(:klass) do
45
+ Class.new do
46
+ include ActiveModel::Validations
47
+ attr_accessor :password, :name
48
+ validates :password, password: { strict: true }
49
+ end
50
+ end
51
+
52
+ subject { klass.new }
53
+
54
+ it { should allow_value("Password123").for(:password) }
55
+ it { should allow_value("Password-123").for(:password) }
56
+
57
+ it { should_not allow_value('').for(:password) }
58
+ it { should_not allow_value(' ').for(:password) }
59
+ it { should_not allow_value(nil).for(:password) }
60
+ it { should_not allow_value("pass").for(:password) }
61
+ it { should_not allow_value(" password").for(:password) }
62
+ it { should_not allow_value(" password ").for(:password) }
63
+ it { should_not allow_value("password ").for(:password) }
64
+ it { should_not allow_value("pass word").for(:password) }
65
+ it { should_not allow_value("password-12345678910").for(:password) }
66
+ it { should_not allow_value("password").for(:password) }
67
+ it { should_not allow_value("password1234").for(:password) }
68
+ it { should_not allow_value("pa$$word").for(:password) }
69
+ it { should_not allow_value("pass-word").for(:password) }
70
+ it { should_not allow_value("pass_word").for(:password) }
71
+ it { should_not allow_value("password!").for(:password) }
72
+ it { should_not allow_value("password@").for(:password) }
73
+ it { should_not allow_value("password#").for(:password) }
74
+ it { should_not allow_value("password%").for(:password) }
75
+ it { should_not allow_value("password^").for(:password) }
76
+ it { should_not allow_value("password&").for(:password) }
77
+ it { should_not allow_value("password*").for(:password) }
78
+ it { should_not allow_value("! \#$%\`|").for(:password) }
79
+ it { should_not allow_value("<>@[]\`|").for(:password) }
80
+
81
+ it { should ensure_valid_password_format_of(:password) }
82
+ it { should_not ensure_valid_password_format_of(:name) }
83
+ end
84
+
85
+ end
@@ -0,0 +1,42 @@
1
+ require 'spec_helper'
2
+
3
+ describe PhoneValidator do
4
+
5
+ context "has a valid value" do
6
+ let(:klass) do
7
+ Class.new do
8
+ include ActiveModel::Validations
9
+ attr_accessor :phone, :name
10
+ validates :phone, phone: true
11
+ end
12
+ end
13
+
14
+ subject { klass.new }
15
+
16
+ it { should allow_value("1234567").for(:phone) }
17
+ it { should allow_value("123-4567").for(:phone) }
18
+ it { should allow_value("123 4567").for(:phone) }
19
+ it { should allow_value("5551234567").for(:phone) }
20
+ it { should allow_value("555-123-4567").for(:phone) }
21
+ it { should allow_value("555 123 4567").for(:phone) }
22
+ it { should allow_value("(555) 123-4567").for(:phone) }
23
+ it { should allow_value("(555) 123-4567 ext 1234").for(:phone) }
24
+ it { should allow_value("(555) 123-4567 ext1234").for(:phone) }
25
+ it { should allow_value("(555) 123-4567 ext-1234").for(:phone) }
26
+ it { should allow_value("1-555-123-4567").for(:phone) }
27
+ it { should allow_value("+1-555-123-4567").for(:phone) }
28
+ it { should allow_value("+1 (555) 123-4567 ext-1234").for(:phone) }
29
+
30
+ it { should_not allow_value('').for(:phone) }
31
+ it { should_not allow_value(nil).for(:phone) }
32
+ it { should_not allow_value("123_4567").for(:phone) }
33
+ it { should_not allow_value("(555) 123-4567 ext:1234").for(:phone) }
34
+ it { should_not allow_value("(555) 123-4567 ext_1234").for(:phone) }
35
+ it { should_not allow_value("! \#$%\`|").for(:phone) }
36
+ it { should_not allow_value("<>@[]\`|").for(:phone) }
37
+
38
+ it { should ensure_valid_phone_format_of(:phone) }
39
+ it { should_not ensure_valid_phone_format_of(:name) }
40
+ end
41
+
42
+ end
@@ -0,0 +1,31 @@
1
+ require 'spec_helper'
2
+
3
+ describe SedolValidator do
4
+
5
+ context "has a valid value" do
6
+ let(:klass) do
7
+ Class.new do
8
+ include ActiveModel::Validations
9
+ attr_accessor :sedol, :name
10
+ validates :sedol, sedol: true
11
+ end
12
+ end
13
+
14
+ subject { klass.new }
15
+
16
+ it { should allow_value("B0WNLY7").for(:sedol) }
17
+
18
+ it { should_not allow_value('').for(:sedol) }
19
+ it { should_not allow_value(' ').for(:sedol) }
20
+ it { should_not allow_value(nil).for(:sedol) }
21
+ it { should_not allow_value("B0WNL").for(:sedol) }
22
+ it { should_not allow_value("B0WNLY").for(:sedol) }
23
+ it { should_not allow_value("B0WNLY77").for(:sedol) }
24
+ it { should_not allow_value("! \#$%\`|").for(:sedol) }
25
+ it { should_not allow_value("<>@[]\`|").for(:sedol) }
26
+
27
+ it { should ensure_valid_sedol_format_of(:sedol) }
28
+ it { should_not ensure_valid_sedol_format_of(:name) }
29
+ end
30
+
31
+ end
@@ -0,0 +1,41 @@
1
+ require 'spec_helper'
2
+
3
+ describe SlugValidator do
4
+
5
+ context "has a valid value" do
6
+ let(:klass) do
7
+ Class.new do
8
+ include ActiveModel::Validations
9
+ attr_accessor :slug, :name
10
+ validates :slug, slug: true
11
+ end
12
+ end
13
+
14
+ subject { klass.new }
15
+
16
+ it { should allow_value("slug").for(:slug) }
17
+ it { should allow_value("slug1234").for(:slug) }
18
+ it { should allow_value("slug-word").for(:slug) }
19
+ it { should allow_value("slug-1234").for(:slug) }
20
+
21
+ it { should_not allow_value('').for(:slug) }
22
+ it { should_not allow_value(' ').for(:slug) }
23
+ it { should_not allow_value(nil).for(:slug) }
24
+ it { should_not allow_value(" slug").for(:slug) }
25
+ it { should_not allow_value(" slug ").for(:slug) }
26
+ it { should_not allow_value("slug ").for(:slug) }
27
+ it { should_not allow_value(" 1234").for(:slug) }
28
+ it { should_not allow_value(" 1234 ").for(:slug) }
29
+ it { should_not allow_value("1234 ").for(:slug) }
30
+ it { should_not allow_value("slug word").for(:slug) }
31
+ it { should_not allow_value("slug 1234").for(:slug) }
32
+ it { should_not allow_value("slug_word").for(:slug) }
33
+ it { should_not allow_value("slug_1234").for(:slug) }
34
+ it { should_not allow_value("! \#$%\`|").for(:slug) }
35
+ it { should_not allow_value("<>@[]\`|").for(:slug) }
36
+
37
+ it { should ensure_valid_slug_format_of(:slug) }
38
+ it { should_not ensure_valid_slug_format_of(:name) }
39
+ end
40
+
41
+ end
@@ -0,0 +1,36 @@
1
+ require 'spec_helper'
2
+
3
+ describe SsnValidator do
4
+
5
+ context "has a valid value" do
6
+ let(:klass) do
7
+ Class.new do
8
+ include ActiveModel::Validations
9
+ attr_accessor :ssn, :name
10
+ validates :ssn, ssn: true
11
+ end
12
+ end
13
+
14
+ subject { klass.new }
15
+
16
+ it { should allow_value("333-22-4444").for(:ssn) }
17
+ it { should allow_value("333224444").for(:ssn) }
18
+
19
+ it { should_not allow_value('').for(:ssn) }
20
+ it { should_not allow_value(' ').for(:ssn) }
21
+ it { should_not allow_value(nil).for(:ssn) }
22
+ it { should_not allow_value(" 333-22-4444").for(:ssn) }
23
+ it { should_not allow_value(" 333-22-4444 ").for(:ssn) }
24
+ it { should_not allow_value("333-22-4444 ").for(:ssn) }
25
+ it { should_not allow_value("333 22 4444").for(:ssn) }
26
+ it { should_not allow_value("333-22-444n").for(:ssn) }
27
+ it { should_not allow_value("333 22 4444").for(:ssn) }
28
+ it { should_not allow_value("3-2-4").for(:ssn) }
29
+ it { should_not allow_value("! \#$%\`|").for(:ssn) }
30
+ it { should_not allow_value("<>@[]\`|").for(:ssn) }
31
+
32
+ it { should ensure_valid_ssn_format_of(:ssn) }
33
+ it { should_not ensure_valid_ssn_format_of(:name) }
34
+ end
35
+
36
+ end
@@ -0,0 +1,106 @@
1
+ require 'spec_helper'
2
+
3
+ describe UrlValidator do
4
+
5
+ context "has valid format" do
6
+ let(:klass) do
7
+ Class.new do
8
+ include ActiveModel::Validations
9
+ attr_accessor :url, :name
10
+ validates :url, url: true
11
+ end
12
+ end
13
+
14
+ subject { klass.new }
15
+
16
+ it { should ensure_valid_url_format_of(:url) }
17
+ it { should_not ensure_valid_url_format_of(:name) }
18
+
19
+ it { should allow_value("http://example.com").for(:url) }
20
+ it { should allow_value("http://FooBar.cOm").for(:url) }
21
+ it { should allow_value("http://foo.bar.baz.com").for(:url) }
22
+ it { should allow_value("http://123.com").for(:url) }
23
+ it { should allow_value("http://www.example.ru").for(:url) }
24
+ it { should allow_value("http://user-example.co.uk").for(:url) }
25
+ it { should allow_value("https://example.com").for(:url) }
26
+ it { should allow_value("http://example.org/").for(:url) }
27
+ it { should allow_value("https://example.net/index.html").for(:url) }
28
+ it { should allow_value("http://example.net/login.php").for(:url) }
29
+ it { should allow_value("https://example.travel/").for(:url) }
30
+ it { should allow_value("http://example.aero").for(:url) }
31
+ it { should allow_value("http://example.aero?foo=bar").for(:url) }
32
+
33
+ it { should_not allow_value('').for(:url) }
34
+ it { should_not allow_value(' ').for(:url) }
35
+ it { should_not allow_value(nil).for(:url) }
36
+ it { should_not allow_value("example").for(:url) }
37
+ it { should_not allow_value("ftp://foo.bar.baz.com").for(:url) }
38
+ end
39
+
40
+ describe "url must be in a specific domain" do
41
+ let(:klass) do
42
+ Class.new do
43
+ include ActiveModel::Validations
44
+ attr_accessor :url1, :url2
45
+ validates :url1, url: { domain: :org }
46
+ validates :url2, url: { domain: [:org, 'edu', 'Com.Au'] }
47
+ end
48
+ end
49
+
50
+ subject { klass.new }
51
+
52
+ it { should allow_value("http://example.org").for(:url1) }
53
+ it { should_not allow_value("http://example.com").for(:url1) }
54
+
55
+ it { should allow_value("http://example.org").for(:url2) }
56
+ it { should allow_value("http://example.edu").for(:url2) }
57
+ it { should allow_value("http://example.com.au").for(:url2) }
58
+ it { should allow_value("http://example.Com.Au").for(:url2) }
59
+ it { should_not allow_value("http://example.com").for(:url2) }
60
+ end
61
+
62
+ describe "url must be domain root" do
63
+ let(:klass) do
64
+ Class.new do
65
+ include ActiveModel::Validations
66
+ attr_accessor :url1, :url2
67
+ validates :url1, url: { root: true }
68
+ validates :url2, url: { root: false }
69
+ end
70
+ end
71
+
72
+ subject { klass.new }
73
+
74
+ it { should allow_value("http://example.org").for(:url1) }
75
+ it { should allow_value("http://example.org/").for(:url1) }
76
+ it { should_not allow_value("http://example.com/test").for(:url1) }
77
+ it { should_not allow_value("http://example.com/#fragment").for(:url1) }
78
+ it { should_not allow_value("http://example.com/?key=value").for(:url1) }
79
+
80
+
81
+ it { should allow_value("http://example.org").for(:url2) }
82
+ it { should allow_value("http://example.org/lorem").for(:url2) }
83
+ end
84
+
85
+ describe "url must have a specific scheme" do
86
+ let(:klass) do
87
+ Class.new do
88
+ include ActiveModel::Validations
89
+ attr_accessor :url1, :url2
90
+ validates :url1, url: { scheme: 'http' }
91
+ validates :url2, url: { scheme: ['HTTP', :https] }
92
+ end
93
+ end
94
+
95
+ subject { klass.new }
96
+
97
+ it { should allow_value("http://example.org").for(:url1) }
98
+ it { should_not allow_value("https://example.org").for(:url1) }
99
+
100
+ it { should allow_value("http://example.org").for(:url2) }
101
+ it { should allow_value("https://example.org").for(:url2) }
102
+ it { should allow_value("HTTPS://example.org").for(:url2) }
103
+ it { should_not allow_value("ftp://example.org").for(:url2) }
104
+ end
105
+
106
+ end
@@ -0,0 +1,37 @@
1
+ require 'spec_helper'
2
+
3
+ describe UsernameValidator do
4
+
5
+ context "has a valid value" do
6
+ let(:klass) do
7
+ Class.new do
8
+ include ActiveModel::Validations
9
+ attr_accessor :username, :name
10
+ validates :username, username: true
11
+ end
12
+ end
13
+
14
+ subject { klass.new }
15
+
16
+ it { should allow_value("username").for(:username) }
17
+ it { should allow_value("username123").for(:username) }
18
+ it { should allow_value("username_123").for(:username) }
19
+ it { should allow_value("username-123").for(:username) }
20
+
21
+ it { should_not allow_value('').for(:username) }
22
+ it { should_not allow_value(' ').for(:username) }
23
+ it { should_not allow_value(nil).for(:username) }
24
+ it { should_not allow_value("u").for(:username) }
25
+ it { should_not allow_value(" username").for(:username) }
26
+ it { should_not allow_value(" username ").for(:username) }
27
+ it { should_not allow_value("username ").for(:username) }
28
+ it { should_not allow_value("user name").for(:username) }
29
+ it { should_not allow_value("username-123456789").for(:username) }
30
+ it { should_not allow_value("! \#$%\`|").for(:username) }
31
+ it { should_not allow_value("<>@[]\`|").for(:username) }
32
+
33
+ it { should ensure_valid_username_format_of(:username) }
34
+ it { should_not ensure_valid_username_format_of(:name) }
35
+ end
36
+
37
+ end