Skip to content
Browse files

Valida formato do email

  • Loading branch information...
1 parent e268fc3 commit a7401f0818f50e42cc3babe2d4d31920e6760d57 @elomar committed Mar 9, 2012
Showing with 19 additions and 1 deletion.
  1. +3 −1 app/models/usuario.rb
  2. +16 −0 spec/models/usuario_spec.rb
View
4 app/models/usuario.rb
@@ -2,5 +2,7 @@ class Usuario < ActiveRecord::Base
attr_accessible :nome, :email
validates :nome, presence: true, length: { maximum: 50 }
- validates :email, presence: true
+
+ VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
+ validates :email, presence: true, format: VALID_EMAIL_REGEX
end
View
16 spec/models/usuario_spec.rb
@@ -27,4 +27,20 @@
it { should_not be_valid }
end
+ describe "quando email é inválido" do
+ emails_invalidos = %w[usuario@foo,com usuario_at_foo.org example.usuario@foo.]
+ emails_invalidos.each do |email_invalido|
+ before { @usuario.email = email_invalido }
+ it { should_not be_valid }
+ end
+ end
+
+ describe "quando email é válido" do
+ emails_validos = %w[usuario@foo.com A_usuario@f.b.org frst.lst@foo.jp a+b@baz.cn]
+ emails_validos.each do |email_valido|
+ before { @usuario.email = email_valido }
+ it { should be_valid }
+ end
+ end
+
end

0 comments on commit a7401f0

Please sign in to comment.
Something went wrong with that request. Please try again.