Commit 80bd2df4 authored by Peter Leitzen's avatar Peter Leitzen Committed by Vitali Tatarintev

Validate path being a list of strings

Checks whether the alert's field path is a list of strings
parent db63248d
......@@ -7,9 +7,20 @@ module AlertManagement
attr_accessor :project, :path, :label, :type
SUPPORTED_TYPES = %w[string numeric datetime].freeze
validates :project, presence: true
validates :path, presence: true
validates :label, presence: true
validates :type, presence: true
validates :type, inclusion: { in: SUPPORTED_TYPES }
validate :path_is_list_of_strings
private
def path_is_list_of_strings
unless path.is_a?(Array) && path.all? { |segment| segment.is_a?(String) }
errors.add(:path, 'must be a list of strings')
end
end
end
end
......@@ -3,7 +3,7 @@
FactoryBot.define do
factory :alert_management_alert_payload_field, class: 'AlertManagement::AlertPayloadField' do
project
path { 'title' }
path { ['title'] }
label { 'Title' }
type { 'string' }
end
......
......@@ -3,10 +3,22 @@
require 'spec_helper'
RSpec.describe AlertManagement::AlertPayloadField do
let(:alert_payload_field) { build_stubbed(:alert_management_alert_payload_field) }
describe 'Validations' do
it { is_expected.to validate_presence_of(:project) }
it { is_expected.to validate_presence_of(:path) }
it { is_expected.to validate_presence_of(:label) }
it { is_expected.to validate_presence_of(:type) }
it { is_expected.to validate_inclusion_of(:type).in_array(described_class::SUPPORTED_TYPES) }
describe '#path_is_list_of_strings' do
before do
alert_management_alert_payload_field.path = path
alert_management_alert_payload_field.valid?
end
context 'when path is nil'
context 'when path is empty array'
context 'when path does not contain only strings'
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