2018-06-27 12:20:23 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-03-31 13:02:26 -04:00
|
|
|
class CronValidator < ActiveModel::EachValidator
|
2020-05-08 20:09:39 -04:00
|
|
|
ATTRIBUTE_WHITELIST = %i[cron freeze_start freeze_end].freeze
|
|
|
|
|
|
|
|
NonWhitelistedAttributeError = Class.new(StandardError)
|
|
|
|
|
2017-03-31 13:02:26 -04:00
|
|
|
def validate_each(record, attribute, value)
|
2020-05-08 20:09:39 -04: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 11:10:36 -04:00
|
|
|
raise NonWhitelistedAttributeError, "Non-whitelisted attribute"
|
2020-05-08 20:09:39 -04:00
|
|
|
end
|
2017-03-31 13:02:26 -04:00
|
|
|
end
|
|
|
|
end
|