From 84ebfa18cb7643cfaf296a480e0726429cab1311 Mon Sep 17 00:00:00 2001 From: Andrew Cantino Date: Tue, 26 May 2015 21:16:52 -0700 Subject: [PATCH] add a validation that warns the user if they have not provided a path when using JSONPath --- app/models/agents/website_agent.rb | 9 +++++++++ spec/models/agents/website_agent_spec.rb | 18 +++++++++++++++++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/app/models/agents/website_agent.rb b/app/models/agents/website_agent.rb index aad791ee27..4f861e6df9 100644 --- a/app/models/agents/website_agent.rb +++ b/app/models/agents/website_agent.rb @@ -151,6 +151,15 @@ def validate_options 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 diff --git a/spec/models/agents/website_agent_spec.rb b/spec/models/agents/website_agent_spec.rb index 7f45cb9095..c0959f4d64 100644 --- a/spec/models/agents/website_agent_spec.rb +++ b/spec/models/agents/website_agent_spec.rb @@ -75,6 +75,23 @@ @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 @@ checker.check event = Event.last - puts event.payload expect(event.payload['version']).to eq(2) end end