Commit 1be6dd33 authored by Maxime Orefice's avatar Maxime Orefice

Validate filename for json schema validator

parent f89905ce
......@@ -10,8 +10,12 @@
# end
#
class JsonSchemaValidator < ActiveModel::EachValidator
FILENAME_ALLOWED = /\A[a-z0-9_-]*\Z/.freeze
FilenameError = Class.new(StandardError)
def initialize(options)
raise ArgumentError, "Expected 'filename' as an argument" unless options[:filename]
raise FilenameError, "Must be a valid 'filename'" unless options[:filename].match?(FILENAME_ALLOWED)
super(options)
end
......
......@@ -8,7 +8,7 @@ describe JsonSchemaValidator do
subject { validator.validate(build_report_result) }
context 'when file_path is set' do
context 'when filename is set' do
let(:validator) { described_class.new(attributes: [:data], filename: "build_report_result_data") }
context 'when data is valid' do
......@@ -31,12 +31,20 @@ describe JsonSchemaValidator do
end
end
context 'when file_path is not set' do
context 'when filename is not set' do
let(:validator) { described_class.new(attributes: [:data]) }
it 'raises an ArgumentError' do
expect { subject }.to raise_error(ArgumentError)
end
end
context 'when filename is invalid' do
let(:validator) { described_class.new(attributes: [:data], filename: "invalid$filename") }
it 'raises a FilenameError' do
expect { subject }.to raise_error(described_class::FilenameError)
end
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