Commit de4c9a27 authored by Grzegorz Bizon's avatar Grzegorz Bizon

Improve CI stage configuration entry validations

parent b228787f
......@@ -10,14 +10,16 @@ module Gitlab
validations do
validates :config, type: String
validates :global, required_attribute: true
validate :known_stage, on: :processed
def known_stage
unless known?
stages_list = global.stages.join(', ')
errors.add(:config,
"should be one of defined stages (#{stages_list})")
with_options on: :processed do
validates :global, required: true
validate do
unless known?
errors.add(:config,
'should be one of defined stages ' \
"(#{global.stages.join(', ')})")
end
end
end
end
......
......@@ -33,7 +33,7 @@ module Gitlab
end
end
class RequiredAttributeValidator < ActiveModel::EachValidator
class RequiredValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
if value.nil?
raise Entry::InvalidError,
......
......@@ -28,10 +28,10 @@ describe Gitlab::Ci::Config::Node::Stage do
context 'when stage config is incorrect' do
describe '#errors' do
context 'when reference to global node is not set' do
let(:stage) { described_class.new(config) }
let(:stage) { described_class.new('test') }
it 'raises error' do
expect { stage }.to raise_error(
expect { stage.validate! }.to raise_error(
Gitlab::Ci::Config::Node::Entry::InvalidError,
/Entry needs global attribute set internally./
)
......
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