Merge leading and trailing whitespace validators
This commit is contained in:
parent
873ac155ca
commit
b4ef61a9f3
7 changed files with 73 additions and 78 deletions
|
@ -95,8 +95,7 @@ class User < ActiveRecord::Base
|
||||||
validates :display_name, :if => proc { |u| u.display_name_changed? },
|
validates :display_name, :if => proc { |u| u.display_name_changed? },
|
||||||
:invalid_chars => true,
|
:invalid_chars => true,
|
||||||
:invalid_url_chars => true,
|
:invalid_url_chars => true,
|
||||||
:leading_whitespace => true,
|
:whitespace => { :leading => false, :trailing => false }
|
||||||
:trailing_whitespace => true
|
|
||||||
validates :email, :presence => true, :confirmation => true, :invalid_chars => true
|
validates :email, :presence => true, :confirmation => true, :invalid_chars => true
|
||||||
validates :email, :if => proc { |u| u.email_changed? },
|
validates :email, :if => proc { |u| u.email_changed? },
|
||||||
:uniqueness => { :case_sensitive => false }
|
:uniqueness => { :case_sensitive => false }
|
||||||
|
|
|
@ -1,5 +0,0 @@
|
||||||
class LeadingWhitespaceValidator < ActiveModel::EachValidator
|
|
||||||
def validate_each(record, attribute, value)
|
|
||||||
record.errors[attribute] << (options[:message] || I18n.t("validations.leading_whitespace")) if value =~ /\A\s/
|
|
||||||
end
|
|
||||||
end
|
|
|
@ -1,5 +0,0 @@
|
||||||
class TrailingWhitespaceValidator < ActiveModel::EachValidator
|
|
||||||
def validate_each(record, attribute, value)
|
|
||||||
record.errors[attribute] << (options[:message] || I18n.t("validations.trailing_whitespace")) if value =~ /\s\z/
|
|
||||||
end
|
|
||||||
end
|
|
11
app/validators/whitespace_validator.rb
Normal file
11
app/validators/whitespace_validator.rb
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
class WhitespaceValidator < ActiveModel::EachValidator
|
||||||
|
def validate_each(record, attribute, value)
|
||||||
|
unless options.fetch(:leading, true)
|
||||||
|
record.errors[attribute] << (options[:message] || I18n.t("validations.leading_whitespace")) if value =~ /\A\s/
|
||||||
|
end
|
||||||
|
|
||||||
|
unless options.fetch(:trailing, true)
|
||||||
|
record.errors[attribute] << (options[:message] || I18n.t("validations.trailing_whitespace")) if value =~ /\s\z/
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
|
@ -1,33 +0,0 @@
|
||||||
require "test_helper"
|
|
||||||
|
|
||||||
class LeadingWhitespaceValidatable
|
|
||||||
include ActiveModel::Validations
|
|
||||||
validates :string, :leading_whitespace => true
|
|
||||||
attr_accessor :string
|
|
||||||
end
|
|
||||||
|
|
||||||
class LeadingWhitespaceValidatorTest < ActiveSupport::TestCase
|
|
||||||
include Rails::Dom::Testing::Assertions::SelectorAssertions
|
|
||||||
|
|
||||||
def test_with_leading_whitespace
|
|
||||||
validator = LeadingWhitespaceValidatable.new
|
|
||||||
|
|
||||||
strings = [" ", " test", " ", "\ttest"]
|
|
||||||
|
|
||||||
strings.each do |v|
|
|
||||||
validator.string = v
|
|
||||||
assert_not validator.valid?, "'#{v}' should not be valid"
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_without_leading_whitespace
|
|
||||||
validator = LeadingWhitespaceValidatable.new
|
|
||||||
|
|
||||||
strings = ["test", "test ", "t est", "test\t", ".test", "_test"]
|
|
||||||
|
|
||||||
strings.each do |v|
|
|
||||||
validator.string = v
|
|
||||||
assert validator.valid?, "'#{v}' should be valid"
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
|
@ -1,33 +0,0 @@
|
||||||
require "test_helper"
|
|
||||||
|
|
||||||
class TrailingWhitespaceValidatable
|
|
||||||
include ActiveModel::Validations
|
|
||||||
validates :string, :trailing_whitespace => true
|
|
||||||
attr_accessor :string
|
|
||||||
end
|
|
||||||
|
|
||||||
class TrailingWhitespaceValidatorTest < ActiveSupport::TestCase
|
|
||||||
include Rails::Dom::Testing::Assertions::SelectorAssertions
|
|
||||||
|
|
||||||
def test_with_trailing_whitespace
|
|
||||||
validator = TrailingWhitespaceValidatable.new
|
|
||||||
|
|
||||||
strings = [" ", "test ", " ", "test\t", "_test_ "]
|
|
||||||
|
|
||||||
strings.each do |v|
|
|
||||||
validator.string = v
|
|
||||||
assert_not validator.valid?, "'#{v}' should not be valid"
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_without_trailing_whitespace
|
|
||||||
validator = TrailingWhitespaceValidatable.new
|
|
||||||
|
|
||||||
strings = ["test", " test", "tes t", "\ttest", "test.", "test_"]
|
|
||||||
|
|
||||||
strings.each do |v|
|
|
||||||
validator.string = v
|
|
||||||
assert validator.valid?, "'#{v}' should be valid"
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
61
test/validators/whitespace_validator_test.rb
Normal file
61
test/validators/whitespace_validator_test.rb
Normal file
|
@ -0,0 +1,61 @@
|
||||||
|
require "test_helper"
|
||||||
|
|
||||||
|
class LeadingWhitespaceValidatable
|
||||||
|
include ActiveModel::Validations
|
||||||
|
validates :string, :whitespace => { :leading => false }
|
||||||
|
attr_accessor :string
|
||||||
|
end
|
||||||
|
|
||||||
|
class TrailingWhitespaceValidatable
|
||||||
|
include ActiveModel::Validations
|
||||||
|
validates :string, :whitespace => { :trailing => false }
|
||||||
|
attr_accessor :string
|
||||||
|
end
|
||||||
|
|
||||||
|
class WhitespaceValidatorTest < ActiveSupport::TestCase
|
||||||
|
include Rails::Dom::Testing::Assertions::SelectorAssertions
|
||||||
|
|
||||||
|
def test_with_leading_whitespace
|
||||||
|
validator = LeadingWhitespaceValidatable.new
|
||||||
|
|
||||||
|
strings = [" ", " test", " ", "\ttest"]
|
||||||
|
|
||||||
|
strings.each do |v|
|
||||||
|
validator.string = v
|
||||||
|
assert_not validator.valid?, "'#{v}' should not be valid"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_without_leading_whitespace
|
||||||
|
validator = LeadingWhitespaceValidatable.new
|
||||||
|
|
||||||
|
strings = ["test", "test ", "t est", "test\t", ".test", "_test"]
|
||||||
|
|
||||||
|
strings.each do |v|
|
||||||
|
validator.string = v
|
||||||
|
assert validator.valid?, "'#{v}' should be valid"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_with_trailing_whitespace
|
||||||
|
validator = TrailingWhitespaceValidatable.new
|
||||||
|
|
||||||
|
strings = [" ", "test ", " ", "test\t", "_test_ "]
|
||||||
|
|
||||||
|
strings.each do |v|
|
||||||
|
validator.string = v
|
||||||
|
assert_not validator.valid?, "'#{v}' should not be valid"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_without_trailing_whitespace
|
||||||
|
validator = TrailingWhitespaceValidatable.new
|
||||||
|
|
||||||
|
strings = ["test", " test", "tes t", "\ttest", "test.", "test_"]
|
||||||
|
|
||||||
|
strings.each do |v|
|
||||||
|
validator.string = v
|
||||||
|
assert validator.valid?, "'#{v}' should be valid"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
Loading…
Add table
Add a link
Reference in a new issue