Merge pull request #831 from cantino/website_agent_jsonpath_validation

add a validation that warns the user if they have not provided a path when using JSONPath
This commit is contained in:
Andrew Cantino 2015-05-28 00:57:37 -04:00
commit 285407caea
2 changed files with 26 additions and 1 deletions

View file

@ -151,6 +151,15 @@ module Agents
end
validate_web_request_options!
validate_extract_options!
end
def validate_extract_options!
if extraction_type == "json" && interpolated['extract'].is_a?(Hash)
unless interpolated['extract'].all? { |name, details| details.is_a?(Hash) && details['path'].present? }
errors.add(:base, 'When type is json, all extractions must have a path attribute.')
end
end
end
def check

View file

@ -75,6 +75,23 @@ describe Agents::WebsiteAgent do
@checker.options['force_encoding'] = 'UTF-42'
expect(@checker).not_to be_valid
end
context "in 'json' type" do
it "should ensure that all extractions have a 'path'" do
@checker.options['type'] = 'json'
@checker.options['extract'] = {
'url' => { 'foo' => 'bar' },
}
expect(@checker).to_not be_valid
expect(@checker.errors_on(:base)).to include("When type is json, all extractions must have a path attribute.")
@checker.options['type'] = 'json'
@checker.options['extract'] = {
'url' => { 'path' => 'bar' },
}
expect(@checker).to be_valid
end
end
end
describe "#check" do
@ -179,7 +196,6 @@ describe Agents::WebsiteAgent do
checker.check
event = Event.last
puts event.payload
expect(event.payload['version']).to eq(2)
end
end