flash_validators 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (66) hide show
  1. checksums.yaml +7 -0
  2. data/.coveralls.yml +1 -0
  3. data/.gitignore +17 -0
  4. data/.rspec +4 -0
  5. data/.travis.yml +14 -0
  6. data/Gemfile +4 -0
  7. data/LICENSE.txt +22 -0
  8. data/README.md +589 -0
  9. data/Rakefile +6 -0
  10. data/config/locales/en.yml +73 -0
  11. data/flash_validators.gemspec +28 -0
  12. data/lib/flash_validators/matchers/ensure_valid_boolean_format_of.rb +20 -0
  13. data/lib/flash_validators/matchers/ensure_valid_currency_format_of.rb +20 -0
  14. data/lib/flash_validators/matchers/ensure_valid_email_format_of.rb +20 -0
  15. data/lib/flash_validators/matchers/ensure_valid_equality_matcher_of.rb +32 -0
  16. data/lib/flash_validators/matchers/ensure_valid_hex_format_of.rb +20 -0
  17. data/lib/flash_validators/matchers/ensure_valid_imei_format_of.rb +20 -0
  18. data/lib/flash_validators/matchers/ensure_valid_ip_format_of.rb +20 -0
  19. data/lib/flash_validators/matchers/ensure_valid_latitude_format_of.rb +20 -0
  20. data/lib/flash_validators/matchers/ensure_valid_longitude_format_of.rb +20 -0
  21. data/lib/flash_validators/matchers/ensure_valid_mac_address_format_of.rb +20 -0
  22. data/lib/flash_validators/matchers/ensure_valid_name_format_of.rb +20 -0
  23. data/lib/flash_validators/matchers/ensure_valid_password_format_of.rb +20 -0
  24. data/lib/flash_validators/matchers/ensure_valid_phone_format_of.rb +20 -0
  25. data/lib/flash_validators/matchers/ensure_valid_slug_format_of.rb +20 -0
  26. data/lib/flash_validators/matchers/ensure_valid_ssn_format_of.rb +20 -0
  27. data/lib/flash_validators/matchers/ensure_valid_url_format_of.rb +20 -0
  28. data/lib/flash_validators/matchers/ensure_valid_username_format_of.rb +20 -0
  29. data/lib/flash_validators/validators/boolean_validator.rb +9 -0
  30. data/lib/flash_validators/validators/currency_validator.rb +17 -0
  31. data/lib/flash_validators/validators/email_validator.rb +25 -0
  32. data/lib/flash_validators/validators/equality_validator.rb +27 -0
  33. data/lib/flash_validators/validators/hex_validator.rb +9 -0
  34. data/lib/flash_validators/validators/imei_validator.rb +37 -0
  35. data/lib/flash_validators/validators/ip_validator.rb +9 -0
  36. data/lib/flash_validators/validators/latitude_validator.rb +9 -0
  37. data/lib/flash_validators/validators/longitude_validator.rb +9 -0
  38. data/lib/flash_validators/validators/mac_address_validator.rb +24 -0
  39. data/lib/flash_validators/validators/name_validator.rb +9 -0
  40. data/lib/flash_validators/validators/password_validator.rb +9 -0
  41. data/lib/flash_validators/validators/phone_validator.rb +9 -0
  42. data/lib/flash_validators/validators/slug_validator.rb +9 -0
  43. data/lib/flash_validators/validators/ssn_validator.rb +9 -0
  44. data/lib/flash_validators/validators/url_validator.rb +36 -0
  45. data/lib/flash_validators/validators/username_validator.rb +9 -0
  46. data/lib/flash_validators/version.rb +3 -0
  47. data/lib/flash_validators.rb +41 -0
  48. data/spec/lib/boolean_validator_spec.rb +35 -0
  49. data/spec/lib/currency_validator_spec.rb +63 -0
  50. data/spec/lib/email_validator_spec.rb +111 -0
  51. data/spec/lib/equality_validator_spec.rb +340 -0
  52. data/spec/lib/hex_validator_spec.rb +53 -0
  53. data/spec/lib/imei_validator_spec.rb +41 -0
  54. data/spec/lib/ip_validator_spec.rb +33 -0
  55. data/spec/lib/latitude_validator_spec.rb +31 -0
  56. data/spec/lib/longitude_validator_spec.rb +31 -0
  57. data/spec/lib/mac_address_validator_spec.rb +54 -0
  58. data/spec/lib/name_validator_spec.rb +39 -0
  59. data/spec/lib/password_validator_spec.rb +45 -0
  60. data/spec/lib/phone_validator_spec.rb +42 -0
  61. data/spec/lib/slug_validator_spec.rb +41 -0
  62. data/spec/lib/ssn_validator_spec.rb +36 -0
  63. data/spec/lib/url_validator_spec.rb +109 -0
  64. data/spec/lib/username_validator_spec.rb +37 -0
  65. data/spec/spec_helper.rb +11 -0
  66. metadata +224 -0
@@ -0,0 +1,24 @@
1
+ class MacAddressValidator < ActiveModel::EachValidator
2
+
3
+ def validate_each(record, attribute, value)
4
+ unless self.class.valid?(value, options)
5
+ record.errors[attribute] << (options[:message] || I18n.t('errors.messages.mac_address'))
6
+ end
7
+ end
8
+
9
+ private
10
+
11
+ def self.validate_format(mac_address)
12
+ !!(mac_address =~ /^([\h]{2}:){5}[\h]{2}?$/i) || # 08:00:2b:01:02:03
13
+ !!(mac_address =~ /^([\h]{2}[-|\.|\s]){5}[\h]{2}?$/i) || # 08-00-2b-01-02-03 or 08.00.2b.01.02.03
14
+ !!(mac_address =~ /^([\h]{6})[-|\.][\h]{6}?$/i) || # 08002b-010203 or 08002b.010203
15
+ !!(mac_address =~ /^([\h]{6}):[\h]{6}?$/i) || # 08002b:010203
16
+ !!(mac_address =~ /^([\h]{4}[-|\.|\s]){2}[\h]{4}?$/i) || # 0800.2b01.0203 or 0800-2b01-0203 0800 2b01 0203
17
+ !!(mac_address =~ /^[\h]{12}?$/i) # 08002b010203
18
+ end
19
+
20
+ def self.valid?(mac_address, options)
21
+ validate_format(mac_address)
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('errors.messages.name'))
6
+ end
7
+ end
8
+
9
+ end
@@ -0,0 +1,9 @@
1
+ class PasswordValidator < ActiveModel::EachValidator
2
+
3
+ def validate_each(record, attribute, value)
4
+ unless value =~ /^[a-z0-9!@#$%^&*_-]{6,18}$/
5
+ record.errors[attribute] << (options[:message] || I18n.t('errors.messages.password'))
6
+ end
7
+ end
8
+
9
+ 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('errors.messages.phone'))
6
+ end
7
+ end
8
+
9
+ 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('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('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('errors.messages.url'))
9
+ end
10
+
11
+ private
12
+
13
+ DEFAULT_SCHEMES = [:http, :https]
14
+
15
+ def self.validate_domain(uri, domains)
16
+ host_downcased = uri.host.to_s.downcase
17
+ domains.empty? || domains.any? { |domain| host_downcased.end_with?(".#{domain.downcase}") }
18
+ end
19
+
20
+ def self.validate_scheme(uri, schemes)
21
+ scheme_downcased = uri.scheme.to_s.downcase
22
+ schemes.empty? || schemes.any? { |scheme| scheme_downcased == scheme.to_s.downcase }
23
+ end
24
+
25
+ def self.validate_root(uri)
26
+ ['/', ''].include?(uri.path) && uri.query.blank? && uri.fragment.blank?
27
+ end
28
+
29
+ def valid?(uri, options)
30
+ uri.kind_of?(URI::Generic) \
31
+ && self.class.validate_domain(uri, [*(options[:domain])]) \
32
+ && self.class.validate_scheme(uri, [*(options[:scheme] || UrlValidator::DEFAULT_SCHEMES)]) \
33
+ && (!!options[:root] ? self.class.validate_root(uri) : 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('errors.messages.username'))
6
+ end
7
+ end
8
+
9
+ end
@@ -0,0 +1,3 @@
1
+ module FlashValidators
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,41 @@
1
+ require 'active_model'
2
+ require 'active_support'
3
+ require 'rspec/matchers'
4
+ require 'flash_validators/version'
5
+ require 'flash_validators/validators/boolean_validator'
6
+ require 'flash_validators/validators/currency_validator'
7
+ require 'flash_validators/validators/email_validator'
8
+ require 'flash_validators/validators/equality_validator'
9
+ require 'flash_validators/validators/hex_validator'
10
+ require 'flash_validators/validators/imei_validator'
11
+ require 'flash_validators/validators/ip_validator'
12
+ require 'flash_validators/validators/latitude_validator'
13
+ require 'flash_validators/validators/longitude_validator'
14
+ require 'flash_validators/validators/mac_address_validator'
15
+ require 'flash_validators/validators/name_validator'
16
+ require 'flash_validators/validators/password_validator'
17
+ require 'flash_validators/validators/phone_validator'
18
+ require 'flash_validators/validators/slug_validator'
19
+ require 'flash_validators/validators/ssn_validator'
20
+ require 'flash_validators/validators/url_validator'
21
+ require 'flash_validators/validators/username_validator'
22
+
23
+ if defined?(RSpec)
24
+ require 'flash_validators/matchers/ensure_valid_boolean_format_of'
25
+ require 'flash_validators/matchers/ensure_valid_currency_format_of'
26
+ require 'flash_validators/matchers/ensure_valid_email_format_of'
27
+ require 'flash_validators/matchers/ensure_valid_equality_matcher_of'
28
+ require 'flash_validators/matchers/ensure_valid_hex_format_of'
29
+ require 'flash_validators/matchers/ensure_valid_imei_format_of'
30
+ require 'flash_validators/matchers/ensure_valid_ip_format_of'
31
+ require 'flash_validators/matchers/ensure_valid_latitude_format_of'
32
+ require 'flash_validators/matchers/ensure_valid_longitude_format_of'
33
+ require 'flash_validators/matchers/ensure_valid_mac_address_format_of'
34
+ require 'flash_validators/matchers/ensure_valid_name_format_of'
35
+ require 'flash_validators/matchers/ensure_valid_password_format_of'
36
+ require 'flash_validators/matchers/ensure_valid_phone_format_of'
37
+ require 'flash_validators/matchers/ensure_valid_slug_format_of'
38
+ require 'flash_validators/matchers/ensure_valid_ssn_format_of'
39
+ require 'flash_validators/matchers/ensure_valid_url_format_of'
40
+ require 'flash_validators/matchers/ensure_valid_username_format_of'
41
+ end
@@ -0,0 +1,35 @@
1
+ require 'spec_helper'
2
+
3
+ describe BooleanValidator do
4
+
5
+ context "Boolean has a valid value" do
6
+ let(:klass) do
7
+ Class.new do
8
+ include ActiveModel::Validations
9
+ attr_accessor :active, :name
10
+ validates :active, boolean: true
11
+ end
12
+ end
13
+
14
+ subject { klass.new }
15
+
16
+ it { should allow_value(true).for(:active) }
17
+ it { should allow_value(false).for(:active) }
18
+ it { should allow_value(1).for(:active) }
19
+ it { should allow_value(0).for(:active) }
20
+
21
+ it { should_not allow_value('').for(:active) }
22
+ it { should_not allow_value(' ').for(:active) }
23
+ it { should_not allow_value(nil).for(:active) }
24
+ it { should_not allow_value("true").for(:active) }
25
+ it { should_not allow_value("false").for(:active) }
26
+ it { should_not allow_value("1").for(:active) }
27
+ it { should_not allow_value("0").for(:active) }
28
+ it { should_not allow_value("! \#$%\`|").for(:active) }
29
+ it { should_not allow_value("<>@[]\`|").for(:active) }
30
+
31
+ it { should ensure_valid_boolean_format_of(:active) }
32
+ it { should_not ensure_valid_boolean_format_of(:name) }
33
+ end
34
+
35
+ end
@@ -0,0 +1,63 @@
1
+ require 'spec_helper'
2
+
3
+ describe CurrencyValidator do
4
+
5
+ context "Currency has a valid value" do
6
+ let(:klass) do
7
+ Class.new do
8
+ include ActiveModel::Validations
9
+ attr_accessor :price, :name
10
+ validates :price, currency: true
11
+ end
12
+ end
13
+
14
+ subject { klass.new }
15
+
16
+ it { should allow_value(".00").for(:price) }
17
+ it { should allow_value("1.0").for(:price) }
18
+ it { should allow_value("1.00").for(:price) }
19
+ it { should allow_value("12345678.00").for(:price) }
20
+
21
+ it { should_not allow_value('').for(:price) }
22
+ it { should_not allow_value(' ').for(:price) }
23
+ it { should_not allow_value(nil).for(:price) }
24
+ it { should_not allow_value("1").for(:price) }
25
+ it { should_not allow_value("1.000").for(:price) }
26
+ it { should_not allow_value("$1.00").for(:price) }
27
+ it { should_not allow_value("! \#$%\`|").for(:price) }
28
+ it { should_not allow_value("<>@[]\`|").for(:price) }
29
+
30
+ it { should ensure_valid_currency_format_of(:price) }
31
+ it { should_not ensure_valid_currency_format_of(:name) }
32
+ end
33
+
34
+ context "Currency with strict option has a valid value" do
35
+ let(:klass) do
36
+ Class.new do
37
+ include ActiveModel::Validations
38
+ attr_accessor :price, :name
39
+ validates :price, currency: { strict: true }
40
+ end
41
+ end
42
+
43
+ subject { klass.new }
44
+
45
+ it { should allow_value("1.00").for(:price) }
46
+ it { should allow_value("12345678.00").for(:price) }
47
+
48
+ it { should_not allow_value('').for(:price) }
49
+ it { should_not allow_value(' ').for(:price) }
50
+ it { should_not allow_value(nil).for(:price) }
51
+ it { should_not allow_value(".00").for(:price) }
52
+ it { should_not allow_value("1").for(:price) }
53
+ it { should_not allow_value("1.0").for(:price) }
54
+ it { should_not allow_value("1.000").for(:price) }
55
+ it { should_not allow_value("$1.00").for(:price) }
56
+ it { should_not allow_value("! \#$%\`|").for(:price) }
57
+ it { should_not allow_value("<>@[]\`|").for(:price) }
58
+
59
+ it { should ensure_valid_currency_format_of(:price) }
60
+ it { should_not ensure_valid_currency_format_of(:name) }
61
+ end
62
+
63
+ end
@@ -0,0 +1,111 @@
1
+ require 'spec_helper'
2
+
3
+ describe EmailValidator do
4
+
5
+ subject { klass.new }
6
+
7
+ ## Valid email formats
8
+ context "Email with valid format" do
9
+ let(:klass) do
10
+ Class.new do
11
+ include ActiveModel::Validations
12
+ attr_accessor :email, :name
13
+ validates :email, email: true
14
+ end
15
+ end
16
+
17
+ [
18
+ "s_u@example.com",
19
+ "super.user@example.com",
20
+ "super+user@example.com",
21
+ "super-user@example.com",
22
+ "super+user@example-site.com",
23
+ "user@example.com",
24
+ "user@example-site.com",
25
+ "user@en.example.com",
26
+ "user@example.museum",
27
+ "user@123.com",
28
+ "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ@example.com",
29
+ "01234567890@example.com"
30
+ ].each do |email|
31
+ it "#{email.inspect} should be valid" do
32
+ should allow_value(email).for(:email)
33
+ end
34
+ end
35
+
36
+ [
37
+ "",
38
+ " ",
39
+ "example.com",
40
+ "super user@example.com",
41
+ " user@example.com",
42
+ " user@example.com ",
43
+ "user@example.com ",
44
+ "user",
45
+ "user@com",
46
+ "user@.com",
47
+ "user@example",
48
+ "user@example.",
49
+ "user@example.c",
50
+ "user_example.com",
51
+ "user@example_site.com",
52
+ "user@example.com@example.com",
53
+ "user@",
54
+ "user@! \"\#$%(),/;<>_[]\`|.com",
55
+ "user@127.0.0.1",
56
+ "user@127.0.0.1:25",
57
+ "user@example.com\n<script>alert('hello')</script>",
58
+ "@example.com",
59
+ "@example",
60
+ "@",
61
+ "! \#$%\`|@example.com",
62
+ "<>@[]\`|@example.com"
63
+ ].each do |email|
64
+ it "#{email.inspect} should be invalid" do
65
+ should_not allow_value(email).for(:email)
66
+ end
67
+ end
68
+
69
+ it { should ensure_valid_email_format_of(:email) }
70
+ it { should_not ensure_valid_email_format_of(:name) }
71
+ end
72
+
73
+ ## Valid email domain formats
74
+ context "Email is in the specific domain" do
75
+ context "Email domain specified as string" do
76
+ let(:klass) do
77
+ Class.new do
78
+ include ActiveModel::Validations
79
+ attr_accessor :email, :name
80
+ validates :email, email: { domain: "edu" }
81
+ end
82
+ end
83
+
84
+ it { should allow_value("user@example.edu").for(:email) }
85
+ it { should_not allow_value("user@example.com").for(:email) }
86
+
87
+ it { should ensure_valid_email_format_of(:email) }
88
+ it { should_not ensure_valid_email_format_of(:name) }
89
+ end
90
+
91
+ context "Email set as an array of strings and symbols" do
92
+ let(:klass) do
93
+ Class.new do
94
+ include ActiveModel::Validations
95
+ attr_accessor :email, :name
96
+ validates :email, email: { domain: ['com', :edu, 'Com.Au'] }
97
+ end
98
+ end
99
+
100
+ it { should allow_value("user@example.com").for(:email) }
101
+ it { should allow_value("user@example.edu").for(:email) }
102
+ it { should allow_value("user@example.com.au").for(:email) }
103
+ it { should allow_value("user@example.Com.Au").for(:email) }
104
+ it { should_not allow_value("user@example.org").for(:email) }
105
+
106
+ it { should ensure_valid_email_format_of(:email) }
107
+ it { should_not ensure_valid_email_format_of(:name) }
108
+ end
109
+ end
110
+
111
+ end