Commit 036f3297 authored by Gabriel Mazetto's avatar Gabriel Mazetto

Extract emoji validation rules to a custom validator

parent aa87fcd0
...@@ -40,7 +40,7 @@ class AwardEmojisFinder ...@@ -40,7 +40,7 @@ class AwardEmojisFinder
def validate_name_param def validate_name_param
return unless params[:name] return unless params[:name]
raise ArgumentError, 'Invalid name param' unless params[:name].to_s.in?(Gitlab::Emoji.emojis_names) raise ArgumentError, 'Invalid name param' unless TanukiEmoji.find_by_alpha_code(params[:name].to_s)
end end
def validate_awarded_by_param def validate_awarded_by_param
......
...@@ -14,7 +14,7 @@ class AwardEmoji < ApplicationRecord ...@@ -14,7 +14,7 @@ class AwardEmoji < ApplicationRecord
validates :user, presence: true validates :user, presence: true
validates :awardable, presence: true, unless: :importing? validates :awardable, presence: true, unless: :importing?
validates :name, presence: true, inclusion: { in: Gitlab::Emoji.emojis_names } validates :name, presence: true, 'gitlab/emoji_name': true
validates :name, uniqueness: { scope: [:user, :awardable_type, :awardable_id] }, unless: -> { ghost_user? || importing? } validates :name, uniqueness: { scope: [:user, :awardable_type, :awardable_id] }, unless: -> { ghost_user? || importing? }
participant :user participant :user
......
...@@ -22,7 +22,7 @@ class UserStatus < ApplicationRecord ...@@ -22,7 +22,7 @@ class UserStatus < ApplicationRecord
enum availability: { not_set: 0, busy: 1 } enum availability: { not_set: 0, busy: 1 }
validates :user, presence: true validates :user, presence: true
validates :emoji, inclusion: { in: Gitlab::Emoji.emojis_names } validates :emoji, 'gitlab/emoji_name': true
validates :message, length: { maximum: 100 }, allow_blank: true validates :message, length: { maximum: 100 }, allow_blank: true
scope :scheduled_for_cleanup, -> { where(arel_table[:clear_status_at].lteq(Time.current)) } scope :scheduled_for_cleanup, -> { where(arel_table[:clear_status_at].lteq(Time.current)) }
......
# frozen_string_literal: true
# Gitlab::EmojiNameValidator
#
# Validates that the provided value matches an indexed emoji alpha code
#
# @example Usage
# class AwardEmoji < ApplicationRecord
# validate :name, 'gitlab/emoji_name': true
# end
module Gitlab
class EmojiNameValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
unless TanukiEmoji.find_by_alpha_code(value.to_s)
record.errors.add(attribute, (options[:message] || 'is not a valid emoji name'))
end
end
end
end
...@@ -53,7 +53,7 @@ RSpec.describe Users::UpdateService do ...@@ -53,7 +53,7 @@ RSpec.describe Users::UpdateService do
result = update_user(user, status: { emoji: "Moo!" }) result = update_user(user, status: { emoji: "Moo!" })
expect(result[:status]).to eq(:error) expect(result[:status]).to eq(:error)
expect(result[:message]).to eq("Emoji is not included in the list") expect(result[:message]).to eq("Emoji is not a valid emoji name")
end end
it 'updates user detail with provided attributes' do it 'updates user detail with provided attributes' do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment