flash_validators 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.coveralls.yml +1 -0
- data/.gitignore +17 -0
- data/.rspec +4 -0
- data/.travis.yml +14 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +589 -0
- data/Rakefile +6 -0
- data/config/locales/en.yml +73 -0
- data/flash_validators.gemspec +28 -0
- data/lib/flash_validators/matchers/ensure_valid_boolean_format_of.rb +20 -0
- data/lib/flash_validators/matchers/ensure_valid_currency_format_of.rb +20 -0
- data/lib/flash_validators/matchers/ensure_valid_email_format_of.rb +20 -0
- data/lib/flash_validators/matchers/ensure_valid_equality_matcher_of.rb +32 -0
- data/lib/flash_validators/matchers/ensure_valid_hex_format_of.rb +20 -0
- data/lib/flash_validators/matchers/ensure_valid_imei_format_of.rb +20 -0
- data/lib/flash_validators/matchers/ensure_valid_ip_format_of.rb +20 -0
- data/lib/flash_validators/matchers/ensure_valid_latitude_format_of.rb +20 -0
- data/lib/flash_validators/matchers/ensure_valid_longitude_format_of.rb +20 -0
- data/lib/flash_validators/matchers/ensure_valid_mac_address_format_of.rb +20 -0
- data/lib/flash_validators/matchers/ensure_valid_name_format_of.rb +20 -0
- data/lib/flash_validators/matchers/ensure_valid_password_format_of.rb +20 -0
- data/lib/flash_validators/matchers/ensure_valid_phone_format_of.rb +20 -0
- data/lib/flash_validators/matchers/ensure_valid_slug_format_of.rb +20 -0
- data/lib/flash_validators/matchers/ensure_valid_ssn_format_of.rb +20 -0
- data/lib/flash_validators/matchers/ensure_valid_url_format_of.rb +20 -0
- data/lib/flash_validators/matchers/ensure_valid_username_format_of.rb +20 -0
- data/lib/flash_validators/validators/boolean_validator.rb +9 -0
- data/lib/flash_validators/validators/currency_validator.rb +17 -0
- data/lib/flash_validators/validators/email_validator.rb +25 -0
- data/lib/flash_validators/validators/equality_validator.rb +27 -0
- data/lib/flash_validators/validators/hex_validator.rb +9 -0
- data/lib/flash_validators/validators/imei_validator.rb +37 -0
- data/lib/flash_validators/validators/ip_validator.rb +9 -0
- data/lib/flash_validators/validators/latitude_validator.rb +9 -0
- data/lib/flash_validators/validators/longitude_validator.rb +9 -0
- data/lib/flash_validators/validators/mac_address_validator.rb +24 -0
- data/lib/flash_validators/validators/name_validator.rb +9 -0
- data/lib/flash_validators/validators/password_validator.rb +9 -0
- data/lib/flash_validators/validators/phone_validator.rb +9 -0
- data/lib/flash_validators/validators/slug_validator.rb +9 -0
- data/lib/flash_validators/validators/ssn_validator.rb +9 -0
- data/lib/flash_validators/validators/url_validator.rb +36 -0
- data/lib/flash_validators/validators/username_validator.rb +9 -0
- data/lib/flash_validators/version.rb +3 -0
- data/lib/flash_validators.rb +41 -0
- data/spec/lib/boolean_validator_spec.rb +35 -0
- data/spec/lib/currency_validator_spec.rb +63 -0
- data/spec/lib/email_validator_spec.rb +111 -0
- data/spec/lib/equality_validator_spec.rb +340 -0
- data/spec/lib/hex_validator_spec.rb +53 -0
- data/spec/lib/imei_validator_spec.rb +41 -0
- data/spec/lib/ip_validator_spec.rb +33 -0
- data/spec/lib/latitude_validator_spec.rb +31 -0
- data/spec/lib/longitude_validator_spec.rb +31 -0
- data/spec/lib/mac_address_validator_spec.rb +54 -0
- data/spec/lib/name_validator_spec.rb +39 -0
- data/spec/lib/password_validator_spec.rb +45 -0
- data/spec/lib/phone_validator_spec.rb +42 -0
- data/spec/lib/slug_validator_spec.rb +41 -0
- data/spec/lib/ssn_validator_spec.rb +36 -0
- data/spec/lib/url_validator_spec.rb +109 -0
- data/spec/lib/username_validator_spec.rb +37 -0
- data/spec/spec_helper.rb +11 -0
- metadata +224 -0
@@ -0,0 +1,73 @@
|
|
1
|
+
en:
|
2
|
+
errors:
|
3
|
+
messages:
|
4
|
+
boolean: "is not a valid boolean"
|
5
|
+
currency: "is not a valid currency"
|
6
|
+
email: "is not a valid email"
|
7
|
+
equality: "is not %{operator} %{attr}"
|
8
|
+
hex: "is not a valid hex color"
|
9
|
+
imei: "is not a valid IMEI"
|
10
|
+
ip: "is not a valid IP"
|
11
|
+
longitude: "is not a valid latitude"
|
12
|
+
longitude: "is not a valid longitude"
|
13
|
+
mac_address: "is not a valid MAC address"
|
14
|
+
name: "is not a valid name"
|
15
|
+
password: "is not a valid password"
|
16
|
+
phone: "is not a valid phone number"
|
17
|
+
slug: "is not a valid slug"
|
18
|
+
ssn: "is not a valid social security number"
|
19
|
+
url: "is not a valid URL"
|
20
|
+
username: "is not a valid username"
|
21
|
+
flash_validators:
|
22
|
+
matchers:
|
23
|
+
ensure_valid_boolean_format_of:
|
24
|
+
failure_message_for_should: "#{model} should ensure valid boolean format of attribute #{attr}"
|
25
|
+
failure_message_for_should_not: "#{model} should not ensure valid boolean format of attribute #{attr}"
|
26
|
+
ensure_valid_currency_format_of:
|
27
|
+
failure_message_for_should: "#{model} should ensure valid currency format of attribute #{attr}"
|
28
|
+
failure_message_for_should_not: "#{model} should not ensure valid currency format of attribute #{attr}"
|
29
|
+
ensure_valid_email_format_of:
|
30
|
+
failure_message_for_should: "#{model} should ensure valid email format of attribute #{attr}"
|
31
|
+
failure_message_for_should_not: "#{model} should not ensure valid email format of attribute #{attr}"
|
32
|
+
ensure_valid_equality_format_of:
|
33
|
+
failure_message_for_should: "#{model} should ensure equality of #{operator} on attribute #{attr}"
|
34
|
+
failure_message_for_should_not: "#{model} should not ensure equality of #{operator} on attribute #{attr}"
|
35
|
+
ensure_valid_hex_format_of:
|
36
|
+
failure_message_for_should: "#{model} should ensure valid hex format of attribute #{attr}"
|
37
|
+
failure_message_for_should_not: "#{model} should not ensure valid hex format of attribute #{attr}"
|
38
|
+
ensure_valid_imei_format_of:
|
39
|
+
failure_message_for_should: "#{model} should ensure valid IMEI address format of attribute #{attr}"
|
40
|
+
failure_message_for_should_not: "#{model} should not ensure valid IMEI address format of attribute #{attr}"
|
41
|
+
ensure_valid_ip_format_of:
|
42
|
+
failure_message_for_should: "#{model} should ensure valid IP address format of attribute #{attr}"
|
43
|
+
failure_message_for_should_not: "#{model} should not ensure valid IP address format of attribute #{attr}"
|
44
|
+
ensure_valid_latitude_format_of:
|
45
|
+
failure_message_for_should: "#{model} should ensure valid latitude format of attribute #{attr}"
|
46
|
+
failure_message_for_should_not: "#{model} should not ensure valid latitude format of attribute #{attr}"
|
47
|
+
ensure_valid_longitude_format_of:
|
48
|
+
failure_message_for_should: "#{model} should ensure valid longitude format of attribute #{attr}"
|
49
|
+
failure_message_for_should_not: "#{model} should not ensure valid longitude format of attribute #{attr}"
|
50
|
+
ensure_valid_mac_address_format_of:
|
51
|
+
failure_message_for_should: "#{model} should ensure valid MAC address format of attribute #{attr}"
|
52
|
+
failure_message_for_should_not: "#{model} should not ensure valid MAC address format of attribute #{attr}"
|
53
|
+
ensure_valid_name_format_of:
|
54
|
+
failure_message_for_should: "#{model} should ensure valid name format of attribute #{attr}"
|
55
|
+
failure_message_for_should_not: "#{model} should not ensure valid name format of attribute #{attr}"
|
56
|
+
ensure_valid_password_format_of:
|
57
|
+
failure_message_for_should: "#{model} should ensure valid password format of attribute #{attr}"
|
58
|
+
failure_message_for_should_not: "#{model} should not ensure valid password format of attribute #{attr}"
|
59
|
+
ensure_valid_phone_format_of:
|
60
|
+
failure_message_for_should: "#{model} should ensure valid phone format of attribute #{attr}"
|
61
|
+
failure_message_for_should_not: "#{model} should not ensure valid phone format of attribute #{attr}"
|
62
|
+
ensure_valid_slug_format_of:
|
63
|
+
failure_message_for_should: "#{model} should ensure valid slug format of attribute #{attr}"
|
64
|
+
failure_message_for_should_not: "#{model} should not ensure valid slug format of attribute #{attr}"
|
65
|
+
ensure_valid_ssn_format_of:
|
66
|
+
failure_message_for_should: "#{model} should ensure valid ssn format of attribute #{attr}"
|
67
|
+
failure_message_for_should_not: "#{model} should not ensure valid ssn format of attribute #{attr}"
|
68
|
+
ensure_valid_url_format_of:
|
69
|
+
failure_message_for_should: "#{model} should ensure valid URL format of attribute #{attr}"
|
70
|
+
failure_message_for_should_not: "#{model} should not ensure valid URL format of attribute #{attr}"
|
71
|
+
ensure_valid_username_format_of:
|
72
|
+
failure_message_for_should: "#{model} should ensure valid username format of attribute #{attr}"
|
73
|
+
failure_message_for_should_not: "#{model} should not ensure valid username format of attribute #{attr}"
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'flash_validators/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "flash_validators"
|
8
|
+
spec.version = FlashValidators::VERSION
|
9
|
+
spec.authors = ["Juan Gomez"]
|
10
|
+
spec.email = ["j.gomez@drexed.com"]
|
11
|
+
spec.summary = %q{Gem for commonly used model validators.}
|
12
|
+
spec.description = %q{Validate commonly used attributes easily with flash validators.}
|
13
|
+
spec.homepage = "https://github.com/drexed/flash_validators"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_dependency "activemodel", "> 3.0.0"
|
22
|
+
spec.add_dependency "activesupport", "> 3.0.0"
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.5"
|
24
|
+
spec.add_development_dependency "coveralls"
|
25
|
+
spec.add_development_dependency "rake"
|
26
|
+
spec.add_development_dependency "rspec", "~> 3.0.0.beta"
|
27
|
+
spec.add_development_dependency "shoulda-matchers"
|
28
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
RSpec::Matchers.define :ensure_valid_boolean_format_of do |attribute|
|
2
|
+
match do |model|
|
3
|
+
model.send("#{attribute}=", nil)
|
4
|
+
model.valid?
|
5
|
+
|
6
|
+
if model.errors.has_key?(attribute)
|
7
|
+
model.errors[attribute].include?(I18n.t('errors.messages.boolean'))
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
failure_message do |model|
|
12
|
+
I18n.t 'flash_validators.matchers.ensure_valid_boolean_format_of.failure_message_for_should',
|
13
|
+
model: model.class
|
14
|
+
end
|
15
|
+
|
16
|
+
failure_message_when_negated do |model|
|
17
|
+
I18n.t 'flash_validators.matchers.ensure_valid_boolean_format_of.failure_message_for_should_not',
|
18
|
+
model: model.class
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
RSpec::Matchers.define :ensure_valid_currency_format_of do |attribute|
|
2
|
+
match do |model|
|
3
|
+
model.send("#{attribute}=", "$1234.56")
|
4
|
+
model.valid?
|
5
|
+
|
6
|
+
if model.errors.has_key?(attribute)
|
7
|
+
model.errors[attribute].include?(I18n.t('errors.messages.currency'))
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
failure_message do |model|
|
12
|
+
I18n.t 'flash_validators.matchers.ensure_valid_currency_format_of.failure_message_for_should',
|
13
|
+
model: model.class
|
14
|
+
end
|
15
|
+
|
16
|
+
failure_message_when_negated do |model|
|
17
|
+
I18n.t 'flash_validators.matchers.ensure_valid_currency_format_of.failure_message_for_should_not',
|
18
|
+
model: model.class
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
RSpec::Matchers.define :ensure_valid_email_format_of do |attribute|
|
2
|
+
match do |model|
|
3
|
+
model.send("#{attribute}=", "invalid@email_address")
|
4
|
+
model.valid?
|
5
|
+
|
6
|
+
if model.errors.has_key?(attribute)
|
7
|
+
model.errors[attribute].include?(I18n.t('errors.messages.email'))
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
failure_message do |model|
|
12
|
+
I18n.t 'flash_validators.matchers.ensure_valid_email_format_of.failure_message_for_should',
|
13
|
+
model: model.class
|
14
|
+
end
|
15
|
+
|
16
|
+
failure_message_when_negated do |model|
|
17
|
+
I18n.t 'flash_validators.matchers.ensure_valid_email_format_of.failure_message_for_should_not',
|
18
|
+
model: model.class
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
RSpec::Matchers.define :ensure_equality_of do |attribute|
|
2
|
+
chain :to do |to|
|
3
|
+
@to = to
|
4
|
+
end
|
5
|
+
|
6
|
+
chain :operator do |operator|
|
7
|
+
@operator = operator
|
8
|
+
end
|
9
|
+
|
10
|
+
match do |model|
|
11
|
+
raise Exception if @to.nil? || @operator.nil?
|
12
|
+
|
13
|
+
value = model.send(attribute)
|
14
|
+
model.send("#{@to}=", value)
|
15
|
+
model.send("#{@operator}=", value)
|
16
|
+
model.valid?
|
17
|
+
|
18
|
+
if model.errors.has_key?(attribute)
|
19
|
+
model.errors[attribute].include?(I18n.t('errors.messages.equality', attr: @to, operator: @operator))
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
failure_message do |model|
|
24
|
+
I18n.t 'flash_validators.matchers.ensure_valid_equality_format_of.failure_message_for_should',
|
25
|
+
model: model.class, attr: attribute.inspect, operator: "operator"
|
26
|
+
end
|
27
|
+
|
28
|
+
failure_message_when_negated do |model|
|
29
|
+
I18n.t 'flash_validators.matchers.ensure_valid_equality_format_of.failure_message_for_should_not',
|
30
|
+
model: model.class, attr: attribute.inspect, operator: "operator"
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
RSpec::Matchers.define :ensure_valid_hex_format_of do |attribute|
|
2
|
+
match do |model|
|
3
|
+
model.send("#{attribute}=", "#9999999")
|
4
|
+
model.valid?
|
5
|
+
|
6
|
+
if model.errors.has_key?(attribute)
|
7
|
+
model.errors[attribute].include?(I18n.t('errors.messages.hex'))
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
failure_message do |model|
|
12
|
+
I18n.t 'flash_validators.matchers.ensure_valid_hex_format_of.failure_message_for_should',
|
13
|
+
model: model.class
|
14
|
+
end
|
15
|
+
|
16
|
+
failure_message_when_negated do |model|
|
17
|
+
I18n.t 'flash_validators.matchers.ensure_valid_hex_format_of.failure_message_for_should_not',
|
18
|
+
model: model.class
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
RSpec::Matchers.define :ensure_valid_imei_format_of do |attribute|
|
2
|
+
match do |model|
|
3
|
+
model.send("#{attribute}=", "invalid.imei")
|
4
|
+
model.valid?
|
5
|
+
|
6
|
+
if model.errors.has_key?(attribute)
|
7
|
+
model.errors[attribute].include?(I18n.t('errors.messages.imei'))
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
failure_message do |model|
|
12
|
+
I18n.t 'flash_validators.matchers.ensure_valid_imei_format_of.failure_message_for_should',
|
13
|
+
model: model.class
|
14
|
+
end
|
15
|
+
|
16
|
+
failure_message_when_negated do |model|
|
17
|
+
I18n.t 'flash_validators.matchers.ensure_valid_imei_format_of.failure_message_for_should_not',
|
18
|
+
model: model.class
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
RSpec::Matchers.define :ensure_valid_ip_format_of do |attribute|
|
2
|
+
match do |model|
|
3
|
+
model.send("#{attribute}=", ".")
|
4
|
+
model.valid?
|
5
|
+
|
6
|
+
if model.errors.has_key?(attribute)
|
7
|
+
model.errors[attribute].include?(I18n.t('errors.messages.ip'))
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
failure_message do |model|
|
12
|
+
I18n.t 'flash_validators.matchers.ensure_valid_ip_format_of.failure_message_for_should',
|
13
|
+
model: model.class
|
14
|
+
end
|
15
|
+
|
16
|
+
failure_message_when_negated do |model|
|
17
|
+
I18n.t 'flash_validators.matchers.ensure_valid_ip_format_of.failure_message_for_should_not',
|
18
|
+
model: model.class
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
RSpec::Matchers.define :ensure_valid_latitude_format_of do |attribute|
|
2
|
+
match do |model|
|
3
|
+
model.send("#{attribute}=", 91)
|
4
|
+
model.valid?
|
5
|
+
|
6
|
+
if model.errors.has_key?(attribute)
|
7
|
+
model.errors[attribute].include?(I18n.t('errors.messages.latitude'))
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
failure_message do |model|
|
12
|
+
I18n.t 'flash_validators.matchers.ensure_valid_latitude_format_of.failure_message_for_should',
|
13
|
+
model: model.class
|
14
|
+
end
|
15
|
+
|
16
|
+
failure_message_when_negated do |model|
|
17
|
+
I18n.t 'flash_validators.matchers.ensure_valid_latitude_format_of.failure_message_for_should_not',
|
18
|
+
model: model.class
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
RSpec::Matchers.define :ensure_valid_longitude_format_of do |attribute|
|
2
|
+
match do |model|
|
3
|
+
model.send("#{attribute}=", 181)
|
4
|
+
model.valid?
|
5
|
+
|
6
|
+
if model.errors.has_key?(attribute)
|
7
|
+
model.errors[attribute].include?(I18n.t('errors.messages.longitude'))
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
failure_message do |model|
|
12
|
+
I18n.t 'flash_validators.matchers.ensure_valid_longitude_format_of.failure_message_for_should',
|
13
|
+
model: model.class
|
14
|
+
end
|
15
|
+
|
16
|
+
failure_message_when_negated do |model|
|
17
|
+
I18n.t 'flash_validators.matchers.ensure_valid_longitude_format_of.failure_message_for_should_not',
|
18
|
+
model: model.class
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
RSpec::Matchers.define :ensure_valid_mac_address_format_of do |attribute|
|
2
|
+
match do |model|
|
3
|
+
model.send("#{attribute}=", "invalid.mac.address")
|
4
|
+
model.valid?
|
5
|
+
|
6
|
+
if model.errors.has_key?(attribute)
|
7
|
+
model.errors[attribute].include?(I18n.t('errors.messages.mac_address'))
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
failure_message do |model|
|
12
|
+
I18n.t 'flash_validators.matchers.ensure_valid_mac_address_format_of.failure_message_for_should',
|
13
|
+
model: model.class
|
14
|
+
end
|
15
|
+
|
16
|
+
failure_message_when_negated do |model|
|
17
|
+
I18n.t 'flash_validators.matchers.ensure_valid_mac_address_format_of.failure_message_for_should_not',
|
18
|
+
model: model.class
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
RSpec::Matchers.define :ensure_valid_name_format_of do |attribute|
|
2
|
+
match do |model|
|
3
|
+
model.send("#{attribute}=", "Jame$ Ear1 Jones")
|
4
|
+
model.valid?
|
5
|
+
|
6
|
+
if model.errors.has_key?(attribute)
|
7
|
+
model.errors[attribute].include?(I18n.t('errors.messages.name'))
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
failure_message do |model|
|
12
|
+
I18n.t 'flash_validators.matchers.ensure_valid_name_format_of.failure_message_for_should',
|
13
|
+
model: model.class
|
14
|
+
end
|
15
|
+
|
16
|
+
failure_message_when_negated do |model|
|
17
|
+
I18n.t 'flash_validators.matchers.ensure_valid_name_format_of.failure_message_for_should_not',
|
18
|
+
model: model.class
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
RSpec::Matchers.define :ensure_valid_password_format_of do |attribute|
|
2
|
+
match do |model|
|
3
|
+
model.send("#{attribute}=", "pass word")
|
4
|
+
model.valid?
|
5
|
+
|
6
|
+
if model.errors.has_key?(attribute)
|
7
|
+
model.errors[attribute].include?(I18n.t('errors.messages.password'))
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
failure_message do |model|
|
12
|
+
I18n.t 'flash_validators.matchers.ensure_valid_password_format_of.failure_message_for_should',
|
13
|
+
model: model.class
|
14
|
+
end
|
15
|
+
|
16
|
+
failure_message_when_negated do |model|
|
17
|
+
I18n.t 'flash_validators.matchers.ensure_valid_password_format_of.failure_message_for_should_not',
|
18
|
+
model: model.class
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
RSpec::Matchers.define :ensure_valid_phone_format_of do |attribute|
|
2
|
+
match do |model|
|
3
|
+
model.send("#{attribute}=", "555-123n")
|
4
|
+
model.valid?
|
5
|
+
|
6
|
+
if model.errors.has_key?(attribute)
|
7
|
+
model.errors[attribute].include?(I18n.t('errors.messages.phone'))
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
failure_message do |model|
|
12
|
+
I18n.t 'flash_validators.matchers.ensure_valid_phone_format_of.failure_message_for_should',
|
13
|
+
model: model.class
|
14
|
+
end
|
15
|
+
|
16
|
+
failure_message_when_negated do |model|
|
17
|
+
I18n.t 'flash_validators.matchers.ensure_valid_phone_format_of.failure_message_for_should_not',
|
18
|
+
model: model.class
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
RSpec::Matchers.define :ensure_valid_slug_format_of do |attribute|
|
2
|
+
match do |model|
|
3
|
+
model.send("#{attribute}=", "sl ug")
|
4
|
+
model.valid?
|
5
|
+
|
6
|
+
if model.errors.has_key?(attribute)
|
7
|
+
model.errors[attribute].include?(I18n.t('errors.messages.slug'))
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
failure_message do |model|
|
12
|
+
I18n.t 'flash_validators.matchers.ensure_valid_slug_format_of.failure_message_for_should',
|
13
|
+
model: model.class
|
14
|
+
end
|
15
|
+
|
16
|
+
failure_message_when_negated do |model|
|
17
|
+
I18n.t 'flash_validators.matchers.ensure_valid_slug_format_of.failure_message_for_should_not',
|
18
|
+
model: model.class
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
RSpec::Matchers.define :ensure_valid_ssn_format_of do |attribute|
|
2
|
+
match do |model|
|
3
|
+
model.send("#{attribute}=", "555-0s-1234")
|
4
|
+
model.valid?
|
5
|
+
|
6
|
+
if model.errors.has_key?(attribute)
|
7
|
+
model.errors[attribute].include?(I18n.t('errors.messages.ssn'))
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
failure_message do |model|
|
12
|
+
I18n.t 'flash_validators.matchers.ensure_valid_ssn_format_of.failure_message_for_should',
|
13
|
+
model: model.class
|
14
|
+
end
|
15
|
+
|
16
|
+
failure_message_when_negated do |model|
|
17
|
+
I18n.t 'flash_validators.matchers.ensure_valid_ssn_format_of.failure_message_for_should_not',
|
18
|
+
model: model.class
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
RSpec::Matchers.define :ensure_valid_url_format_of do |attribute|
|
2
|
+
match do |model|
|
3
|
+
model.send("#{attribute}=", ".")
|
4
|
+
model.valid?
|
5
|
+
|
6
|
+
if model.errors.has_key?(attribute)
|
7
|
+
model.errors[attribute].include?(I18n.t('errors.messages.url'))
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
failure_message do |model|
|
12
|
+
I18n.t 'flash_validators.matchers.ensure_valid_url_format_of.failure_message_for_should',
|
13
|
+
model: model.class
|
14
|
+
end
|
15
|
+
|
16
|
+
failure_message_when_negated do |model|
|
17
|
+
I18n.t 'flash_validators.matchers.ensure_valid_url_format_of.failure_message_for_should_not',
|
18
|
+
model: model.class
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
RSpec::Matchers.define :ensure_valid_username_format_of do |attribute|
|
2
|
+
match do |model|
|
3
|
+
model.send("#{attribute}=", "user name")
|
4
|
+
model.valid?
|
5
|
+
|
6
|
+
if model.errors.has_key?(attribute)
|
7
|
+
model.errors[attribute].include?(I18n.t('errors.messages.username'))
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
failure_message do |model|
|
12
|
+
I18n.t 'flash_validators.matchers.ensure_valid_username_format_of.failure_message_for_should',
|
13
|
+
model: model.class
|
14
|
+
end
|
15
|
+
|
16
|
+
failure_message_when_negated do |model|
|
17
|
+
I18n.t 'flash_validators.matchers.ensure_valid_username_format_of.failure_message_for_should_not',
|
18
|
+
model: model.class
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
class CurrencyValidator < ActiveModel::EachValidator
|
2
|
+
|
3
|
+
def validate_each(record, attribute, value)
|
4
|
+
strict_mode = options[:strict] || false
|
5
|
+
|
6
|
+
if strict_mode
|
7
|
+
format = /^\d+(\.\d{2})$/ # Strict: requires leading number and exactly two decimals, 1.45
|
8
|
+
else
|
9
|
+
format = /^\d*+(\.\d{1,2})$/
|
10
|
+
end
|
11
|
+
|
12
|
+
unless value =~ format
|
13
|
+
record.errors[attribute] << (options[:message] || I18n.t('errors.messages.currency'))
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
class EmailValidator < ActiveModel::EachValidator
|
2
|
+
|
3
|
+
def validate_each(record, attribute, value)
|
4
|
+
unless valid?(value, options)
|
5
|
+
record.errors[attribute] << (options[:message] || I18n.t('errors.messages.email'))
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
private
|
10
|
+
|
11
|
+
def self.validate_format(email)
|
12
|
+
!!(email =~ /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i)
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.validate_domain(email, domains)
|
16
|
+
email_downcased = email.to_s.downcase
|
17
|
+
domains.empty? || domains.any? { |domain| email_downcased.end_with?(".#{domain.downcase}") }
|
18
|
+
end
|
19
|
+
|
20
|
+
def valid?(email, options)
|
21
|
+
self.class.validate_format(email) \
|
22
|
+
&& self.class.validate_domain(email, [*(options[:domain])])
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
class EqualityValidator < ActiveModel::EachValidator
|
2
|
+
|
3
|
+
def validate_each(record, attribute, value)
|
4
|
+
to_value = options[:to]
|
5
|
+
operator = OPERATORS.fetch(options[:operator])
|
6
|
+
|
7
|
+
if (options[:operator] == :less_than) || (options[:operator] == :greater_than)
|
8
|
+
raise Exception if to_value.nil? || operator.nil?
|
9
|
+
end
|
10
|
+
|
11
|
+
unless value.send(operator, record.send(to_value.to_sym))
|
12
|
+
record.errors[attribute] << (options[:message] || I18n.t('errors.messages.equality', attr: to_value, operator: operator))
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
OPERATORS = {
|
19
|
+
less_than: :<,
|
20
|
+
less_than_or_equal_to: :<=,
|
21
|
+
greater_than: :>,
|
22
|
+
greater_than_or_equal_to: :>=,
|
23
|
+
equal_to: :==,
|
24
|
+
not_equal_to: :!=
|
25
|
+
}
|
26
|
+
|
27
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
class ImeiValidator < ActiveModel::EachValidator
|
2
|
+
|
3
|
+
def validate_each(record, attribute, value)
|
4
|
+
unless valid?(value.to_s, options)
|
5
|
+
record.errors[attribute] << (options[:message] || I18n.t('errors.messages.imei'))
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
private
|
10
|
+
|
11
|
+
def self.validate_format(imei_number)
|
12
|
+
!!(imei_number =~ /\A[\d\.\:\-\s]+\z/i) # 356843052637512 or 35-6843052-637512 or 35.6843052.637512
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.luhn_valid?(input)
|
16
|
+
numbers = input.gsub(/\D/, '').reverse
|
17
|
+
|
18
|
+
sum, i = 0, 0
|
19
|
+
|
20
|
+
numbers.each_char do |ch|
|
21
|
+
n = ch.to_i
|
22
|
+
n *= 2 if i.odd?
|
23
|
+
n = 1 + (n - 10) if n >= 10
|
24
|
+
|
25
|
+
sum += n
|
26
|
+
i += 1
|
27
|
+
end
|
28
|
+
|
29
|
+
(sum % 10).zero?
|
30
|
+
end
|
31
|
+
|
32
|
+
def valid?(imei, options)
|
33
|
+
self.class.validate_format(imei) \
|
34
|
+
&& self.class.luhn_valid?(imei)
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
@@ -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('errors.messages.ip'))
|
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('errors.messages.longitude'))
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
end
|