Commit 6b915762 authored by allison.browne's avatar allison.browne

Use custom EachValidators

Use custom EachValidator for project assosciation validation and
for Zoom URL validation.
parent c3879358
......@@ -4,8 +4,8 @@ class ZoomMeeting < ApplicationRecord
belongs_to :project, optional: false
belongs_to :issue, optional: false
validates :url, presence: true, length: { maximum: 255 }
validates_with ZoomMeetingValidator
validates :url, presence: true, length: { maximum: 255 }, zoom_url: true
validates :issue, project_association: true
enum issue_status: {
added: 1,
......@@ -14,5 +14,4 @@ class ZoomMeeting < ApplicationRecord
scope :added_to_issue, -> { where(issue_status: :added) }
scope :removed_from_issue, -> { where(issue_status: :removed) }
end
# frozen_string_literal: true
# IssueProjectAssociationValidator
#
# Custom validator to validate that the same project associated with
# the record is also associated with the value
#
# Example:
# class ZoomMeeting < ApplicationRecord
# belongs_to :project, optional: false
# belongs_to :issue, optional: false
# validates :issue, issue_project_association: true
# end
class ProjectAssociationValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
return if record.project == value&.project
record.errors[attribute] << 'must associate the same project'
end
end
# frozen_string_literal: true
# ZoomMeetingValidator
#
# Custom validator to TODO
#
class ZoomMeetingValidator < ActiveModel::Validator
def validate(zoom_meeting)
unless Gitlab::ZoomLinkExtractor.new(zoom_meeting.url).links.size == 1
zoom_meeting.errors.add(:url, 'must contain one valid Zoom URL')
end
unless zoom_meeting.project == zoom_meeting.issue&.project
zoom_meeting.errors.add(:issue, 'must associate the same project')
end
true
end
end
# frozen_string_literal: true
# ZoomUrlValidator
#
# Custom validator for zoom urls
#
class ZoomUrlValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
return if Gitlab::ZoomLinkExtractor.new(value).links.size == 1
record.errors.add(:url, 'must contain one valid Zoom URL')
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