format_validators 0.0.5 → 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +5 -0
- data/README.rdoc +5 -1
- data/VERSION +1 -1
- data/app/validators/currency_format_validator.rb +7 -2
- data/app/validators/devise_password_validator.rb +21 -0
- data/format_validators.gemspec +3 -1
- data/spec/validators/currency_format_validator_spec.rb +54 -44
- data/spec/validators/devise_password_validator_spec.rb +68 -0
- metadata +4 -2
data/CHANGELOG
CHANGED
data/README.rdoc
CHANGED
@@ -10,9 +10,13 @@ A consolidation of a few of the most common validations.
|
|
10
10
|
* Social Security Number
|
11
11
|
* ZIP code
|
12
12
|
|
13
|
+
== Bundler
|
14
|
+
|
15
|
+
gem "format_validators"
|
16
|
+
|
13
17
|
== Install
|
14
18
|
|
15
|
-
|
19
|
+
gem install format_validators
|
16
20
|
|
17
21
|
== Examples:
|
18
22
|
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.6
|
@@ -1,7 +1,12 @@
|
|
1
1
|
class CurrencyFormatValidator < ActiveModel::EachValidator
|
2
2
|
def validate_each record, attribute, value
|
3
|
-
|
3
|
+
strict_mode = options[:strict] || false
|
4
|
+
|
5
|
+
format = /^\d*+(\.\d{1,2})?$/
|
6
|
+
# Strict: requires leading number and exactly two decimals, 1.45
|
7
|
+
format = /^\d+(\.\d{2})?$/ if strict_mode
|
8
|
+
|
4
9
|
message = attribute.to_s.humanize + " must contain dollars and cents, seperated by a period"
|
5
|
-
record.errors[attribute] << (options[:message] || message) unless value =~ format
|
10
|
+
record.errors[attribute] << (options[:message] || message) unless value.to_s =~ format
|
6
11
|
end
|
7
12
|
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# Used on a device user model to remove password checks when updating the user model.
|
2
|
+
# Passwords are not needed unless the user is a new record or the reset password token is present
|
3
|
+
#
|
4
|
+
# A model using this validation must include a password, password_confirmation, and rest_password_token fields.
|
5
|
+
class DevisePasswordValidator < ActiveModel::EachValidator
|
6
|
+
|
7
|
+
def validate_each record, attribute, value
|
8
|
+
if (record.password.blank? || record.password_confirmation.blank?) && (record.id.blank? || (!record.id.blank? && !record.reset_password_token.blank?))
|
9
|
+
record.errors[:password] << "is required"
|
10
|
+
elsif (!record.password.blank? || !record.password_confirmation.blank?) && record.password_confirmation != record.password
|
11
|
+
record.errors[:password] << "does not match Password Confirmation"
|
12
|
+
record.errors[:password_confirmation] = nil
|
13
|
+
elsif (!record.password.blank? || !record.password_confirmation.blank?) && record.password.length < 6
|
14
|
+
record.errors[:password] << "must be longer than 6 characters"
|
15
|
+
elsif record.password
|
16
|
+
if (record.password.include?(' ') || record.password_confirmation.include?(' '))
|
17
|
+
record.errors[:password] << 'cannot include spaces'
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
data/format_validators.gemspec
CHANGED
@@ -5,7 +5,7 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = "format_validators"
|
8
|
-
s.version = "0.0.
|
8
|
+
s.version = "0.0.6"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Jeremiah Hemphill"]
|
@@ -28,6 +28,7 @@ Gem::Specification.new do |s|
|
|
28
28
|
"VERSION",
|
29
29
|
"app/validators/currency_format_validator.rb",
|
30
30
|
"app/validators/days_of_week_validator.rb",
|
31
|
+
"app/validators/devise_password_validator.rb",
|
31
32
|
"app/validators/florida_counties_validator.rb",
|
32
33
|
"app/validators/phone_format_validator.rb",
|
33
34
|
"app/validators/ssn_format_validator.rb",
|
@@ -77,6 +78,7 @@ Gem::Specification.new do |s|
|
|
77
78
|
"spec/support/basic_record.rb",
|
78
79
|
"spec/validators/currency_format_validator_spec.rb",
|
79
80
|
"spec/validators/days_of_week_validator_spec.rb",
|
81
|
+
"spec/validators/devise_password_validator_spec.rb",
|
80
82
|
"spec/validators/florida_counties_integration_spec.rb",
|
81
83
|
"spec/validators/florida_counties_spec.rb",
|
82
84
|
"spec/validators/phone_format_validator_spec.rb",
|
@@ -1,51 +1,61 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe CurrencyFormatValidator do
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
it
|
22
|
-
|
23
|
-
|
24
|
-
end
|
25
|
-
|
26
|
-
it "should validate the format for ##" do
|
27
|
-
@record.errors[:cost].should_not_receive("<<")
|
28
|
-
@validator.validate_each(@record, :cost, "45")
|
29
|
-
end
|
30
|
-
|
31
|
-
it "should not validate the format for #.#.#" do
|
32
|
-
@record.errors[:cost].should_receive("<<")
|
33
|
-
@validator.validate_each(@record, :cost, "4.5.6")
|
34
|
-
end
|
35
|
-
|
36
|
-
it "should not validate the format for string" do
|
37
|
-
@record.errors[:cost].should_receive("<<")
|
38
|
-
@validator.validate_each(@record, :cost, "word")
|
39
|
-
end
|
40
|
-
|
41
|
-
it "should not validate the format for #.#" do
|
42
|
-
@record.errors[:cost].should_receive("<<")
|
43
|
-
@validator.validate_each(@record, :cost, "1.2")
|
44
|
-
end
|
4
|
+
before :each do
|
5
|
+
@validator = CurrencyFormatValidator.new attributes: {}
|
6
|
+
@mock = mock
|
7
|
+
@mock.stub!(:errors).and_return([])
|
8
|
+
@mock.errors.stub!(:[]).and_return({})
|
9
|
+
@mock.errors[].stub!(:<<)
|
10
|
+
end
|
11
|
+
|
12
|
+
subject { @validator }
|
13
|
+
|
14
|
+
# Correct data yields no errors!
|
15
|
+
context 'with perfectly valid, correctly-formatted input should produce no errors' do
|
16
|
+
before { @mock.should_not_receive :errors }
|
17
|
+
it { subject.validate_each @mock, 'date', '123.45' }
|
18
|
+
it { subject.validate_each @mock, 'date', '23.45' }
|
19
|
+
it { subject.validate_each @mock, 'date', '0.45' }
|
20
|
+
it { subject.validate_each @mock, 'date', '.45' }
|
21
|
+
it { subject.validate_each @mock, 'date', '.4' }
|
22
|
+
it { subject.validate_each @mock, 'date', '4' }
|
23
|
+
end
|
45
24
|
|
46
|
-
|
47
|
-
|
48
|
-
|
25
|
+
# Incorrect data yields errors!
|
26
|
+
context 'with invalid input should produce a validation error' do
|
27
|
+
before { @mock.errors[].should_receive :<< }
|
28
|
+
it { subject.validate_each @mock, 'date', '23.456' }
|
29
|
+
it { subject.validate_each @mock, 'date', 'two' }
|
30
|
+
it { subject.validate_each @mock, 'date', '23..456' }
|
31
|
+
it { subject.validate_each @mock, 'date', '23.45.6' }
|
32
|
+
it { subject.validate_each @mock, 'date', '.234' }
|
33
|
+
end
|
34
|
+
|
35
|
+
context 'in strict mode' do
|
36
|
+
before :each do
|
37
|
+
@validator = CurrencyFormatValidator.new attributes: {}, strict: true
|
38
|
+
end
|
39
|
+
subject { @validator }
|
40
|
+
context 'with perfectly valid, correctly-formatted input should produce no errors' do
|
41
|
+
before { @mock.should_not_receive :errors }
|
42
|
+
it { subject.validate_each @mock, 'date', '123.45' }
|
43
|
+
it { subject.validate_each @mock, 'date', '23.45' }
|
44
|
+
it { subject.validate_each @mock, 'date', '0.45' }
|
45
|
+
it { subject.validate_each @mock, 'date', '4' }
|
46
|
+
end
|
47
|
+
|
48
|
+
context 'with invalid input should produce a validation error' do
|
49
|
+
before { @mock.errors[].should_receive :<< }
|
50
|
+
it { subject.validate_each @mock, 'date', '.45' }
|
51
|
+
it { subject.validate_each @mock, 'date', '.4' }
|
52
|
+
it { subject.validate_each @mock, 'date', '0.4' }
|
53
|
+
it { subject.validate_each @mock, 'date', '23.456' }
|
54
|
+
it { subject.validate_each @mock, 'date', 'two' }
|
55
|
+
it { subject.validate_each @mock, 'date', '23..456' }
|
56
|
+
it { subject.validate_each @mock, 'date', '23.45.6' }
|
57
|
+
it { subject.validate_each @mock, 'date', '.234' }
|
49
58
|
end
|
50
59
|
end
|
60
|
+
|
51
61
|
end
|
@@ -0,0 +1,68 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe DevisePasswordValidator do
|
4
|
+
describe ".validate_each" do
|
5
|
+
before(:each) do
|
6
|
+
@options = {:attributes => {}}
|
7
|
+
@validator = DevisePasswordValidator.new(@options)
|
8
|
+
@record = BasicRecord.new(:password)
|
9
|
+
@record.stub!(:password)
|
10
|
+
@record.stub!(:password_confirmation)
|
11
|
+
@record.stub!(:id)
|
12
|
+
@record.stub!(:reset_password_token)
|
13
|
+
end
|
14
|
+
|
15
|
+
it "should validate a password of at least 6 characters, no spaces, and password is not being reset" do
|
16
|
+
@record.errors[:password].should_not_receive("<<")
|
17
|
+
@record.stub!(:password).and_return("goodpa")
|
18
|
+
@record.stub!(:password_confirmation).and_return("goodpa")
|
19
|
+
@validator.validate_each(@record, :password, "goodpa")
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should not validate if password is not at least 6 characters" do
|
23
|
+
@record.errors[:password].should_receive("<<")
|
24
|
+
@record.stub!(:password).and_return("badpa")
|
25
|
+
@record.stub!(:password_confirmation).and_return "badpa"
|
26
|
+
@validator.validate_each(@record, :password, "badpa")
|
27
|
+
end
|
28
|
+
|
29
|
+
it "should not validate if password has a space" do
|
30
|
+
@record.errors[:password].should_receive("<<")
|
31
|
+
@record.stub!(:password).and_return "bad pass"
|
32
|
+
@record.stub!(:password_confirmation).and_return "bad pass"
|
33
|
+
@validator.validate_each(@record, :password, "bad pass")
|
34
|
+
end
|
35
|
+
|
36
|
+
it "should not validate if password does not match password confirmation" do
|
37
|
+
@record.errors[:password].should_receive("<<")
|
38
|
+
@record.stub!(:password).and_return "goodpa"
|
39
|
+
@record.stub!(:password_confirmation).and_return "goodpa2"
|
40
|
+
@validator.validate_each(@record, :password, "goodpa")
|
41
|
+
end
|
42
|
+
|
43
|
+
it "should not validate if password is blank and the record has no id" do
|
44
|
+
@record.errors[:password].should_receive("<<")
|
45
|
+
@record.stub!(:password).and_return ""
|
46
|
+
@record.stub!(:password_confirmation).and_return ""
|
47
|
+
@validator.validate_each(@record, :password, "")
|
48
|
+
end
|
49
|
+
|
50
|
+
it "should validate if password is blank and the record has an id and the reset token is blank" do
|
51
|
+
@record.errors[:password].should_not_receive("<<")
|
52
|
+
@record.stub!(:password).and_return ""
|
53
|
+
@record.stub!(:id).and_return "1"
|
54
|
+
@record.stub!(:password_confirmation).and_return ""
|
55
|
+
@validator.validate_each(@record, :password, "")
|
56
|
+
end
|
57
|
+
|
58
|
+
it "should not validate if password is blank and the record has an id and the reset token is not blank" do
|
59
|
+
@record.errors[:password].should_receive("<<")
|
60
|
+
@record.stub!(:password).and_return ""
|
61
|
+
@record.stub!(:id).and_return "1"
|
62
|
+
@record.stub!(:reset_password_token).and_return "alhbasdhkhlb"
|
63
|
+
@record.stub!(:password_confirmation).and_return ""
|
64
|
+
@validator.validate_each(@record, :password, "")
|
65
|
+
end
|
66
|
+
|
67
|
+
end
|
68
|
+
end
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: format_validators
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.0.
|
5
|
+
version: 0.0.6
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Jeremiah Hemphill
|
@@ -165,6 +165,7 @@ files:
|
|
165
165
|
- VERSION
|
166
166
|
- app/validators/currency_format_validator.rb
|
167
167
|
- app/validators/days_of_week_validator.rb
|
168
|
+
- app/validators/devise_password_validator.rb
|
168
169
|
- app/validators/florida_counties_validator.rb
|
169
170
|
- app/validators/phone_format_validator.rb
|
170
171
|
- app/validators/ssn_format_validator.rb
|
@@ -214,6 +215,7 @@ files:
|
|
214
215
|
- spec/support/basic_record.rb
|
215
216
|
- spec/validators/currency_format_validator_spec.rb
|
216
217
|
- spec/validators/days_of_week_validator_spec.rb
|
218
|
+
- spec/validators/devise_password_validator_spec.rb
|
217
219
|
- spec/validators/florida_counties_integration_spec.rb
|
218
220
|
- spec/validators/florida_counties_spec.rb
|
219
221
|
- spec/validators/phone_format_validator_spec.rb
|
@@ -232,7 +234,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
232
234
|
requirements:
|
233
235
|
- - ">="
|
234
236
|
- !ruby/object:Gem::Version
|
235
|
-
hash:
|
237
|
+
hash: 26858958529201011
|
236
238
|
segments:
|
237
239
|
- 0
|
238
240
|
version: "0"
|