Skip to content

Commit

Permalink
Add choice_rule payload validation
Browse files Browse the repository at this point in the history
  • Loading branch information
agrare committed May 22, 2024
1 parent 94d0bf3 commit 88457eb
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
20 changes: 16 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,22 @@ module Floe
class Workflow
class ChoiceRule
class Data < Floe::Workflow::ChoiceRule
attr_reader :compare_key

def initialize(*)
super

allowed_compare_keys = %w[IsNull IsPresent IsNumeric IsString IsBoolean IsTimestamp StringMatches]
allowed_compare_keys += %w[String Numeric Boolean Timestamp].flat_map { |k| ["#{k}Equals", "#{k}EqualsPath"] }
allowed_compare_keys += %w[String Numeric Timestamp].flat_map { |k| ["#{k}LessThan", "#{k}LessThanPath", "#{k}GreaterThan", "#{k}GreaterThanPath", "#{k}LessThanEquals", "#{k}LessThanEqualsPath", "#{k}GreaterThanEquals", "#{k}GreaterThanEqualsPath"] }

compare_keys = payload.keys & allowed_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 +96,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
9 changes: 9 additions & 0 deletions spec/workflow/choice_rule_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,15 @@
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

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

Expand Down

0 comments on commit 88457eb

Please sign in to comment.