From 735cb9855f01eb3cb10ae32b231159aec09e7946 Mon Sep 17 00:00:00 2001 From: Tsvetomila Mihaylova Date: Tue, 6 Dec 2011 09:36:15 +0200 Subject: [PATCH 1/2] Validators should not validate empty or nil values. --- lib/validators/email_format_validator.rb | 2 +- lib/validators/name_format_validator.rb | 2 +- lib/validators/url_format_validator.rb | 2 +- .../validators/email_format_validator_spec.rb | 12 +++++++ spec/validators/name_format_validator_spec.rb | 32 ++++++++++++++++++- spec/validators/url_format_validator_spec.rb | 12 +++++++ 6 files changed, 58 insertions(+), 4 deletions(-) diff --git a/lib/validators/email_format_validator.rb b/lib/validators/email_format_validator.rb index 3340d3a..9d82eff 100644 --- a/lib/validators/email_format_validator.rb +++ b/lib/validators/email_format_validator.rb @@ -1,6 +1,6 @@ class EmailFormatValidator < ActiveModel::EachValidator def validate_each(record, attribute, value) - record.errors[attribute] << (options[:message] || 'is not a valid email') unless EmailFormatValidator.valid_email?(value) + record.errors[attribute] << (options[:message] || 'is not a valid email') unless value.blank? || EmailFormatValidator.valid_email?(value) end def self.valid_email?(value) diff --git a/lib/validators/name_format_validator.rb b/lib/validators/name_format_validator.rb index e55b2b3..3973a6d 100644 --- a/lib/validators/name_format_validator.rb +++ b/lib/validators/name_format_validator.rb @@ -1,6 +1,6 @@ class NameFormatValidator < ActiveModel::EachValidator def validate_each(record, attribute, value) - record.errors[attribute] << (options[:message] || 'is not a valid name') unless NameFormatValidator.valid_name?(value) + record.errors[attribute] << (options[:message] || 'is not a valid name') unless value.blank? || NameFormatValidator.valid_name?(value) end def self.valid_name?(value) diff --git a/lib/validators/url_format_validator.rb b/lib/validators/url_format_validator.rb index 9fe865e..b630697 100644 --- a/lib/validators/url_format_validator.rb +++ b/lib/validators/url_format_validator.rb @@ -1,6 +1,6 @@ class UrlFormatValidator < ActiveModel::EachValidator def validate_each(record, attribute, value) - record.errors[attribute] << (options[:message] || 'is not a valid url') unless UrlFormatValidator.valid_url?(value) + record.errors[attribute] << (options[:message] || 'is not a valid url') unless value.blank? || UrlFormatValidator.valid_url?(value) end def self.valid_url?(value) diff --git a/spec/validators/email_format_validator_spec.rb b/spec/validators/email_format_validator_spec.rb index 9c71aa9..1a92113 100644 --- a/spec/validators/email_format_validator_spec.rb +++ b/spec/validators/email_format_validator_spec.rb @@ -22,4 +22,16 @@ class UserWithEmail < ActiveRecord::Base user.should be_valid end + + it 'does not validate empty values' do + user.email = '' + + user.should be_valid + end + + it 'does not validate nil values' do + user.email = nil + + user.should be_valid + end end diff --git a/spec/validators/name_format_validator_spec.rb b/spec/validators/name_format_validator_spec.rb index 60f11e8..caa6cd0 100644 --- a/spec/validators/name_format_validator_spec.rb +++ b/spec/validators/name_format_validator_spec.rb @@ -17,9 +17,39 @@ class UserWithName < ActiveRecord::Base user.should_not be_valid end - it 'accepts valid name format' do + it 'accepts names consisting of one word' do + user.name = 'bill' + + user.should be_valid + end + + it 'accepts more than one words separated with space' do user.name = 'Homer Simpson' user.should be_valid end + + it 'accepts more than one words separated with dash' do + user.name = 'Mary-Jane' + + user.should be_valid + end + + it 'does not accept names not starting with a letter' do + user.name = '-Bill' + + user.should_not be_valid + end + + it 'does not validate empty values' do + user.name = '' + + user.should be_valid + end + + it 'does not validate nil values' do + user.name = nil + + user.should be_valid + end end diff --git a/spec/validators/url_format_validator_spec.rb b/spec/validators/url_format_validator_spec.rb index a050dfb..8abac24 100644 --- a/spec/validators/url_format_validator_spec.rb +++ b/spec/validators/url_format_validator_spec.rb @@ -26,4 +26,16 @@ class UserWithUrl < ActiveRecord::Base user.should be_valid end + + it 'does not validate empty values' do + user.url = '' + + user.should be_valid + end + + it 'does not validate nil values' do + user.url = nil + + user.should be_valid + end end From fc4467d0db6e12b00d2532d980cc1ca7b6c0aa2d Mon Sep 17 00:00:00 2001 From: Tsvetomila Mihaylova Date: Tue, 6 Dec 2011 11:06:02 +0200 Subject: [PATCH 2/2] Allowed dot and apostrophe characters for name format. --- lib/validators/name_format_validator.rb | 2 +- spec/validators/name_format_validator_spec.rb | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/validators/name_format_validator.rb b/lib/validators/name_format_validator.rb index 3973a6d..5330d7b 100644 --- a/lib/validators/name_format_validator.rb +++ b/lib/validators/name_format_validator.rb @@ -4,7 +4,7 @@ def validate_each(record, attribute, value) end def self.valid_name?(value) - value =~ /^[\p{Alpha}][\p{Alpha}\s-]*$/i + value =~ /^[\p{Alpha}][\p{Alpha}\.\'\s-]*$/i end end diff --git a/spec/validators/name_format_validator_spec.rb b/spec/validators/name_format_validator_spec.rb index caa6cd0..4251764 100644 --- a/spec/validators/name_format_validator_spec.rb +++ b/spec/validators/name_format_validator_spec.rb @@ -52,4 +52,16 @@ class UserWithName < ActiveRecord::Base user.should be_valid end + + it 'accepts apostrophes in the names' do + user.name = "O'Connell" + + user.should be_valid + end + + it 'accepts dots in the names' do + user.name = 'John Jr.' + + user.should be_valid + end end