From: Tom Hughes Date: Mon, 5 Nov 2018 18:54:19 +0000 (+0000) Subject: Merge character validators X-Git-Tag: live~2783^2~2 X-Git-Url: https://git.openstreetmap.org/rails.git/commitdiff_plain/d73a5d4bc015d304e7b8863a0e927ac0b134e07b Merge character validators --- diff --git a/app/models/changeset_comment.rb b/app/models/changeset_comment.rb index 0b84409db..a0ad6f2ea 100644 --- a/app/models/changeset_comment.rb +++ b/app/models/changeset_comment.rb @@ -28,7 +28,7 @@ class ChangesetComment < ActiveRecord::Base validates :changeset, :presence => true, :associated => true validates :author, :presence => true, :associated => true validates :visible, :inclusion => [true, false] - validates :body, :invalid_chars => true + validates :body, :characters => true # Return the comment text def body diff --git a/app/models/changeset_tag.rb b/app/models/changeset_tag.rb index 7cfea1e04..942fafb2a 100644 --- a/app/models/changeset_tag.rb +++ b/app/models/changeset_tag.rb @@ -21,6 +21,6 @@ class ChangesetTag < ActiveRecord::Base belongs_to :changeset validates :changeset, :presence => true, :associated => true - validates :k, :v, :allow_blank => true, :length => { :maximum => 255 }, :invalid_chars => true + validates :k, :v, :allow_blank => true, :length => { :maximum => 255 }, :characters => true validates :k, :uniqueness => { :scope => :changeset_id } end diff --git a/app/models/diary_comment.rb b/app/models/diary_comment.rb index 475872b26..ade7a64ea 100644 --- a/app/models/diary_comment.rb +++ b/app/models/diary_comment.rb @@ -28,7 +28,7 @@ class DiaryComment < ActiveRecord::Base scope :visible, -> { where(:visible => true) } - validates :body, :presence => true, :invalid_chars => true + validates :body, :presence => true, :characters => true validates :diary_entry, :user, :associated => true after_save :spam_check diff --git a/app/models/diary_entry.rb b/app/models/diary_entry.rb index c49c2d427..d61241993 100644 --- a/app/models/diary_entry.rb +++ b/app/models/diary_entry.rb @@ -37,8 +37,8 @@ class DiaryEntry < ActiveRecord::Base scope :visible, -> { where(:visible => true) } - validates :title, :presence => true, :length => 1..255, :invalid_chars => true - validates :body, :presence => true, :invalid_chars => true + validates :title, :presence => true, :length => 1..255, :characters => true + validates :body, :presence => true, :characters => true validates :latitude, :allow_nil => true, :numericality => { :greater_than_or_equal_to => -90, :less_than_or_equal_to => 90 } diff --git a/app/models/issue_comment.rb b/app/models/issue_comment.rb index 2fe96f472..0841295e1 100644 --- a/app/models/issue_comment.rb +++ b/app/models/issue_comment.rb @@ -24,7 +24,7 @@ class IssueComment < ActiveRecord::Base belongs_to :issue belongs_to :user - validates :body, :presence => true, :invalid_chars => true + validates :body, :presence => true, :characters => true validates :user, :presence => true validates :issue, :presence => true end diff --git a/app/models/message.rb b/app/models/message.rb index 889137de0..4e89cb2e1 100644 --- a/app/models/message.rb +++ b/app/models/message.rb @@ -32,7 +32,7 @@ class Message < ActiveRecord::Base validates :title, :presence => true, :utf8 => true, :length => 1..255 validates :body, :sent_on, :sender, :recipient, :presence => true - validates :title, :body, :invalid_chars => true + validates :title, :body, :characters => true def self.from_mail(mail, from, to) if mail.multipart? diff --git a/app/models/node_tag.rb b/app/models/node_tag.rb index cdc11300e..43915bc12 100644 --- a/app/models/node_tag.rb +++ b/app/models/node_tag.rb @@ -18,6 +18,6 @@ class NodeTag < ActiveRecord::Base belongs_to :node validates :node, :presence => true, :associated => true - validates :k, :v, :allow_blank => true, :length => { :maximum => 255 }, :invalid_chars => true + validates :k, :v, :allow_blank => true, :length => { :maximum => 255 }, :characters => true validates :k, :uniqueness => { :scope => :node_id } end diff --git a/app/models/note_comment.rb b/app/models/note_comment.rb index 2347d43d0..f94032e1e 100644 --- a/app/models/note_comment.rb +++ b/app/models/note_comment.rb @@ -33,7 +33,7 @@ class NoteComment < ActiveRecord::Base validates :visible, :inclusion => [true, false] validates :author, :associated => true validates :event, :inclusion => %w[opened closed reopened commented hidden] - validates :body, :length => { :maximum => 2000 }, :invalid_chars => true + validates :body, :length => { :maximum => 2000 }, :characters => true # Return the comment text def body diff --git a/app/models/old_node_tag.rb b/app/models/old_node_tag.rb index 365841dca..77b78751b 100644 --- a/app/models/old_node_tag.rb +++ b/app/models/old_node_tag.rb @@ -19,6 +19,6 @@ class OldNodeTag < ActiveRecord::Base belongs_to :old_node, :foreign_key => [:node_id, :version] validates :old_node, :presence => true, :associated => true - validates :k, :v, :allow_blank => true, :length => { :maximum => 255 }, :invalid_chars => true + validates :k, :v, :allow_blank => true, :length => { :maximum => 255 }, :characters => true validates :k, :uniqueness => { :scope => [:node_id, :version] } end diff --git a/app/models/old_relation_tag.rb b/app/models/old_relation_tag.rb index 801ca7cab..4a247949c 100644 --- a/app/models/old_relation_tag.rb +++ b/app/models/old_relation_tag.rb @@ -19,6 +19,6 @@ class OldRelationTag < ActiveRecord::Base belongs_to :old_relation, :foreign_key => [:relation_id, :version] validates :old_relation, :presence => true, :associated => true - validates :k, :v, :allow_blank => true, :length => { :maximum => 255 }, :invalid_chars => true + validates :k, :v, :allow_blank => true, :length => { :maximum => 255 }, :characters => true validates :k, :uniqueness => { :scope => [:relation_id, :version] } end diff --git a/app/models/old_way_tag.rb b/app/models/old_way_tag.rb index 3662b1b7c..5832f6d4f 100644 --- a/app/models/old_way_tag.rb +++ b/app/models/old_way_tag.rb @@ -19,6 +19,6 @@ class OldWayTag < ActiveRecord::Base belongs_to :old_way, :foreign_key => [:way_id, :version] validates :old_way, :presence => true, :associated => true - validates :k, :v, :allow_blank => true, :length => { :maximum => 255 }, :invalid_chars => true + validates :k, :v, :allow_blank => true, :length => { :maximum => 255 }, :characters => true validates :k, :uniqueness => { :scope => [:way_id, :version] } end diff --git a/app/models/redaction.rb b/app/models/redaction.rb index e99460c3b..e6d748706 100644 --- a/app/models/redaction.rb +++ b/app/models/redaction.rb @@ -31,8 +31,8 @@ class Redaction < ActiveRecord::Base has_many :old_ways has_many :old_relations - validates :title, :presence => true, :invalid_chars => true - validates :description, :presence => true, :invalid_chars => true + validates :title, :presence => true, :characters => true + validates :description, :presence => true, :characters => true validates :description_format, :inclusion => { :in => %w[text html markdown] } # this method overrides the AR default to provide the rich diff --git a/app/models/relation_tag.rb b/app/models/relation_tag.rb index fc0ecdddf..151615f72 100644 --- a/app/models/relation_tag.rb +++ b/app/models/relation_tag.rb @@ -18,6 +18,6 @@ class RelationTag < ActiveRecord::Base belongs_to :relation validates :relation, :presence => true, :associated => true - validates :k, :v, :allow_blank => true, :length => { :maximum => 255 }, :invalid_chars => true + validates :k, :v, :allow_blank => true, :length => { :maximum => 255 }, :characters => true validates :k, :uniqueness => { :scope => :relation_id } end diff --git a/app/models/report.rb b/app/models/report.rb index ac00f1cfb..9bbf221df 100644 --- a/app/models/report.rb +++ b/app/models/report.rb @@ -27,7 +27,7 @@ class Report < ActiveRecord::Base validates :issue, :presence => true validates :user, :presence => true - validates :details, :presence => true, :invalid_chars => true + validates :details, :presence => true, :characters => true validates :category, :presence => true def self.categories_for(reportable) diff --git a/app/models/trace.rb b/app/models/trace.rb index 942d160f5..1d0b11d43 100644 --- a/app/models/trace.rb +++ b/app/models/trace.rb @@ -38,8 +38,8 @@ class Trace < ActiveRecord::Base scope :tagged, ->(t) { joins(:tags).where(:gpx_file_tags => { :tag => t }) } validates :user, :presence => true, :associated => true - validates :name, :presence => true, :length => 1..255, :invalid_chars => true - validates :description, :presence => { :on => :create }, :length => 1..255, :invalid_chars => true + validates :name, :presence => true, :length => 1..255, :characters => true + validates :description, :presence => { :on => :create }, :length => 1..255, :characters => true validates :timestamp, :presence => true validates :visibility, :inclusion => %w[private public trackable identifiable] diff --git a/app/models/tracetag.rb b/app/models/tracetag.rb index 0a59ba29a..75fcb48b7 100644 --- a/app/models/tracetag.rb +++ b/app/models/tracetag.rb @@ -22,5 +22,5 @@ class Tracetag < ActiveRecord::Base belongs_to :trace, :foreign_key => "gpx_id" validates :trace, :associated => true - validates :tag, :length => 1..255, :format => %r{\A[^/;.,?]*\z}, :invalid_chars => true + validates :tag, :length => 1..255, :format => %r{\A[^/;.,?]*\z}, :characters => true end diff --git a/app/models/user.rb b/app/models/user.rb index a57c88379..27ed7b648 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -93,10 +93,9 @@ class User < ActiveRecord::Base validates :display_name, :if => proc { |u| u.display_name_changed? }, :uniqueness => { :case_sensitive => false } validates :display_name, :if => proc { |u| u.display_name_changed? }, - :invalid_chars => true, - :invalid_url_chars => true, + :characters => { :url_safe => true }, :whitespace => { :leading => false, :trailing => false } - validates :email, :presence => true, :confirmation => true, :invalid_chars => true + validates :email, :presence => true, :confirmation => true, :characters => true validates :email, :if => proc { |u| u.email_changed? }, :uniqueness => { :case_sensitive => false } validates :pass_crypt, :confirmation => true, :length => 8..255 diff --git a/app/models/user_block.rb b/app/models/user_block.rb index 5f313c640..02af385a7 100644 --- a/app/models/user_block.rb +++ b/app/models/user_block.rb @@ -26,7 +26,7 @@ class UserBlock < ActiveRecord::Base validate :moderator_permissions - validates :reason, :invalid_chars => true + validates :reason, :characters => true belongs_to :user, :class_name => "User", :foreign_key => :user_id belongs_to :creator, :class_name => "User", :foreign_key => :creator_id diff --git a/app/models/user_preference.rb b/app/models/user_preference.rb index 639bbc07a..3963bd02a 100644 --- a/app/models/user_preference.rb +++ b/app/models/user_preference.rb @@ -17,7 +17,7 @@ class UserPreference < ActiveRecord::Base belongs_to :user validates :user, :presence => true, :associated => true - validates :k, :v, :length => 1..255, :invalid_chars => true + validates :k, :v, :length => 1..255, :characters => true # Turn this Node in to an XML Node without the wrapper. def to_xml_node diff --git a/app/models/way_tag.rb b/app/models/way_tag.rb index 7a5d9e72c..c4df0abb5 100644 --- a/app/models/way_tag.rb +++ b/app/models/way_tag.rb @@ -18,6 +18,6 @@ class WayTag < ActiveRecord::Base belongs_to :way validates :way, :presence => true, :associated => true - validates :k, :v, :allow_blank => true, :length => { :maximum => 255 }, :invalid_chars => true + validates :k, :v, :allow_blank => true, :length => { :maximum => 255 }, :characters => true validates :k, :uniqueness => { :scope => :way_id } end diff --git a/app/validators/characters_validator.rb b/app/validators/characters_validator.rb new file mode 100644 index 000000000..cbcd03a16 --- /dev/null +++ b/app/validators/characters_validator.rb @@ -0,0 +1,12 @@ +class CharactersValidator < ActiveModel::EachValidator + INVALID_CHARS = "\x00-\x08\x0b-\x0c\x0e-\x1f\x7f\ufffe\uffff".freeze + INVALID_URL_CHARS = "/;.,?%#".freeze + + def validate_each(record, attribute, value) + record.errors[attribute] << (options[:message] || I18n.t("validations.invalid_chars")) if value =~ /[#{INVALID_CHARS}]/ + + if options[:url_safe] + record.errors[attribute] << (options[:message] || I18n.t("validations.invalid_url_chars", :invalid_url_chars => INVALID_URL_CHARS)) if value =~ /[#{INVALID_URL_CHARS}]/ + end + end +end diff --git a/app/validators/invalid_chars_validator.rb b/app/validators/invalid_chars_validator.rb deleted file mode 100644 index f21f5a7cd..000000000 --- a/app/validators/invalid_chars_validator.rb +++ /dev/null @@ -1,7 +0,0 @@ -class InvalidCharsValidator < ActiveModel::EachValidator - INVALID_CHARS = "\x00-\x08\x0b-\x0c\x0e-\x1f\x7f\ufffe\uffff".freeze - - def validate_each(record, attribute, value) - record.errors[attribute] << (options[:message] || I18n.t("validations.invalid_chars")) if value =~ /[#{INVALID_CHARS}]/ - end -end diff --git a/app/validators/invalid_url_chars_validator.rb b/app/validators/invalid_url_chars_validator.rb deleted file mode 100644 index a93f6c6d9..000000000 --- a/app/validators/invalid_url_chars_validator.rb +++ /dev/null @@ -1,7 +0,0 @@ -class InvalidUrlCharsValidator < ActiveModel::EachValidator - INVALID_URL_CHARS = "/;.,?%#".freeze - - def validate_each(record, attribute, value) - record.errors[attribute] << (options[:message] || I18n.t("validations.invalid_url_chars", :invalid_url_chars => INVALID_URL_CHARS)) if value =~ /[#{INVALID_URL_CHARS}]/ - end -end diff --git a/test/validators/characters_validator_test.rb b/test/validators/characters_validator_test.rb new file mode 100644 index 000000000..e1409a5dc --- /dev/null +++ b/test/validators/characters_validator_test.rb @@ -0,0 +1,66 @@ +require "test_helper" + +class InvalidCharsValidatable + include ActiveModel::Validations + validates :chars, :characters => true + attr_accessor :chars +end + +class InvalidUrlCharsValidatable + include ActiveModel::Validations + validates :chars, :characters => { :url_safe => true } + attr_accessor :chars +end + +class CharactersValidatorTest < ActiveSupport::TestCase + include Rails::Dom::Testing::Assertions::SelectorAssertions + + def test_with_valid_chars + c = InvalidCharsValidatable.new + + valid = ["Name.", "'me", "he\"", "
", "*ho", "\"help\"@", + "vergrößern", "ルシステムにも対応します", "輕觸搖晃的遊戲", "/;.,?%#"] + + valid.each do |v| + c.chars = v + assert c.valid?, "'#{v}' should be valid" + end + end + + def test_with_invalid_chars + c = InvalidCharsValidatable.new + + invalid = ["\x7f
", "test@example.com\x0e-", "s/\x1ff", "aa/\ufffe", + "aa\x0b-,", "aa?\x08", "/;\uffff.,?", "\x00-も対応します/", "\x0c#ping", + "foo\x1fbar", "foo\x7fbar", "foo\ufffebar", "foo\uffffbar"] + + invalid.each do |v| + c.chars = v + assert_not c.valid?, "'#{v}' should not be valid" + end + end + + def test_with_valid_url_chars + c = InvalidUrlCharsValidatable.new + + valid = ["Name", "'me", "he\"", "
", "*ho", "\"help\"@", + "vergrößern", "ルシステムにも対応します", "輕觸搖晃的遊戲"] + + valid.each do |v| + c.chars = v + assert c.valid?, "'#{v}' should be valid" + end + end + + def test_with_invalid_url_chars + c = InvalidUrlCharsValidatable.new + + invalid = ["Name.", "you;me", "he\"#", "
", "50%", "good?", + "vergrößern,deutsche", "ルシステムに;.も対応します", "輕觸搖/晃的遊戲", "/;.,?%#"] + + invalid.each do |v| + c.chars = v + assert_not c.valid?, "'#{v}' should not be valid" + end + end +end diff --git a/test/validators/invalid_chars_validator_test.rb b/test/validators/invalid_chars_validator_test.rb deleted file mode 100644 index 1e46269a0..000000000 --- a/test/validators/invalid_chars_validator_test.rb +++ /dev/null @@ -1,36 +0,0 @@ -require "test_helper" - -class InvalidCharsValidatable - include ActiveModel::Validations - validates :chars, :invalid_chars => true - attr_accessor :chars -end - -class InvalidCharsValidatorTest < ActiveSupport::TestCase - include Rails::Dom::Testing::Assertions::SelectorAssertions - - def test_with_valid_chars - c = InvalidCharsValidatable.new - - valid = ["Name.", "'me", "he\"", "
", "*ho", "\"help\"@", - "vergrößern", "ルシステムにも対応します", "輕觸搖晃的遊戲", "/;.,?%#"] - - valid.each do |v| - c.chars = v - assert c.valid?, "'#{v}' should be valid" - end - end - - def test_with_invalid_chars - c = InvalidCharsValidatable.new - - invalid = ["\x7f
", "test@example.com\x0e-", "s/\x1ff", "aa/\ufffe", - "aa\x0b-,", "aa?\x08", "/;\uffff.,?", "\x00-も対応します/", "\x0c#ping", - "foo\x1fbar", "foo\x7fbar", "foo\ufffebar", "foo\uffffbar"] - - invalid.each do |v| - c.chars = v - assert_not c.valid?, "'#{v}' should not be valid" - end - end -end diff --git a/test/validators/invalid_url_chars_validator_test.rb b/test/validators/invalid_url_chars_validator_test.rb deleted file mode 100644 index ac35dcdee..000000000 --- a/test/validators/invalid_url_chars_validator_test.rb +++ /dev/null @@ -1,36 +0,0 @@ -require "test_helper" - -class InvalidUrlCharsValidatable - include ActiveModel::Validations - validates :chars, :invalid_url_chars => true - attr_accessor :chars -end - -class InvalidUrlCharsValidatorTest < ActiveSupport::TestCase - include Rails::Dom::Testing::Assertions::SelectorAssertions - - def test_with_valid_url_chars - c = InvalidUrlCharsValidatable.new - - valid = ["\x7f
", "test@examplecom\x0e-", "s\x1ff", "aa\ufffe", - "aa\x0b-", "aa\x08", "\uffff::", "\x00-も対応します", "\x0c*ping", - "foo\x1fbar", "foo\x7fbar", "foo\ufffebar", "foo\uffffbar"] - - valid.each do |v| - c.chars = v - assert c.valid?, "'#{v}' should be valid" - end - end - - def test_with_invalid_url_chars - c = InvalidUrlCharsValidatable.new - - invalid = ["Name.", "you;me", "he\"#", "
", "50%", "good?", - "vergrößern,deutsche", "ルシステムに;.も対応します", "輕觸搖/晃的遊戲", "/;.,?%#"] - - invalid.each do |v| - c.chars = v - assert_not c.valid?, "'#{v}' should not be valid" - end - end -end