Skip to content

Commit

Permalink
Merge pull request #831 from cantino/website_agent_jsonpath_validation
Browse files Browse the repository at this point in the history
add a validation that warns the user if they have not provided a path when using JSONPath
  • Loading branch information
cantino committed May 28, 2015
2 parents 562f95a + 84ebfa1 commit 285407c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
9 changes: 9 additions & 0 deletions app/models/agents/website_agent.rb
Expand Up @@ -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
Expand Down
18 changes: 17 additions & 1 deletion spec/models/agents/website_agent_spec.rb
Expand Up @@ -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
Expand Down Expand Up @@ -179,7 +196,6 @@

checker.check
event = Event.last
puts event.payload
expect(event.payload['version']).to eq(2)
end
end
Expand Down

0 comments on commit 285407c

Please sign in to comment.