Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Specifiy invalid chars in username error message #2044

Merged
merged 15 commits into from Nov 5, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 1 addition & 2 deletions app/models/user.rb
Expand Up @@ -95,8 +95,7 @@ class User < ActiveRecord::Base
validates :display_name, :if => proc { |u| u.display_name_changed? },
:invalid_chars => true,
:invalid_url_chars => true,
:leading_whitespace => true,
:trailing_whitespace => true
:whitespace => { :leading => false, :trailing => false }
validates :email, :presence => true, :confirmation => true, :invalid_chars => true
validates :email, :if => proc { |u| u.email_changed? },
:uniqueness => { :case_sensitive => false }
Expand Down
5 changes: 0 additions & 5 deletions app/validators/leading_whitespace_validator.rb

This file was deleted.

5 changes: 0 additions & 5 deletions app/validators/trailing_whitespace_validator.rb

This file was deleted.

11 changes: 11 additions & 0 deletions app/validators/whitespace_validator.rb
@@ -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
33 changes: 0 additions & 33 deletions test/validators/leading_whitespace_validator_test.rb

This file was deleted.

33 changes: 0 additions & 33 deletions test/validators/trailing_whitespace_validator_test.rb

This file was deleted.

61 changes: 61 additions & 0 deletions test/validators/whitespace_validator_test.rb
@@ -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