Jelajahi Sumber

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
Andrew Cantino 9 tahun lalu
induk
melakukan
285407caea

+ 9 - 0
app/models/agents/website_agent.rb

@@ -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

+ 17 - 1
spec/models/agents/website_agent_spec.rb

@@ -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