]> cat aescling's git repositories - mastodon.git/commitdiff
add validation to tag name (#4194)
authormasarakki <masaki182@gmail.com>
Fri, 14 Jul 2017 09:02:49 +0000 (18:02 +0900)
committerEugen Rochko <eugen@zeonfederated.com>
Fri, 14 Jul 2017 09:02:49 +0000 (11:02 +0200)
app/models/tag.rb
spec/models/tag_spec.rb

index a14e2cc989bee362d2088f900fb9aa24bf3b55ac..0fa08e157c831d224dec5e67349751af2440f698 100644 (file)
 class Tag < ApplicationRecord
   has_and_belongs_to_many :statuses
 
-  HASHTAG_RE = /(?:^|[^\/\)\w])#([[:word:]_]*[[:alpha:]_][[:word:]_]*)/i
+  HASHTAG_NAME_RE = '[[:word:]_]*[[:alpha:]_][[:word:]_]*'
+  HASHTAG_RE = /(?:^|[^\/\)\w])#(#{HASHTAG_NAME_RE})/i
 
-  validates :name, presence: true, uniqueness: true
+  validates :name, presence: true, uniqueness: true, format: { with: /\A#{HASHTAG_NAME_RE}\z/i }
 
   def to_param
     name
index 555474c4493162974ef79a073936bfea6e11eb8c..f727fa1ddf8f33d74430f9faeefc878d48d75c32 100644 (file)
@@ -1,6 +1,24 @@
 require 'rails_helper'
 
 RSpec.describe Tag, type: :model do
+  describe 'validations' do
+    it 'invalid with #' do
+      expect(Tag.new(name: '#hello_world')).to_not be_valid
+    end
+
+    it 'invalid with .' do
+      expect(Tag.new(name: '.abcdef123')).to_not be_valid
+    end
+
+    it 'invalid with spaces' do
+      expect(Tag.new(name: 'hello world')).to_not be_valid
+    end
+
+    it 'valid with aesthetic' do
+      expect(Tag.new(name: 'aesthetic')).to be_valid
+    end
+  end
+
   describe 'HASHTAG_RE' do
     subject { Tag::HASHTAG_RE }