validatious 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -53,6 +53,18 @@ correct format of a URL.
53
53
  or
54
54
 
55
55
  validates_url_format_of :url
56
+
57
+
58
+ ### Email
59
+
60
+ This validates the format of an email address, ensuring that the attribute conforms
61
+ to the correct format of an email address.
62
+
63
+ validates :email, :email => true
64
+
65
+ or
66
+
67
+ validates_email_format_of :email
56
68
 
57
69
 
58
70
  ## CONTRIBUTE
@@ -6,6 +6,10 @@ module ActiveModel
6
6
  validates_with UrlValidator, _merge_attributes(attr_names)
7
7
  end
8
8
 
9
+ def validates_email_format_of(*attr_names)
10
+ validates_with EmailValidator, _merge_attributes(attr_names)
11
+ end
12
+
9
13
  end
10
14
  end
11
15
  end
@@ -1,4 +1,5 @@
1
1
  en:
2
2
  errors:
3
3
  messages:
4
- invalid_url: "is not a valid URL"
4
+ invalid_url: "is not a valid URL"
5
+ invalid_email: "is not a valid email address"
@@ -0,0 +1,19 @@
1
+ module Validatious
2
+ module Validators
3
+
4
+ # == Active Model Email Validator
5
+ class EmailValidator < ActiveModel::EachValidator
6
+ SPECIALCHARS = Regexp.escape('!#$%&\'*-/=?+-^_`{|}~')
7
+ PARTUNQUOTED = '(([[:alnum:]' + SPECIALCHARS + ']+[\.\+]+))*[[:alnum:]' + SPECIALCHARS + '+]+'
8
+ PARTQUOTED = '\"(([[:alnum:]' + SPECIALCHARS + '\.\+]*|(\\\\[\u0001-\uFFFF]))*)\"'
9
+ REGEX = Regexp.new('^((' + PARTUNQUOTED + ')|(' + PARTQUOTED + ')+)@(((\w+\-+)|(\w+\.))*\w{1,63}\.[a-z]{2,6}$)', Regexp::EXTENDED | Regexp::IGNORECASE)
10
+
11
+ def validate_each(record, attribute, value)
12
+ record.errors.add(attribute, :invalid_email, options) if value.to_s !~ REGEX
13
+ end
14
+ end
15
+ end
16
+ end
17
+
18
+ # Compatibility with ActiveModel validates method which matches option keys to their validator class
19
+ ActiveModel::Validations::EmailValidator = Validatious::Validators::EmailValidator
@@ -1,3 +1,3 @@
1
1
  module Validatious
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
data/lib/validatious.rb CHANGED
@@ -7,6 +7,7 @@ module Validatious
7
7
  end
8
8
 
9
9
  require "validatious/validators/url_validator"
10
+ require "validatious/validators/email_validator"
10
11
  require "validatious/helper_methods"
11
12
 
12
13
  require 'active_support/i18n'
@@ -0,0 +1,13 @@
1
+ class Topic
2
+ include ActiveModel::Validations
3
+ include ActiveModel::Validations::Callbacks
4
+
5
+ attr_accessor :title, :email
6
+
7
+ def initialize(attributes = {})
8
+ attributes.each do |key, value|
9
+ send "#{key}=", value
10
+ end
11
+ end
12
+
13
+ end
@@ -4,6 +4,7 @@ describe Validatious, 'HelperMethods' do
4
4
 
5
5
  it 'should define class validation methods' do
6
6
  Post.should respond_to(:validates_url_format_of)
7
+ Post.should respond_to(:validates_email_format_of)
7
8
  end
8
9
 
9
10
  end
@@ -0,0 +1,34 @@
1
+ # encoding: utf-8
2
+
3
+ require "spec_helper"
4
+
5
+ describe Validatious::Validators::EmailValidator do
6
+
7
+ before(:each) do
8
+ Topic.validates_email_format_of(:email)
9
+ end
10
+
11
+ let(:topic) { post = Topic.new(:title => "The title", :email => "invalid email") }
12
+
13
+ [
14
+ 'bob@bones.com',
15
+ 'bob.bones@bones.com'
16
+ ].each do |email|
17
+ describe email.inspect do
18
+ it "should be valid" do
19
+ topic.email = email
20
+ topic.should be_valid
21
+ end
22
+ end
23
+ end
24
+
25
+ [nil, 1, "", " ", "email"].each do |email|
26
+ describe email.inspect do
27
+ it "should not be valid" do
28
+ topic.email = email
29
+ topic.should_not be_valid
30
+ end
31
+ end
32
+ end
33
+
34
+ end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: validatious
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.1.0
5
+ version: 0.2.0
6
6
  platform: ruby
7
7
  authors:
8
8
  - Joel Moss
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-06-28 00:00:00 +01:00
13
+ date: 2011-06-29 00:00:00 +01:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
@@ -54,13 +54,16 @@ files:
54
54
  - lib/validatious.rb
55
55
  - lib/validatious/helper_methods.rb
56
56
  - lib/validatious/locale/en.yml
57
+ - lib/validatious/validators/email_validator.rb
57
58
  - lib/validatious/validators/url_validator.rb
58
59
  - lib/validatious/version.rb
59
60
  - spec/spec_helper.rb
60
61
  - spec/support/deferred_garbage_collection.rb
61
62
  - spec/support/models/post.rb
63
+ - spec/support/models/topic.rb
62
64
  - spec/validatious/helper_methods_spec.rb
63
- - spec/validatious/validators/url_spec.rb
65
+ - spec/validatious/validators/email_validator_spec.rb
66
+ - spec/validatious/validators/url_validator_spec.rb
64
67
  - validatious.gemspec
65
68
  has_rdoc: true
66
69
  homepage: https://github.com/joelmoss/validatious
@@ -94,5 +97,7 @@ test_files:
94
97
  - spec/spec_helper.rb
95
98
  - spec/support/deferred_garbage_collection.rb
96
99
  - spec/support/models/post.rb
100
+ - spec/support/models/topic.rb
97
101
  - spec/validatious/helper_methods_spec.rb
98
- - spec/validatious/validators/url_spec.rb
102
+ - spec/validatious/validators/email_validator_spec.rb
103
+ - spec/validatious/validators/url_validator_spec.rb