yukonisuru 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (96) 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 +1101 -0
  9. data/Rakefile +1 -0
  10. data/config/locales/en.yml +113 -0
  11. data/lib/yukonisuru.rb +93 -0
  12. data/lib/yukonisuru/matchers/ensure_valid_alpha_format_of.rb +26 -0
  13. data/lib/yukonisuru/matchers/ensure_valid_alpha_numeric_format_of.rb +26 -0
  14. data/lib/yukonisuru/matchers/ensure_valid_base64_format_of.rb +26 -0
  15. data/lib/yukonisuru/matchers/ensure_valid_boolean_format_of.rb +26 -0
  16. data/lib/yukonisuru/matchers/ensure_valid_credit_card_format_of.rb +26 -0
  17. data/lib/yukonisuru/matchers/ensure_valid_currency_format_of.rb +26 -0
  18. data/lib/yukonisuru/matchers/ensure_valid_cusip_format_of.rb +26 -0
  19. data/lib/yukonisuru/matchers/ensure_valid_email_format_of.rb +26 -0
  20. data/lib/yukonisuru/matchers/ensure_valid_equality_matcher_of.rb +40 -0
  21. data/lib/yukonisuru/matchers/ensure_valid_gtin_format_of.rb +26 -0
  22. data/lib/yukonisuru/matchers/ensure_valid_hex_format_of.rb +26 -0
  23. data/lib/yukonisuru/matchers/ensure_valid_imei_format_of.rb +26 -0
  24. data/lib/yukonisuru/matchers/ensure_valid_ip_format_of.rb +26 -0
  25. data/lib/yukonisuru/matchers/ensure_valid_isbn_format_of.rb +26 -0
  26. data/lib/yukonisuru/matchers/ensure_valid_isin_format_of.rb +26 -0
  27. data/lib/yukonisuru/matchers/ensure_valid_latitude_format_of.rb +26 -0
  28. data/lib/yukonisuru/matchers/ensure_valid_longitude_format_of.rb +26 -0
  29. data/lib/yukonisuru/matchers/ensure_valid_mac_address_format_of.rb +26 -0
  30. data/lib/yukonisuru/matchers/ensure_valid_name_format_of.rb +26 -0
  31. data/lib/yukonisuru/matchers/ensure_valid_password_format_of.rb +26 -0
  32. data/lib/yukonisuru/matchers/ensure_valid_phone_format_of.rb +26 -0
  33. data/lib/yukonisuru/matchers/ensure_valid_sedol_format_of.rb +26 -0
  34. data/lib/yukonisuru/matchers/ensure_valid_slug_format_of.rb +26 -0
  35. data/lib/yukonisuru/matchers/ensure_valid_ssn_format_of.rb +26 -0
  36. data/lib/yukonisuru/matchers/ensure_valid_url_format_of.rb +26 -0
  37. data/lib/yukonisuru/matchers/ensure_valid_username_format_of.rb +26 -0
  38. data/lib/yukonisuru/matchers/ensure_valid_uuid_format_of.rb +26 -0
  39. data/lib/yukonisuru/validators/alpha_numeric_validator.rb +31 -0
  40. data/lib/yukonisuru/validators/alpha_validator.rb +31 -0
  41. data/lib/yukonisuru/validators/base64_validator.rb +9 -0
  42. data/lib/yukonisuru/validators/boolean_validator.rb +9 -0
  43. data/lib/yukonisuru/validators/credit_card_validator.rb +133 -0
  44. data/lib/yukonisuru/validators/currency_validator.rb +23 -0
  45. data/lib/yukonisuru/validators/cusip_validator.rb +33 -0
  46. data/lib/yukonisuru/validators/email_validator.rb +25 -0
  47. data/lib/yukonisuru/validators/equality_validator.rb +27 -0
  48. data/lib/yukonisuru/validators/gtin_validator.rb +59 -0
  49. data/lib/yukonisuru/validators/hex_validator.rb +9 -0
  50. data/lib/yukonisuru/validators/imei_validator.rb +37 -0
  51. data/lib/yukonisuru/validators/ip_validator.rb +9 -0
  52. data/lib/yukonisuru/validators/isbn_validator.rb +24 -0
  53. data/lib/yukonisuru/validators/isin_validator.rb +38 -0
  54. data/lib/yukonisuru/validators/latitude_validator.rb +9 -0
  55. data/lib/yukonisuru/validators/longitude_validator.rb +9 -0
  56. data/lib/yukonisuru/validators/mac_address_validator.rb +24 -0
  57. data/lib/yukonisuru/validators/name_validator.rb +9 -0
  58. data/lib/yukonisuru/validators/password_validator.rb +23 -0
  59. data/lib/yukonisuru/validators/phone_validator.rb +9 -0
  60. data/lib/yukonisuru/validators/sedol_validator.rb +32 -0
  61. data/lib/yukonisuru/validators/slug_validator.rb +9 -0
  62. data/lib/yukonisuru/validators/ssn_validator.rb +9 -0
  63. data/lib/yukonisuru/validators/url_validator.rb +36 -0
  64. data/lib/yukonisuru/validators/username_validator.rb +9 -0
  65. data/lib/yukonisuru/validators/uuid_validator.rb +28 -0
  66. data/lib/yukonisuru/version.rb +3 -0
  67. data/spec/lib/alpha_numeric_validator_spec.rb +91 -0
  68. data/spec/lib/alpha_validator_spec.rb +182 -0
  69. data/spec/lib/base64_validator_spec.rb +33 -0
  70. data/spec/lib/boolean_validator_spec.rb +35 -0
  71. data/spec/lib/credit_card_validator_spec.rb +686 -0
  72. data/spec/lib/currency_validator_spec.rb +63 -0
  73. data/spec/lib/cusip_validator_spec.rb +27 -0
  74. data/spec/lib/email_validator_spec.rb +109 -0
  75. data/spec/lib/equality_validator_spec.rb +334 -0
  76. data/spec/lib/gtin_validator_spec.rb +101 -0
  77. data/spec/lib/hex_validator_spec.rb +73 -0
  78. data/spec/lib/imei_validator_spec.rb +41 -0
  79. data/spec/lib/ip_validator_spec.rb +33 -0
  80. data/spec/lib/isbn_validator_spec.rb +41 -0
  81. data/spec/lib/isin_validator_spec.rb +35 -0
  82. data/spec/lib/latitude_validator_spec.rb +31 -0
  83. data/spec/lib/longitude_validator_spec.rb +31 -0
  84. data/spec/lib/mac_address_validator_spec.rb +54 -0
  85. data/spec/lib/name_validator_spec.rb +39 -0
  86. data/spec/lib/password_validator_spec.rb +87 -0
  87. data/spec/lib/phone_validator_spec.rb +42 -0
  88. data/spec/lib/sedol_validator_spec.rb +31 -0
  89. data/spec/lib/slug_validator_spec.rb +41 -0
  90. data/spec/lib/ssn_validator_spec.rb +36 -0
  91. data/spec/lib/url_validator_spec.rb +109 -0
  92. data/spec/lib/username_validator_spec.rb +37 -0
  93. data/spec/lib/uuid_validator_spec.rb +157 -0
  94. data/spec/spec_helper.rb +12 -0
  95. data/yukonisuru.gemspec +29 -0
  96. metadata +264 -0
@@ -0,0 +1,9 @@
1
+ class IpValidator < ActiveModel::EachValidator
2
+
3
+ def validate_each(record, attribute, value)
4
+ unless value =~ /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/
5
+ record.errors[attribute] << (options[:message] || I18n.t('yukonisuru.errors.messages.ip'))
6
+ end
7
+ end
8
+
9
+ end
@@ -0,0 +1,24 @@
1
+ class IsbnValidator < ActiveModel::EachValidator
2
+
3
+ def validate_each(record, attribute, value)
4
+ unless valid?(value.to_s)
5
+ record.errors[attribute] << (options[:message] || I18n.t('yukonisuru.errors.messages.isbn'))
6
+ end
7
+ end
8
+
9
+ private
10
+
11
+ def valid_format?(value)
12
+ value = '' if value.nil?
13
+ value.gsub!(/-| /, '')
14
+ value.downcase!
15
+
16
+ [10, 13].include?(value.size) &&
17
+ value.chars.all? { |char| ['0','1','2','3','4','5','6','7','8','9','0','x'].include?(char) }
18
+ end
19
+
20
+ def valid?(value)
21
+ valid_format?(value)
22
+ end
23
+
24
+ end
@@ -0,0 +1,38 @@
1
+ class IsinValidator < ActiveModel::EachValidator
2
+
3
+ def validate_each(record, attribute, value)
4
+ unless valid?(value)
5
+ record.errors[attribute] << (options[:message] || I18n.t('yukonisuru.errors.messages.isin'))
6
+ end
7
+ end
8
+
9
+ private
10
+
11
+ def valid_format?(value)
12
+ value =~ /^((AF|AX|AL|DZ|AS|AD|AO|AI|AQ|AG|AR|AM|AW|AU|AT|AZ|BS|BH|BD|BB|BY|BE|BZ|BJ|BM|BT|BO|BQ|BA|BW|BV|BR|IO|BN|BG|BF|BI|KH|CM|CA|CV|KY|CF|TD|CL|CN|CX|CC|CO|KM|CG|CD|CK|CR|CI|HR|CU|CW|CY|CZ|DK|DJ|DM|DO|EC|EG|SV|GQ|ER|EE|ET|FK|FO|FJ|FI|FR|GF|PF|TF|GA|GM|GE|DE|GH|GI|GR|GL|GD|GP|GU|GT|GG|GN|GW|GY|HT|HM|VA|HN|HK|HU|IS|IN|ID|IR|IQ|IE|IM|IL|IT|JM|JP|JE|JO|KZ|KE|KI|KP|KR|KW|KG|LA|LV|LB|LS|LR|LY|LI|LT|LU|MO|MK|MG|MW|MY|MV|ML|MT|MH|MQ|MR|MU|YT|MX|FM|MD|MC|MN|ME|MS|MA|MZ|MM|NA|NR|NP|NL|NC|NZ|NI|NE|NG|NU|NF|MP|NO|OM|PK|PW|PS|PA|PG|PY|PE|PH|PN|PL|PT|PR|QA|RE|RO|RU|RW|BL|SH|KN|LC|MF|PM|VC|WS|SM|ST|SA|SN|RS|SC|SL|SG|SX|SK|SI|SB|SO|ZA|GS|SS|ES|LK|SD|SR|SJ|SZ|SE|CH|SY|TW|TJ|TZ|TH|TL|TG|TK|TO|TT|TN|TR|TM|TC|TV|UG|UA|AE|GB|US|UM|UY|UZ|VU|VE|VN|VG|VI|WF|EH|YE|ZM|ZW{2})([A-Z0-9]{9}))(\d{1})$/
13
+ end
14
+
15
+ def valid_checksum?(value)
16
+ digits = value.split('').map { |i| i.match(/[A-Z]/) ? (i.ord - 55) : i.to_i }
17
+ even_values = digits.values_at(* digits.each_index.select { |i| i.even? })
18
+ odd_values = digits.values_at(* digits.each_index.select { |i| i.odd? })
19
+
20
+ longest, shortest = if even_values.last == value.split('').map(&:to_i)
21
+ [even_values, odd_values]
22
+ else
23
+ [odd_values, even_values]
24
+ end
25
+
26
+ longest = longest.map { |i| i * 2 }
27
+ values = (longest.concat(shortest)).to_s.scan(/./).map(&:to_i)
28
+ sum = values.inject(&:+)
29
+
30
+ (10 - (sum % 10)) % 10
31
+ end
32
+
33
+ def valid?(value)
34
+ valid_format?(value) &&
35
+ valid_checksum?(value)
36
+ end
37
+
38
+ end
@@ -0,0 +1,9 @@
1
+ class LatitudeValidator < ActiveModel::EachValidator
2
+
3
+ def validate_each(record, attribute, value)
4
+ unless value.present? && value >= -90 && value <= 90
5
+ record.errors[attribute] << (options[:message] || I18n.t('yukonisuru.errors.messages.latitude'))
6
+ end
7
+ end
8
+
9
+ end
@@ -0,0 +1,9 @@
1
+ class LongitudeValidator < ActiveModel::EachValidator
2
+
3
+ def validate_each(record, attribute, value)
4
+ unless value.present? && value >= -180 && value <= 180
5
+ record.errors[attribute] << (options[:message] || I18n.t('yukonisuru.errors.messages.longitude'))
6
+ end
7
+ end
8
+
9
+ end
@@ -0,0 +1,24 @@
1
+ class MacAddressValidator < ActiveModel::EachValidator
2
+
3
+ def validate_each(record, attribute, value)
4
+ unless valid?(value)
5
+ record.errors[attribute] << (options[:message] || I18n.t('yukonisuru.errors.messages.mac_address'))
6
+ end
7
+ end
8
+
9
+ private
10
+
11
+ def valid_format?(value)
12
+ (value =~ /^([\h]{2}:){5}[\h]{2}?$/i) ||
13
+ (value =~ /^([\h]{2}[-|\.|\s]){5}[\h]{2}?$/i) ||
14
+ (value =~ /^([\h]{6})[-|\.][\h]{6}?$/i) ||
15
+ (value =~ /^([\h]{6}):[\h]{6}?$/i) ||
16
+ (value =~ /^([\h]{4}[-|\.|\s]){2}[\h]{4}?$/i) ||
17
+ (value =~ /^[\h]{12}?$/i)
18
+ end
19
+
20
+ def valid?(value)
21
+ valid_format?(value)
22
+ end
23
+
24
+ end
@@ -0,0 +1,9 @@
1
+ class NameValidator < ActiveModel::EachValidator
2
+
3
+ def validate_each(record, attribute, value)
4
+ unless value =~ /\A([a-zA-Z'-]+\s+){1,4}[a-zA-Z'-]*\z/i
5
+ record.errors[attribute] << (options[:message] || I18n.t('yukonisuru.errors.messages.name'))
6
+ end
7
+ end
8
+
9
+ end
@@ -0,0 +1,23 @@
1
+ class PasswordValidator < ActiveModel::EachValidator
2
+
3
+ def validate_each(record, attribute, value)
4
+ unless valid?(value, options)
5
+ record.errors[attribute] << (options[:message] || I18n.t('yukonisuru.errors.messages.password'))
6
+ end
7
+ end
8
+
9
+ private
10
+
11
+ def valid_format?(value, options)
12
+ if options[:strict]
13
+ value =~ /^(?=^.{1,255}$)((?=.*[A-Za-z0-9])(?=.*[A-Z])(?=.*[a-z]))^.*$/
14
+ else
15
+ value =~ /^[a-z0-9!@#$%^&*_-]{1,255}$/
16
+ end
17
+ end
18
+
19
+ def valid?(value, options)
20
+ valid_format?(value, options)
21
+ end
22
+
23
+ end
@@ -0,0 +1,9 @@
1
+ class PhoneValidator < ActiveModel::EachValidator
2
+
3
+ def validate_each(record, attribute, value)
4
+ unless value =~ /^[0-9+\(\)#\.\s\/ext-]+$/i
5
+ record.errors[attribute] << (options[:message] || I18n.t('yukonisuru.errors.messages.phone'))
6
+ end
7
+ end
8
+
9
+ end
@@ -0,0 +1,32 @@
1
+ class SedolValidator < ActiveModel::EachValidator
2
+
3
+ def validate_each(record, attribute, value)
4
+ unless valid?(value)
5
+ record.errors[attribute] << (options[:message] || I18n.t('yukonisuru.errors.messages.sedol'))
6
+ end
7
+ end
8
+
9
+ private
10
+
11
+ def valid_format?(value)
12
+ value =~ /^([A-Z0-9]{6})(\d{1})$/
13
+ end
14
+
15
+ def valid_checksum?(value)
16
+ digits = value.split('').map { |i| i.match(/[A-Z]/) ? (i.ord - 55) : i.to_i }
17
+ weights = [1, 3, 1, 7, 3, 9, 1]
18
+ sum = 0
19
+
20
+ digits.each_with_index do |i, idx|
21
+ sum += weights[idx] * i
22
+ end
23
+
24
+ (10 - sum % 10) % 10
25
+ end
26
+
27
+ def valid?(value)
28
+ valid_format?(value) &&
29
+ valid_checksum?(value)
30
+ end
31
+
32
+ end
@@ -0,0 +1,9 @@
1
+ class SlugValidator < ActiveModel::EachValidator
2
+
3
+ def validate_each(record, attribute, value)
4
+ unless value =~ /^[a-z0-9-]+$/
5
+ record.errors[attribute] << (options[:message] || I18n.t('yukonisuru.errors.messages.slug'))
6
+ end
7
+ end
8
+
9
+ end
@@ -0,0 +1,9 @@
1
+ class SsnValidator < ActiveModel::EachValidator
2
+
3
+ def validate_each(record, attribute, value)
4
+ unless value =~ /^\A([\d]{3}\-[\d]{2}\-[\d]{4}|[\d]{9})\Z$/
5
+ record.errors[attribute] << (options[:message] || I18n.t('yukonisuru.errors.messages.ssn'))
6
+ end
7
+ end
8
+
9
+ end
@@ -0,0 +1,36 @@
1
+ require 'uri'
2
+ class UrlValidator < ActiveModel::EachValidator
3
+
4
+ def validate_each(record, attribute, value)
5
+ uri = URI.parse(value)
6
+ raise URI::InvalidURIError unless valid?(uri, options)
7
+ rescue URI::InvalidURIError
8
+ record.errors[attribute] << (options[:message] || I18n.t('yukonisuru.errors.messages.url'))
9
+ end
10
+
11
+ private
12
+
13
+ DEFAULT_SCHEMES = [:http, :https]
14
+
15
+ def valid_domain?(value, options)
16
+ value_downcased = value.host.to_s.downcase
17
+ options.empty? || options.any? { |domain| value_downcased.end_with?(".#{domain.downcase}") }
18
+ end
19
+
20
+ def valid_scheme?(value, options)
21
+ value_downcased = value.scheme.to_s.downcase
22
+ options.empty? || options.any? { |scheme| value_downcased == scheme.to_s.downcase }
23
+ end
24
+
25
+ def valid_root?(value)
26
+ ['/', ''].include?(value.path) && value.query.blank? && value.fragment.blank?
27
+ end
28
+
29
+ def valid?(value, options)
30
+ value.kind_of?(URI::Generic) &&
31
+ valid_domain?(value, [*(options[:domain])]) &&
32
+ valid_scheme?(value, [*(options[:scheme] || UrlValidator::DEFAULT_SCHEMES)]) &&
33
+ (options[:root] ? valid_root?(value) : true)
34
+ end
35
+
36
+ end
@@ -0,0 +1,9 @@
1
+ class UsernameValidator < ActiveModel::EachValidator
2
+
3
+ def validate_each(record, attribute, value)
4
+ unless value =~ /^[a-z0-9_-]{2,16}$/
5
+ record.errors[attribute] << (options[:message] || I18n.t('yukonisuru.errors.messages.username'))
6
+ end
7
+ end
8
+
9
+ end
@@ -0,0 +1,28 @@
1
+ class UuidValidator < ActiveModel::EachValidator
2
+
3
+ def validate_each(record, attribute, value)
4
+ unless valid?(value, options)
5
+ record.errors[attribute] << (options[:message] || I18n.t('yukonisuru.errors.messages.uuid'))
6
+ end
7
+ end
8
+
9
+ private
10
+
11
+ def valid_format?(value, options)
12
+ case options[:version]
13
+ when 3
14
+ value =~ /^[0-9A-F]{8}-[0-9A-F]{4}-3[0-9A-F]{3}-[0-9A-F]{4}-[0-9A-F]{12}$/i
15
+ when 4
16
+ value =~ /^[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i
17
+ when 5
18
+ value =~ /^[0-9A-F]{8}-[0-9A-F]{4}-5[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i
19
+ else
20
+ value =~ /^[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}$/i
21
+ end
22
+ end
23
+
24
+ def valid?(value, options)
25
+ valid_format?(value, options)
26
+ end
27
+
28
+ end
@@ -0,0 +1,3 @@
1
+ module Yukonisuru
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,91 @@
1
+ require 'spec_helper'
2
+
3
+ describe AlphaNumericValidator do
4
+
5
+ context "has a valid value" do
6
+ let(:klass) do
7
+ Class.new do
8
+ include ActiveModel::Validations
9
+ attr_accessor :title, :name
10
+ validates :title, alpha_numeric: true
11
+ end
12
+ end
13
+
14
+ subject { klass.new }
15
+
16
+ it { should allow_value("Example").for(:title) }
17
+ it { should allow_value("Example Title").for(:title) }
18
+ it { should allow_value("Example1").for(:title) }
19
+ it { should allow_value("Example 1").for(:title) }
20
+
21
+ it { should_not allow_value('').for(:title) }
22
+ it { should_not allow_value(' ').for(:title) }
23
+ it { should_not allow_value(nil).for(:title) }
24
+ it { should_not allow_value("Ex-ample").for(:title) }
25
+ it { should_not allow_value("Ex-ample1").for(:title) }
26
+ it { should_not allow_value("! \#$%\`|").for(:title) }
27
+ it { should_not allow_value("<>@[]\`|").for(:title) }
28
+
29
+ it { should ensure_valid_alpha_numeric_format_of(:title) }
30
+ it { should_not ensure_valid_alpha_numeric_format_of(:name) }
31
+ end
32
+
33
+ context "with :strict option has a valid value" do
34
+ let(:klass) do
35
+ Class.new do
36
+ include ActiveModel::Validations
37
+ attr_accessor :title, :name
38
+ validates :title, alpha_numeric: { strict: true }
39
+ end
40
+ end
41
+
42
+ subject { klass.new }
43
+
44
+ it { should allow_value("Example").for(:title) }
45
+ it { should allow_value("Example1").for(:title) }
46
+
47
+ it { should_not allow_value('').for(:title) }
48
+ it { should_not allow_value(' ').for(:title) }
49
+ it { should_not allow_value(nil).for(:title) }
50
+ it { should_not allow_value("Example Title").for(:title) }
51
+ it { should_not allow_value("Example 1").for(:title) }
52
+ it { should_not allow_value("Ex-ample").for(:title) }
53
+ it { should_not allow_value("Ex-ample1").for(:title) }
54
+ it { should_not allow_value("! \#$%\`|").for(:title) }
55
+ it { should_not allow_value("<>@[]\`|").for(:title) }
56
+
57
+ it { should ensure_valid_alpha_numeric_format_of(:title) }
58
+ it { should_not ensure_valid_alpha_numeric_format_of(:name) }
59
+ end
60
+
61
+ context "with case: :lower option has a valid value" do
62
+ let(:klass) do
63
+ Class.new do
64
+ include ActiveModel::Validations
65
+ attr_accessor :title, :name
66
+ validates :title, alpha_numeric: { case: :lower }
67
+ end
68
+ end
69
+
70
+ subject { klass.new }
71
+
72
+ it { should allow_value("example1").for(:title) }
73
+ it { should allow_value("example title 1").for(:title) }
74
+
75
+ it { should_not allow_value('').for(:title) }
76
+ it { should_not allow_value(' ').for(:title) }
77
+ it { should_not allow_value(nil).for(:title) }
78
+ it { should_not allow_value("Example").for(:title) }
79
+ it { should_not allow_value("Example Title").for(:title) }
80
+ it { should_not allow_value("Example 1").for(:title) }
81
+ it { should_not allow_value("Example1").for(:title) }
82
+ it { should_not allow_value("Ex-ample").for(:title) }
83
+ it { should_not allow_value("Ex-ample1").for(:title) }
84
+ it { should_not allow_value("! \#$%\`|").for(:title) }
85
+ it { should_not allow_value("<>@[]\`|").for(:title) }
86
+
87
+ it { should ensure_valid_alpha_numeric_format_of(:title) }
88
+ it { should_not ensure_valid_alpha_numeric_format_of(:name) }
89
+ end
90
+
91
+ end
@@ -0,0 +1,182 @@
1
+ require 'spec_helper'
2
+
3
+ describe AlphaValidator do
4
+
5
+ context "has a valid value" do
6
+ let(:klass) do
7
+ Class.new do
8
+ include ActiveModel::Validations
9
+ attr_accessor :title, :name
10
+ validates :title, alpha: true
11
+ end
12
+ end
13
+
14
+ subject { klass.new }
15
+
16
+ it { should allow_value("Example").for(:title) }
17
+ it { should allow_value("Example Title").for(:title) }
18
+
19
+ it { should_not allow_value('').for(:title) }
20
+ it { should_not allow_value(' ').for(:title) }
21
+ it { should_not allow_value(nil).for(:title) }
22
+ it { should_not allow_value("Example1").for(:title) }
23
+ it { should_not allow_value("Example 1").for(:title) }
24
+ it { should_not allow_value("Ex-ample1").for(:title) }
25
+ it { should_not allow_value("! \#$%\`|").for(:title) }
26
+ it { should_not allow_value("<>@[]\`|").for(:title) }
27
+
28
+ it { should ensure_valid_alpha_format_of(:title) }
29
+ it { should_not ensure_valid_alpha_format_of(:name) }
30
+ end
31
+
32
+ context "with :strict option has a valid value" do
33
+ let(:klass) do
34
+ Class.new do
35
+ include ActiveModel::Validations
36
+ attr_accessor :title, :name
37
+ validates :title, alpha: { strict: true }
38
+ end
39
+ end
40
+
41
+ subject { klass.new }
42
+
43
+ it { should allow_value("Example").for(:title) }
44
+
45
+ it { should_not allow_value('').for(:title) }
46
+ it { should_not allow_value(' ').for(:title) }
47
+ it { should_not allow_value(nil).for(:title) }
48
+ it { should_not allow_value("Example Title").for(:title) }
49
+ it { should_not allow_value("Example 1").for(:title) }
50
+ it { should_not allow_value("Example1").for(:title) }
51
+ it { should_not allow_value("Ex-ample").for(:title) }
52
+ it { should_not allow_value("Ex-ample1").for(:title) }
53
+ it { should_not allow_value("! \#$%\`|").for(:title) }
54
+ it { should_not allow_value("<>@[]\`|").for(:title) }
55
+
56
+ it { should ensure_valid_alpha_format_of(:title) }
57
+ it { should_not ensure_valid_alpha_format_of(:name) }
58
+ end
59
+
60
+ context "with case: :lower option has a valid value" do
61
+ let(:klass) do
62
+ Class.new do
63
+ include ActiveModel::Validations
64
+ attr_accessor :title, :name
65
+ validates :title, alpha: { case: :lower }
66
+ end
67
+ end
68
+
69
+ subject { klass.new }
70
+
71
+ it { should allow_value("example").for(:title) }
72
+ it { should allow_value("example title").for(:title) }
73
+
74
+ it { should_not allow_value('').for(:title) }
75
+ it { should_not allow_value(' ').for(:title) }
76
+ it { should_not allow_value(nil).for(:title) }
77
+ it { should_not allow_value("Example").for(:title) }
78
+ it { should_not allow_value("Example Title").for(:title) }
79
+ it { should_not allow_value("Example 1").for(:title) }
80
+ it { should_not allow_value("Example1").for(:title) }
81
+ it { should_not allow_value("Ex-ample").for(:title) }
82
+ it { should_not allow_value("Ex-ample1").for(:title) }
83
+ it { should_not allow_value("! \#$%\`|").for(:title) }
84
+ it { should_not allow_value("<>@[]\`|").for(:title) }
85
+
86
+ it { should ensure_valid_alpha_format_of(:title) }
87
+ it { should_not ensure_valid_alpha_format_of(:name) }
88
+ end
89
+
90
+ context "with case: :upper option has a valid value" do
91
+ let(:klass) do
92
+ Class.new do
93
+ include ActiveModel::Validations
94
+ attr_accessor :title, :name
95
+ validates :title, alpha: { case: :upper }
96
+ end
97
+ end
98
+
99
+ subject { klass.new }
100
+
101
+ it { should allow_value("EXAMPLE").for(:title) }
102
+ it { should allow_value("EXAMPLE TITLE").for(:title) }
103
+
104
+ it { should_not allow_value('').for(:title) }
105
+ it { should_not allow_value(' ').for(:title) }
106
+ it { should_not allow_value(nil).for(:title) }
107
+ it { should_not allow_value("example").for(:title) }
108
+ it { should_not allow_value("Example").for(:title) }
109
+ it { should_not allow_value("Example Title").for(:title) }
110
+ it { should_not allow_value("Example 1").for(:title) }
111
+ it { should_not allow_value("Example1").for(:title) }
112
+ it { should_not allow_value("Ex-ample").for(:title) }
113
+ it { should_not allow_value("Ex-ample1").for(:title) }
114
+ it { should_not allow_value("! \#$%\`|").for(:title) }
115
+ it { should_not allow_value("<>@[]\`|").for(:title) }
116
+
117
+ it { should ensure_valid_alpha_format_of(:title) }
118
+ it { should_not ensure_valid_alpha_format_of(:name) }
119
+ end
120
+
121
+ context "with case: :lower and :strict option has a valid value" do
122
+ let(:klass) do
123
+ Class.new do
124
+ include ActiveModel::Validations
125
+ attr_accessor :title, :name
126
+ validates :title, alpha: { case: :lower, strict: true }
127
+ end
128
+ end
129
+
130
+ subject { klass.new }
131
+
132
+ it { should allow_value("example").for(:title) }
133
+
134
+ it { should_not allow_value('').for(:title) }
135
+ it { should_not allow_value(' ').for(:title) }
136
+ it { should_not allow_value(nil).for(:title) }
137
+ it { should_not allow_value("Example").for(:title) }
138
+ it { should_not allow_value("Example Title").for(:title) }
139
+ it { should_not allow_value("Example 1").for(:title) }
140
+ it { should_not allow_value("Example1").for(:title) }
141
+ it { should_not allow_value("Ex-ample").for(:title) }
142
+ it { should_not allow_value("Ex-ample1").for(:title) }
143
+ it { should_not allow_value("example title").for(:title) }
144
+ it { should_not allow_value("! \#$%\`|").for(:title) }
145
+ it { should_not allow_value("<>@[]\`|").for(:title) }
146
+
147
+ it { should ensure_valid_alpha_format_of(:title) }
148
+ it { should_not ensure_valid_alpha_format_of(:name) }
149
+ end
150
+
151
+ context "with case: :upper and :strict option has a valid value" do
152
+ let(:klass) do
153
+ Class.new do
154
+ include ActiveModel::Validations
155
+ attr_accessor :title, :name
156
+ validates :title, alpha: { case: :upper, strict: true }
157
+ end
158
+ end
159
+
160
+ subject { klass.new }
161
+
162
+ it { should allow_value("EXAMPLE").for(:title) }
163
+
164
+ it { should_not allow_value('').for(:title) }
165
+ it { should_not allow_value(' ').for(:title) }
166
+ it { should_not allow_value(nil).for(:title) }
167
+ it { should_not allow_value("example").for(:title) }
168
+ it { should_not allow_value("Example").for(:title) }
169
+ it { should_not allow_value("Example Title").for(:title) }
170
+ it { should_not allow_value("Example 1").for(:title) }
171
+ it { should_not allow_value("Example1").for(:title) }
172
+ it { should_not allow_value("Ex-ample").for(:title) }
173
+ it { should_not allow_value("Ex-ample1").for(:title) }
174
+ it { should_not allow_value("EXAMPLE TITLE").for(:title) }
175
+ it { should_not allow_value("! \#$%\`|").for(:title) }
176
+ it { should_not allow_value("<>@[]\`|").for(:title) }
177
+
178
+ it { should ensure_valid_alpha_format_of(:title) }
179
+ it { should_not ensure_valid_alpha_format_of(:name) }
180
+ end
181
+
182
+ end