Add first custom validator for new ci config
This follows a standard `ActiveModel` pattern of creating a custom validators. We use `ActiveModel::EachValidator` here that reuses methods provided by `LegacyValidationHelpers`. We will remove `LegacyValidationHelpers` on some point in the future, at the later stages of CI configuration refactoring. It may be possible to rewrite custom validators to use format like: `validates :config, array_of: String`
This commit is contained in:
parent
a9bd16bd0a
commit
d9ca84015c
|
|
@ -14,15 +14,7 @@ module Gitlab
|
|||
include Validatable
|
||||
|
||||
validations do
|
||||
include LegacyValidationHelpers
|
||||
|
||||
validate :array_of_strings
|
||||
|
||||
def array_of_strings
|
||||
unless validate_array_of_strings(self.config)
|
||||
errors.add(:config, 'should be an array of strings')
|
||||
end
|
||||
end
|
||||
validates :config, array_of_strings: true
|
||||
end
|
||||
|
||||
def value
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ module Gitlab
|
|||
module Node
|
||||
class Validator < SimpleDelegator
|
||||
include ActiveModel::Validations
|
||||
include Node::Validators
|
||||
|
||||
def initialize(node)
|
||||
super(node)
|
||||
|
|
|
|||
|
|
@ -0,0 +1,19 @@
|
|||
module Gitlab
|
||||
module Ci
|
||||
class Config
|
||||
module Node
|
||||
module Validators
|
||||
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
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Loading…
Reference in New Issue