fluke/devops-service/db/validators/run_list_array.rb
Tim Lianov 03dc3d8d99 v3
2018-04-04 22:44:39 +03:00

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