diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2019-11-19 22:11:55 +0000 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2019-11-19 22:11:55 +0000 |
commit | 5a8431feceba47fd8e1804d9aa1b1730606b71d5 (patch) | |
tree | e5df8e0ceee60f4af8093f5c4c2f934b8abced05 /lib/gitlab/config/entry/validators.rb | |
parent | 4d477238500c347c6553d335d920bedfc5a46869 (diff) | |
download | gitlab-ce-5a8431feceba47fd8e1804d9aa1b1730606b71d5.tar.gz |
Add latest changes from gitlab-org/gitlab@12-5-stable-ee
Diffstat (limited to 'lib/gitlab/config/entry/validators.rb')
-rw-r--r-- | lib/gitlab/config/entry/validators.rb | 39 |
1 files changed, 37 insertions, 2 deletions
diff --git a/lib/gitlab/config/entry/validators.rb b/lib/gitlab/config/entry/validators.rb index 374f929878e..d1c23c41d35 100644 --- a/lib/gitlab/config/entry/validators.rb +++ b/lib/gitlab/config/entry/validators.rb @@ -61,8 +61,15 @@ module Gitlab include LegacyValidationHelpers def validate_each(record, attribute, value) - unless validate_array_of_strings(value) - record.errors.add(attribute, 'should be an array of strings') + valid = validate_array_of_strings(value) + + record.errors.add(attribute, 'should be an array of strings') unless valid + + if valid && options[:with] + unless value.all? { |v| v =~ options[:with] } + message = options[:message] || 'contains elements that do not match the format' + record.errors.add(attribute, message) + end end end end @@ -221,6 +228,34 @@ module Gitlab end end + class NestedArrayOfStringsValidator < ArrayOfStringsOrStringValidator + def validate_each(record, attribute, value) + unless validate_nested_array_of_strings(value) + record.errors.add(attribute, 'should be an array containing strings and arrays of strings') + end + end + + private + + def validate_nested_array_of_strings(values) + values.is_a?(Array) && values.all? { |element| validate_array_of_strings_or_string(element) } + end + end + + class StringOrNestedArrayOfStringsValidator < NestedArrayOfStringsValidator + def validate_each(record, attribute, value) + unless validate_string_or_nested_array_of_strings(value) + record.errors.add(attribute, 'should be a string or an array containing strings and arrays of strings') + end + end + + private + + def validate_string_or_nested_array_of_strings(values) + validate_string(values) || validate_nested_array_of_strings(values) + end + end + class TypeValidator < ActiveModel::EachValidator def validate_each(record, attribute, value) type = options[:with] |