43 lines
1.1 KiB
Ruby
43 lines
1.1 KiB
Ruby
require 'db/validators/run_list_array'
|
|
|
|
class ProperNamedTasksFormatValidator < ActiveModel::Validator
|
|
|
|
# should be an array of hashes like:
|
|
# {
|
|
# 'name' => 'restart',
|
|
# 'run_list' => [
|
|
# 'role[restart_service]'
|
|
# ]
|
|
# }
|
|
def validate(record)
|
|
unless record.named_tasks
|
|
record.errors.add :named_tasks, "Tasks should not be nil"
|
|
return
|
|
end
|
|
record.named_tasks.each do |named_task|
|
|
check_name!(named_task, record)
|
|
check_run_list!(named_task, record)
|
|
end
|
|
end
|
|
|
|
private
|
|
|
|
def check_name!(task, record)
|
|
unless task.key?('name')
|
|
record.errors.add :named_tasks, "One of tasks doesn't have a name"
|
|
end
|
|
end
|
|
|
|
def check_run_list!(task, record)
|
|
RunListArrayValidator.validate_list(task['run_list'])
|
|
rescue Devops::Exception::ValidationError => e
|
|
if e.body[:elements]
|
|
wrong_elements = e.body[:elements]
|
|
record.errors.add :named_tasks, "Invalid run list elements: '#{wrong_elements.join(', ')}' for task #{task['name']}."
|
|
else
|
|
record.errors.add :named_tasks, "Task #{task['name']} has invalid run list."
|
|
end
|
|
end
|
|
|
|
end
|