Commit bf5f4271 authored by Matija Čupić's avatar Matija Čupić

Move validation logic into entry

Moves validation logic into the entry instead of having it in a separate
validator.
parent 0ff024d4
......@@ -11,7 +11,16 @@ module Gitlab
include ::Gitlab::Config::Entry::Validatable
validations do
validates :config, array_of_strings_or_arrays_of_strings: true
validate do
unless config.is_a?(String) ||
(config.is_a?(Array) && config.all? { |element| element.is_a?(String) || validate_array_of_strings?(element) })
errors.add(:config, 'should be an array of strings and arrays of strings or string')
end
end
def validate_array_of_strings?(value)
value.is_a?(Array) && value.all? { |element| element.is_a?(String) }
end
end
def value
......
......@@ -11,11 +11,23 @@ module Gitlab
include ::Gitlab::Config::Entry::Validatable
validations do
validates :config, array_of_strings_or_arrays_of_strings: true
validate do
unless config.is_a?(Array) && config.all? { |element| element.is_a?(String) || validate_array_of_strings?(element) }
errors.add(:config, 'should be an array of strings and arrays of strings or string')
end
end
def validate_array_of_strings?(value)
value.is_a?(Array) && value.all? { |element| element.is_a?(String) }
end
end
def value
config.flatten
if config.is_a?(Array)
config.flatten
else
config
end
end
end
end
......
......@@ -206,16 +206,6 @@ module Gitlab
end
end
class ArrayOfStringsOrArraysOfStringsValidator < ActiveModel::EachValidator
include LegacyValidationHelpers
def validate_each(record, attribute, value)
unless value.is_a?(Array) && value.all? { |element| element.is_a?(String) || validate_array_of_strings(element) }
record.errors.add(attribute, 'should be an array of strings and arrays of strings')
end
end
end
class ArrayOfStringsOrRegexpsWithFallbackValidator < ArrayOfStringsOrRegexpsValidator
protected
......
......@@ -81,13 +81,30 @@ describe Gitlab::Ci::Config::Entry::Commands do
end
end
context 'when entry value is not valid' do
context 'when entry value is integer' do
let(:config) { 1 }
describe '#errors' do
it 'saves errors' do
expect(entry.errors)
.to include 'commands config should be an array of strings and arrays of strings'
.to include 'commands config should be an array of strings and arrays of strings or string'
end
end
end
context 'when entry value is multi-level nested array' do
let(:config) { [['ls', ['echo 1']], 'pwd'] }
describe '#errors' do
it 'saves errors' do
expect(entry.errors)
.to include 'commands config should be an array of strings and arrays of strings or string'
end
end
describe '#valid?' do
it 'is not valid' do
expect(entry).not_to be_valid
end
end
end
......
......@@ -72,13 +72,30 @@ describe Gitlab::Ci::Config::Entry::Script do
end
end
context 'when entry value is not correct' do
context 'when entry value is string' do
let(:config) { 'ls' }
describe '#errors' do
it 'saves errors' do
expect(entry.errors)
.to include 'script config should be an array of strings and arrays of strings'
.to include 'script config should be an array of strings and arrays of strings or string'
end
end
describe '#valid?' do
it 'is not valid' do
expect(entry).not_to be_valid
end
end
end
context 'when entry value is multi-level nested array' do
let(:config) { [['ls', ['echo 1']], 'pwd'] }
describe '#errors' do
it 'saves errors' do
expect(entry.errors)
.to include 'script config should be an array of strings and arrays of strings or string'
end
end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment