2018-06-27 16:20:23 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-03-31 17:02:26 +00:00
|
|
|
class CronValidator < ActiveModel::EachValidator
|
2020-05-09 00:09:39 +00:00
|
|
|
ATTRIBUTE_WHITELIST = %i[cron freeze_start freeze_end].freeze
|
|
|
|
|
|
|
|
NonWhitelistedAttributeError = Class.new(StandardError)
|
|
|
|
|
2017-03-31 17:02:26 +00:00
|
|
|
def validate_each(record, attribute, value)
|
2020-05-09 00:09:39 +00:00
|
|
|
if ATTRIBUTE_WHITELIST.include?(attribute)
|
|
|
|
cron_parser = Gitlab::Ci::CronParser.new(record.public_send(attribute), record.cron_timezone) # rubocop:disable GitlabSecurity/PublicSend
|
|
|
|
record.errors.add(attribute, " is invalid syntax") unless cron_parser.cron_valid?
|
|
|
|
else
|
2021-05-04 15:10:36 +00:00
|
|
|
raise NonWhitelistedAttributeError, "Non-whitelisted attribute"
|
2020-05-09 00:09:39 +00:00
|
|
|
end
|
2017-03-31 17:02:26 +00:00
|
|
|
end
|
|
|
|
end
|