Commit 837b45c3 authored by Jason Goodman's avatar Jason Goodman Committed by Shinya Maeda

Extract FeatureFlagUserXidsValidator class

Remove duplicate logic from feature flag Strategy and UserList
parent a7e49cba
...@@ -51,7 +51,7 @@ module Operations ...@@ -51,7 +51,7 @@ module Operations
when STRATEGY_GRADUALROLLOUTUSERID when STRATEGY_GRADUALROLLOUTUSERID
gradual_rollout_user_id_parameters_validation gradual_rollout_user_id_parameters_validation
when STRATEGY_USERWITHID when STRATEGY_USERWITHID
user_with_id_parameters_validation FeatureFlagUserXidsValidator.validate_user_xids(self, :parameters, parameters['userIds'], 'userIds')
end end
end end
...@@ -68,24 +68,6 @@ module Operations ...@@ -68,24 +68,6 @@ module Operations
end end
end end
def user_with_id_parameters_validation
user_ids = parameters['userIds']
unless user_ids.is_a?(String) && !user_ids.match(/[\n\r\t]|,,/) && valid_ids?(user_ids.split(","))
parameters_error("userIds must be a string of unique comma separated values each #{USERID_MAX_LENGTH} characters or less")
end
end
def valid_ids?(user_ids)
user_ids.uniq.length == user_ids.length &&
user_ids.all? { |id| valid_id?(id) }
end
def valid_id?(user_id)
user_id.present? &&
user_id.strip == user_id &&
user_id.length <= USERID_MAX_LENGTH
end
def parameters_error(message) def parameters_error(message)
errors.add(:parameters, message) errors.add(:parameters, message)
false false
......
...@@ -5,8 +5,6 @@ module Operations ...@@ -5,8 +5,6 @@ module Operations
class UserList < ApplicationRecord class UserList < ApplicationRecord
include AtomicInternalId include AtomicInternalId
USERXID_MAX_LENGTH = 256
self.table_name = 'operations_user_lists' self.table_name = 'operations_user_lists'
belongs_to :project belongs_to :project
...@@ -18,27 +16,7 @@ module Operations ...@@ -18,27 +16,7 @@ module Operations
presence: true, presence: true,
uniqueness: { scope: :project_id }, uniqueness: { scope: :project_id },
length: 1..255 length: 1..255
validate :user_xids_validation validates :user_xids, feature_flag_user_xids: true
private
def user_xids_validation
unless user_xids.is_a?(String) && !user_xids.match(/[\n\r\t]|,,/) && valid_xids?(user_xids.split(","))
errors.add(:user_xids,
"user_xids must be a string of unique comma separated values each #{USERXID_MAX_LENGTH} characters or less")
end
end
def valid_xids?(user_xids)
user_xids.uniq.length == user_xids.length &&
user_xids.all? { |xid| valid_xid?(xid) }
end
def valid_xid?(user_xid)
user_xid.present? &&
user_xid.strip == user_xid &&
user_xid.length <= USERXID_MAX_LENGTH
end
end end
end end
end end
# frozen_string_literal: true
class FeatureFlagUserXidsValidator < ActiveModel::EachValidator
USERXID_MAX_LENGTH = 256
def validate_each(record, attribute, value)
self.class.validate_user_xids(record, attribute, value, attribute)
end
class << self
def validate_user_xids(record, attribute, user_xids, error_message_attribute_name)
unless user_xids.is_a?(String) && !user_xids.match(/[\n\r\t]|,,/) && valid_xids?(user_xids.split(","))
record.errors.add(attribute,
"#{error_message_attribute_name} must be a string of unique comma separated values each #{USERXID_MAX_LENGTH} characters or less")
end
end
private
def valid_xids?(user_xids)
user_xids.uniq.length == user_xids.length &&
user_xids.all? { |xid| valid_xid?(xid) }
end
def valid_xid?(user_xid)
user_xid.present? &&
user_xid.strip == user_xid &&
user_xid.length <= USERXID_MAX_LENGTH
end
end
end
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