Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Add choice_rule payload validation #187

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions lib/floe/workflow/choice_rule/data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,24 @@ module Floe
class Workflow
class ChoiceRule
class Data < Floe::Workflow::ChoiceRule
attr_reader :compare_key

COMPARE_KEYS = (
%w[IsNull IsPresent IsNumeric IsString IsBoolean IsTimestamp StringMatches] +
%w[String Numeric Boolean Timestamp].flat_map { |k| ["#{k}Equals", "#{k}EqualsPath"] } +
%w[String Numeric Timestamp].flat_map { |k| %w[LessThan GreaterThan LessThanEquals GreaterThanEquals].flat_map { |x| ["#{k}#{x}", "#{k}#{x}Path"] } }
).freeze
Comment on lines +9 to +13
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would have preferred using a hash mapping the string to the operation.
Then we could drop the switch

But this is good for now

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good idea let me give that a shot


def initialize(*)
super

compare_keys = payload.keys & COMPARE_KEYS
raise Floe::InvalidWorkflowError, "Data-test Expression Choice Rule must have a compare key" if compare_keys.empty?
raise Floe::InvalidWorkflowError, "Data-test Expression Choice Rule must have only one compare key" if compare_keys.size != 1

@compare_key = compare_keys.first
end

def true?(context, input)
lhs = variable_value(context, input)
rhs = compare_value(context, input)
Expand Down Expand Up @@ -80,10 +98,6 @@ def is_timestamp?(value) # rubocop:disable Naming/PredicateName
false
end

def compare_key
@compare_key ||= payload.keys.detect { |key| key.match?(/^(IsNull|IsPresent|IsNumeric|IsString|IsBoolean|IsTimestamp|String|Numeric|Boolean|Timestamp)/) }
end

def compare_value(context, input)
compare_key.end_with?("Path") ? Path.value(payload[compare_key], context, input) : payload[compare_key]
end
Expand Down
27 changes: 27 additions & 0 deletions spec/workflow/choice_rule_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,33 @@
end
end

context "with a missing compare key" do
let(:payload) { {"Variable" => "$.foo", "Next" => "FirstMatchState"} }
let(:input) { {"foo" => "bar"} }

it "raises an exception" do
expect { subject }.to raise_exception(Floe::InvalidWorkflowError, "Data-test Expression Choice Rule must have a compare key")
end
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

      context "with an invalid compare key" do
        let(:payload) { {"Variable" => "$.foo", "InvalidCompare" => "$.bar", "Next" => "FirstMatchState"} }
        let(:input)   { {"foo" => 0, "bar" => 1} }

        it "fails" do
          expect { subject }.to raise_exception(Floe::InvalidWorkflowError)
        end
      end

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 yeah I added two more tests one with a typo'd key which would have succeeded on the prior regex approach, and one testing if you have multiple compare keys


context "with a typo'd compare key" do
let(:payload) { {"Variable" => "$.foo", "StringMatchez" => "bar", "Next" => "FirstMatchState"} }
let(:input) { {"foo" => "bar"} }

it "raises an exception" do
expect { subject }.to raise_exception(Floe::InvalidWorkflowError, "Data-test Expression Choice Rule must have a compare key")
end
end

context "with two compare keys" do
let(:payload) { {"Variable" => "$.foo", "StringMatches" => "bar", "NumericEquals" => 1, "Next" => "FirstMatchState"} }
let(:input) { {"foo" => "bar"} }

it "raises an exception" do
expect { subject }.to raise_exception(Floe::InvalidWorkflowError, "Data-test Expression Choice Rule must have only one compare key")
end
end

context "with IsNull" do
let(:payload) { {"Variable" => "$.foo", "IsNull" => true} }

Expand Down