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 years ago
parent
commit
285407caea
2 changed files with 26 additions and 1 deletions
  1. 9 0
      app/models/agents/website_agent.rb
  2. 17 1
      spec/models/agents/website_agent_spec.rb

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

@@ -151,6 +151,15 @@ module Agents
151 151
       end
152 152
 
153 153
       validate_web_request_options!
154
+      validate_extract_options!
155
+    end
156
+
157
+    def validate_extract_options!
158
+      if extraction_type == "json" && interpolated['extract'].is_a?(Hash)
159
+        unless interpolated['extract'].all? { |name, details| details.is_a?(Hash) && details['path'].present? }
160
+          errors.add(:base, 'When type is json, all extractions must have a path attribute.')
161
+        end
162
+      end
154 163
     end
155 164
 
156 165
     def check

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

@@ -75,6 +75,23 @@ describe Agents::WebsiteAgent do
75 75
         @checker.options['force_encoding'] = 'UTF-42'
76 76
         expect(@checker).not_to be_valid
77 77
       end
78
+
79
+      context "in 'json' type" do
80
+        it "should ensure that all extractions have a 'path'" do
81
+          @checker.options['type'] = 'json'
82
+          @checker.options['extract'] = {
83
+            'url' => { 'foo' => 'bar' },
84
+          }
85
+          expect(@checker).to_not be_valid
86
+          expect(@checker.errors_on(:base)).to include("When type is json, all extractions must have a path attribute.")
87
+
88
+          @checker.options['type'] = 'json'
89
+          @checker.options['extract'] = {
90
+            'url' => { 'path' => 'bar' },
91
+          }
92
+          expect(@checker).to be_valid
93
+        end
94
+      end
78 95
     end
79 96
 
80 97
     describe "#check" do
@@ -179,7 +196,6 @@ describe Agents::WebsiteAgent do
179 196
 
180 197
         checker.check
181 198
         event = Event.last
182
-        puts event.payload
183 199
         expect(event.payload['version']).to eq(2)
184 200
       end
185 201
     end