22 lines
831 B
Ruby
22 lines
831 B
Ruby
require 'exceptions/validation_error'
|
|
class RunListArrayValidator < ActiveModel::EachValidator
|
|
|
|
RUN_LIST_REGEX = /\A(role|recipe)\[[\w-]+(::[\w-]+)?\]\Z/
|
|
|
|
def validate_each(record, attribute, valueArray)
|
|
unless valueArray.empty?
|
|
invalid_elements = valueArray.select {|l| (RUN_LIST_REGEX =~ l).nil?}
|
|
record.errors.add attribute, (options[:message] || "Invalid values: '#{invalid_elements.join("', '")}'") unless invalid_elements.empty?
|
|
end
|
|
end
|
|
|
|
def self.validate_list list
|
|
raise Devops::Exception::ValidationError.new nil, {type: "Is not an array"} unless list.is_a?(Array)
|
|
unless list.empty?
|
|
invalid_elements = list.select{|l| (RUN_LIST_REGEX =~ l).nil?}
|
|
raise Devops::Exception::ValidationError.new(nil, elements: invalid_elements) unless invalid_elements.empty?
|
|
end
|
|
end
|
|
|
|
end
|