2021-03-08 18:12:59 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module ReleaseHighlights
|
|
|
|
class Validator::Entry
|
|
|
|
include ActiveModel::Validations
|
|
|
|
include ActiveModel::Validations::Callbacks
|
|
|
|
|
2022-08-27 11:52:29 +05:30
|
|
|
AVAILABLE_IN = %w(Free Premium Ultimate).freeze
|
2023-04-23 21:23:45 +05:30
|
|
|
HYPHENATED_ATTRIBUTES = [:self_managed, :gitlab_com].freeze
|
2021-03-08 18:12:59 +05:30
|
|
|
|
|
|
|
attr_reader :entry
|
2023-04-23 21:23:45 +05:30
|
|
|
attr_accessor :available_in, :description, :gitlab_com, :image_url, :name, :published_at, :release, :self_managed,
|
|
|
|
:stage
|
2021-03-08 18:12:59 +05:30
|
|
|
|
2022-08-27 11:52:29 +05:30
|
|
|
validates :name, :description, :stage, presence: true
|
2023-04-23 21:23:45 +05:30
|
|
|
validates :self_managed, :gitlab_com, inclusion: { in: [true, false], message: "must be a boolean" }
|
|
|
|
validates :documentation_link, public_url: { dns_rebind_protection: true }
|
|
|
|
validates :image_url, public_url: { dns_rebind_protection: true }, allow_nil: true
|
2021-03-08 18:12:59 +05:30
|
|
|
validates :release, numericality: true
|
|
|
|
validate :validate_published_at
|
2022-08-27 11:52:29 +05:30
|
|
|
validate :validate_available_in
|
2021-03-08 18:12:59 +05:30
|
|
|
|
|
|
|
after_validation :add_line_numbers_to_errors!
|
|
|
|
|
|
|
|
def initialize(entry)
|
|
|
|
@entry = entry
|
|
|
|
end
|
|
|
|
|
|
|
|
def validate_published_at
|
|
|
|
published_at = value_for('published_at')
|
|
|
|
|
|
|
|
return if published_at.is_a?(Date)
|
|
|
|
|
|
|
|
errors.add(:published_at, 'must be valid Date')
|
|
|
|
end
|
|
|
|
|
2022-08-27 11:52:29 +05:30
|
|
|
def validate_available_in
|
|
|
|
available_in = value_for('available_in')
|
2021-03-08 18:12:59 +05:30
|
|
|
|
2022-08-27 11:52:29 +05:30
|
|
|
if !available_in.is_a?(Array) || available_in.empty? || available_in.any? { |p| AVAILABLE_IN.exclude?(p) }
|
|
|
|
errors.add(:available_in, "must be one of #{AVAILABLE_IN}")
|
2021-03-08 18:12:59 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def read_attribute_for_validation(key)
|
|
|
|
value_for(key)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def add_line_numbers_to_errors!
|
|
|
|
errors.messages.each do |attribute, messages|
|
2021-09-04 01:27:46 +05:30
|
|
|
extended_messages = messages.map { |m| "#{m} (line #{line_number_for(attribute)})" }
|
|
|
|
|
|
|
|
errors.delete(attribute)
|
|
|
|
extended_messages.each { |extended_message| errors.add(attribute, extended_message) }
|
2021-03-08 18:12:59 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def line_number_for(key)
|
|
|
|
node = find_node(key)
|
|
|
|
|
|
|
|
(node&.start_line || @entry.start_line) + 1
|
|
|
|
end
|
|
|
|
|
|
|
|
def value_for(key)
|
|
|
|
node = find_node(key)
|
|
|
|
|
|
|
|
return if node.nil?
|
|
|
|
|
|
|
|
index = entry.children.find_index(node)
|
|
|
|
|
|
|
|
next_node = entry.children[index + 1]
|
2023-04-23 21:23:45 +05:30
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
next_node&.to_ruby
|
|
|
|
end
|
|
|
|
|
|
|
|
def find_node(key)
|
2023-04-23 21:23:45 +05:30
|
|
|
formatted_key = key.in?(HYPHENATED_ATTRIBUTES) ? key.to_s.dasherize : key.to_s
|
|
|
|
entry.children.find { |node| node.try(:value) == formatted_key }
|
2021-03-08 18:12:59 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|