activevalidators 1.2.1 → 1.2.2

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.
data/README.md CHANGED
@@ -20,6 +20,7 @@ In your models, the gem provides new validators like `email`, or `url`:
20
20
  validates :email_address, :email => true
21
21
  validates :link_url, :url => true
22
22
  validates :user_phone, :phone => true
23
+ validates :password, :password => { :strength => :medium }
23
24
  end
24
25
 
25
26
  class Article
@@ -49,8 +50,9 @@ Exhaustive list of supported validators and their implementation:
49
50
  * `phone` : based on a regular expression
50
51
  * `slug` : based on `ActiveSupport::String#parameterize`
51
52
  * `ip` : based on `Resolv::IPv[4|6]::Regex`
52
- * `credit_card` : based on the `Luhnacy` gem
53
+ * `credit_card` : based on the `Luhn` algorithm
53
54
  * `date` : based on the `DateValidator` gem
55
+ * `password` : based on a set of regular expressions
54
56
 
55
57
  Todo
56
58
  ----
@@ -72,6 +74,13 @@ Note on Patches/Pull Requests
72
74
  (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
73
75
  * Send me a pull request. Bonus points for topic branches.
74
76
 
77
+
78
+ Contributors
79
+ ------------
80
+ * Franck Verrot
81
+ * Oriol Gual
82
+ * Paco Guzmán
83
+
75
84
  Copyright
76
85
  ---------
77
86
 
@@ -4,9 +4,9 @@ $:.unshift lib unless $:.include?(lib)
4
4
 
5
5
  Gem::Specification.new do |s|
6
6
  s.name = "activevalidators"
7
- s.version = '1.2.1'
7
+ s.version = '1.2.2'
8
8
  s.platform = Gem::Platform::RUBY
9
- s.authors = ["Franck Verrot"]
9
+ s.authors = ["Franck Verrot", "Paco Guzmán", "Oriol Gual"]
10
10
  s.email = ["franck@verrot.fr"]
11
11
  s.homepage = "http://github.com/cesario/activevalidators"
12
12
  s.summary = %q{Collection of ActiveModel/ActiveRecord validations}
@@ -0,0 +1,18 @@
1
+ module ActiveModel
2
+ module Validations
3
+ class PasswordValidator < EachValidator
4
+ REGEXES = {
5
+ :weak => /(?=.{6,}).*/, # 6 characters
6
+ :medium => /^(?=.{7,})(((?=.*[A-Z])(?=.*[a-z]))|((?=.*[A-Z])(?=.*[0-9]))|((?=.*[a-z])(?=.*[0-9]))).*$/, #len=7 chars and numbers
7
+ :strong => /^.*(?=.{8,})(?=.*[a-z])(?=.*[A-Z])(?=.*[\d\W]).*$/#len=8 chars and numbers and special chars
8
+ }
9
+
10
+ def validate_each(record, attribute, value)
11
+ required_strength = options.fetch(:strength, :weak)
12
+ if (REGEXES[required_strength] !~ value)
13
+ record.errors.add(attribute)
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -15,5 +15,6 @@ module ActiveModel
15
15
  autoload :IpValidator
16
16
  autoload :CreditCardValidator
17
17
  autoload :DateValidator, 'date_validator'
18
+ autoload :PasswordValidator
18
19
  end
19
20
  end
@@ -9,5 +9,6 @@ end
9
9
 
10
10
  class TestRecord
11
11
  include ActiveModel::Validations
12
- attr_accessor :ip, :url, :slug, :responder, :global_condition, :local_condition, :phone, :email, :card
12
+ attr_accessor :ip, :url, :slug, :responder, :global_condition,
13
+ :local_condition, :phone, :email, :card, :password
13
14
  end
@@ -0,0 +1,44 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'spec_helper.rb')
2
+
3
+ describe "Password Validation" do
4
+ STRENGTHS = {
5
+ :weak => { :valid => 'sixchr', :invalid => 'foo' },
6
+ :medium => { :valid => 'chrs123', :invalid => 'sixchr' },
7
+ :strong => { :valid => 'HQSij2323#$%', :invalid => 'chrs123' }
8
+ }
9
+
10
+ subject { TestRecord.new }
11
+
12
+ STRENGTHS.each_pair do |strength, passwords|
13
+ describe "#{strength} mode" do
14
+ before(:each) do
15
+ TestRecord.reset_callbacks(:validate)
16
+ TestRecord.validates :password, :password => { :strength => strength }
17
+ end
18
+
19
+ describe "valid passwords" do
20
+ it "accepts a #{strength} password like #{passwords[:valid]}" do
21
+ subject.password = passwords[:valid]
22
+ subject.should be_valid
23
+ subject.should have(0).errors
24
+ end
25
+ end
26
+
27
+ describe "invalid passwords" do
28
+ before :each do
29
+ subject.password = passwords[:invalid]
30
+ end
31
+
32
+ it "rejects invalid passwords like #{passwords[:invalid]}" do
33
+ subject.should_not be_valid
34
+ subject.should have(1).error
35
+ end
36
+
37
+ it "generates an error message of type invalid" do
38
+ subject.should_not be_valid
39
+ subject.errors[:password].should include subject.errors.generate_message(:password, :invalid)
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
metadata CHANGED
@@ -5,16 +5,18 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 1
7
7
  - 2
8
- - 1
9
- version: 1.2.1
8
+ - 2
9
+ version: 1.2.2
10
10
  platform: ruby
11
11
  authors:
12
12
  - Franck Verrot
13
+ - "Paco Guzm\xC3\xA1n"
14
+ - Oriol Gual
13
15
  autorequire:
14
16
  bindir: bin
15
17
  cert_chain: []
16
18
 
17
- date: 2010-12-05 00:00:00 +01:00
19
+ date: 2010-12-06 00:00:00 +01:00
18
20
  default_executable:
19
21
  dependencies:
20
22
  - !ruby/object:Gem::Dependency
@@ -149,6 +151,7 @@ files:
149
151
  - lib/active_model/validations/credit_card_validator.rb
150
152
  - lib/active_model/validations/email_validator.rb
151
153
  - lib/active_model/validations/ip_validator.rb
154
+ - lib/active_model/validations/password_validator.rb
152
155
  - lib/active_model/validations/phone_validator.rb
153
156
  - lib/active_model/validations/respond_to_validator.rb
154
157
  - lib/active_model/validations/slug_validator.rb
@@ -158,6 +161,7 @@ files:
158
161
  - spec/validations/credit_card_spec.rb
159
162
  - spec/validations/email_spec.rb
160
163
  - spec/validations/ip_spec.rb
164
+ - spec/validations/password_spec.rb
161
165
  - spec/validations/phone_spec.rb
162
166
  - spec/validations/respond_to_spec.rb
163
167
  - spec/validations/slug_spec.rb
@@ -199,6 +203,7 @@ test_files:
199
203
  - spec/validations/credit_card_spec.rb
200
204
  - spec/validations/email_spec.rb
201
205
  - spec/validations/ip_spec.rb
206
+ - spec/validations/password_spec.rb
202
207
  - spec/validations/phone_spec.rb
203
208
  - spec/validations/respond_to_spec.rb
204
209
  - spec/validations/slug_spec.rb