2016-06-17 06:06:48 -04:00
|
|
|
module Gitlab
|
|
|
|
module Ci
|
|
|
|
class Config
|
2016-11-14 04:31:45 -05:00
|
|
|
module Entry
|
2016-06-17 06:06:48 -04:00
|
|
|
module Validators
|
2016-06-29 04:46:30 -04:00
|
|
|
class AllowedKeysValidator < ActiveModel::EachValidator
|
|
|
|
def validate_each(record, attribute, value)
|
2016-07-18 09:38:06 -04:00
|
|
|
unknown_keys = record.config.try(:keys).to_a - options[:in]
|
|
|
|
|
|
|
|
if unknown_keys.any?
|
|
|
|
record.errors.add(:config, 'contains unknown keys: ' +
|
|
|
|
unknown_keys.join(', '))
|
2016-06-29 04:46:30 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-06-17 06:06:48 -04:00
|
|
|
class ArrayOfStringsValidator < ActiveModel::EachValidator
|
|
|
|
include LegacyValidationHelpers
|
|
|
|
|
|
|
|
def validate_each(record, attribute, value)
|
|
|
|
unless validate_array_of_strings(value)
|
|
|
|
record.errors.add(attribute, 'should be an array of strings')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-06-17 08:51:39 -04:00
|
|
|
|
2016-06-24 03:49:54 -04:00
|
|
|
class BooleanValidator < ActiveModel::EachValidator
|
|
|
|
include LegacyValidationHelpers
|
|
|
|
|
|
|
|
def validate_each(record, attribute, value)
|
|
|
|
unless validate_boolean(value)
|
|
|
|
record.errors.add(attribute, 'should be a boolean value')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-07-18 08:15:01 -04:00
|
|
|
class DurationValidator < ActiveModel::EachValidator
|
|
|
|
include LegacyValidationHelpers
|
|
|
|
|
|
|
|
def validate_each(record, attribute, value)
|
|
|
|
unless validate_duration(value)
|
|
|
|
record.errors.add(attribute, 'should be a duration')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-06-24 02:58:09 -04:00
|
|
|
class KeyValidator < ActiveModel::EachValidator
|
|
|
|
include LegacyValidationHelpers
|
|
|
|
|
|
|
|
def validate_each(record, attribute, value)
|
|
|
|
unless validate_string(value)
|
|
|
|
record.errors.add(attribute, 'should be a string or symbol')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-11-13 22:56:39 -05:00
|
|
|
class RegexpValidator < ActiveModel::EachValidator
|
|
|
|
include LegacyValidationHelpers
|
|
|
|
|
|
|
|
def validate_each(record, attribute, value)
|
|
|
|
unless validate_regexp(value)
|
|
|
|
record.errors.add(attribute, 'must be a regular expression')
|
|
|
|
end
|
|
|
|
end
|
2016-12-07 00:01:34 -05:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def look_like_regexp?(value)
|
|
|
|
value =~ %r{\A/.*/\z}
|
|
|
|
end
|
|
|
|
|
|
|
|
def validate_regexp(value)
|
|
|
|
look_like_regexp?(value) &&
|
|
|
|
Regexp.new(value.to_s[1...-1]) &&
|
|
|
|
true
|
|
|
|
rescue RegexpError
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class ArrayOfStringsOrRegexps < RegexpValidator
|
|
|
|
def validate_each(record, attribute, value)
|
|
|
|
unless validate_array_of_strings_or_regexps(value)
|
|
|
|
record.errors.add(attribute, 'should be an array of strings or regexps')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def validate_array_of_strings_or_regexps(values)
|
|
|
|
values.is_a?(Array) && values.all?(&method(:validate_string_or_regexp))
|
|
|
|
end
|
|
|
|
|
|
|
|
def validate_string_or_regexp(value)
|
|
|
|
return true if value.is_a?(Symbol)
|
|
|
|
return false unless value.is_a?(String)
|
|
|
|
|
|
|
|
if look_like_regexp?(value)
|
|
|
|
validate_regexp(value)
|
|
|
|
else
|
|
|
|
true
|
|
|
|
end
|
|
|
|
end
|
2016-11-13 22:56:39 -05:00
|
|
|
end
|
|
|
|
|
2016-06-21 05:43:32 -04:00
|
|
|
class TypeValidator < ActiveModel::EachValidator
|
2016-06-17 08:51:39 -04:00
|
|
|
def validate_each(record, attribute, value)
|
2016-06-21 05:43:32 -04:00
|
|
|
type = options[:with]
|
|
|
|
raise unless type.is_a?(Class)
|
|
|
|
|
|
|
|
unless value.is_a?(type)
|
2016-07-19 07:08:28 -04:00
|
|
|
message = options[:message] || "should be a #{type.name}"
|
|
|
|
record.errors.add(attribute, message)
|
2016-06-17 08:51:39 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-06-22 05:22:53 -04:00
|
|
|
|
|
|
|
class VariablesValidator < ActiveModel::EachValidator
|
|
|
|
include LegacyValidationHelpers
|
|
|
|
|
|
|
|
def validate_each(record, attribute, value)
|
|
|
|
unless validate_variables(value)
|
|
|
|
record.errors.add(attribute, 'should be a hash of key value pairs')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-06-17 06:06:48 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|