Commit c019585c authored by Grzegorz Bizon's avatar Grzegorz Bizon

Validate interface only with CI node validator

parent 56e88b8c
...@@ -8,30 +8,33 @@ module Gitlab ...@@ -8,30 +8,33 @@ module Gitlab
class Cache < Entry class Cache < Entry
include Configurable include Configurable
node :key, Key,
description: 'Cache key used to define a cache affinity.'
node :untracked, Boolean,
description: 'Cache all untracked files.'
node :paths, Paths,
description: 'Specify which paths should be cached across builds.'
validations do validations do
validate :allowed_keys validate :keys
def unknown_keys def unknown_keys
return [] unless @node.config.is_a?(Hash) return [] unless config.is_a?(Hash)
config.keys - allowed_keys
@node.config.keys - @node.class.nodes.keys
end end
def allowed_keys def keys
if unknown_keys.any? if unknown_keys.any?
errors.add(:config, "contains unknown keys #{unknown_keys}") errors.add(:config, "contains unknown keys #{unknown_keys}")
end end
end end
end end
node :key, Node::Key, def allowed_keys
description: 'Cache key used to define a cache affinity.' self.class.nodes.keys
end
node :untracked, Boolean,
description: 'Cache all untracked files.'
node :paths, Paths,
description: 'Specify which paths should be cached across builds.'
end end
end end
end end
......
...@@ -8,12 +8,11 @@ module Gitlab ...@@ -8,12 +8,11 @@ module Gitlab
def initialize(node) def initialize(node)
super(node) super(node)
@node = node
end end
def messages def messages
errors.full_messages.map do |error| errors.full_messages.map do |error|
"#{@node.key} #{error}".humanize "#{key} #{error}".humanize
end end
end end
......
...@@ -5,7 +5,13 @@ describe Gitlab::Ci::Config::Node::Validator do ...@@ -5,7 +5,13 @@ describe Gitlab::Ci::Config::Node::Validator do
let(:validator_instance) { validator.new(node) } let(:validator_instance) { validator.new(node) }
let(:node) { spy('node') } let(:node) { spy('node') }
shared_examples 'delegated validator' do describe 'delegated validator' do
before do
validator.class_eval do
validates :test_attribute, presence: true
end
end
context 'when node is valid' do context 'when node is valid' do
before do before do
allow(node).to receive(:test_attribute).and_return('valid value') allow(node).to receive(:test_attribute).and_return('valid value')
...@@ -40,28 +46,4 @@ describe Gitlab::Ci::Config::Node::Validator do ...@@ -40,28 +46,4 @@ describe Gitlab::Ci::Config::Node::Validator do
end end
end end
end end
describe 'attributes validations' do
before do
validator.class_eval do
validates :test_attribute, presence: true
end
end
it_behaves_like 'delegated validator'
end
describe 'interface validations' do
before do
validator.class_eval do
validate do
unless @node.test_attribute == 'valid value'
errors.add(:test_attribute, 'invalid value')
end
end
end
end
it_behaves_like 'delegated validator'
end
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