Skip to content

[WIP] Add choice_rule payload validation#187

Closed
agrare wants to merge 1 commit into
ManageIQ:masterfrom
agrare:choice_rule_payload_validation
Closed

[WIP] Add choice_rule payload validation#187
agrare wants to merge 1 commit into
ManageIQ:masterfrom
agrare:choice_rule_payload_validation

Conversation

@agrare
Copy link
Copy Markdown
Member

@agrare agrare commented May 22, 2024

No description provided.

@agrare agrare requested a review from Fryguy as a code owner May 22, 2024 20:29
Comment thread lib/floe/workflow/choice_rule/data.rb Outdated

def initialize(*)
super
@compare_key = payload.keys.detect { |key| key.match?(/^(#{COMPARE_KEYS.join("|")})/) }
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

TODO I don't like the regular expression matcher here because it actually matches on typos like "StringEqualz" (this was my first spec). This is fine when we have the big case compare_key in true? since the else raises an exception but I want it to be stricter in the payload initialize validation.

@miq-bot miq-bot added the wip label May 22, 2024
@agrare agrare force-pushed the choice_rule_payload_validation branch from 0b56c03 to 88457eb Compare May 22, 2024 20:39
@agrare agrare force-pushed the choice_rule_payload_validation branch 2 times, most recently from a8e11cd to da2c0c7 Compare May 22, 2024 20:42
@agrare agrare force-pushed the choice_rule_payload_validation branch from da2c0c7 to fef939e Compare May 22, 2024 20:45
@miq-bot
Copy link
Copy Markdown
Member

miq-bot commented May 22, 2024

Checked commit agrare@fef939e with ruby 2.7.8, rubocop 1.56.3, haml-lint 0.51.0, and yamllint
2 files checked, 1 offense detected

lib/floe/workflow/choice_rule/data.rb

  • ❗ - Line 12, Col 55 - Performance/CollectionLiteralInLoop - Avoid immutable Array literals in loops. It is better to extract it into a local variable or a constant.

Copy link
Copy Markdown
Member

@kbrock kbrock left a comment

Choose a reason for hiding this comment

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

I like the attr_reader approach. very nice

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
Copy Markdown
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
Copy Markdown
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

Comment on lines +9 to +13
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
Copy link
Copy Markdown
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
Copy Markdown
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

@kbrock kbrock mentioned this pull request May 22, 2024
11 tasks
@miq-bot
Copy link
Copy Markdown
Member

miq-bot commented Aug 13, 2024

This pull request is not mergeable. Please rebase and repush.

@miq-bot
Copy link
Copy Markdown
Member

miq-bot commented Nov 25, 2024

This pull request has been automatically marked as stale because it has not been updated for at least 3 months.

If these changes are still valid, please remove the stale label, make any changes requested by reviewers (if any), and ensure that this issue is being looked at by the assigned/reviewer(s).

2 similar comments
@miq-bot
Copy link
Copy Markdown
Member

miq-bot commented Mar 3, 2025

This pull request has been automatically marked as stale because it has not been updated for at least 3 months.

If these changes are still valid, please remove the stale label, make any changes requested by reviewers (if any), and ensure that this issue is being looked at by the assigned/reviewer(s).

@miq-bot
Copy link
Copy Markdown
Member

miq-bot commented Jun 9, 2025

This pull request has been automatically marked as stale because it has not been updated for at least 3 months.

If these changes are still valid, please remove the stale label, make any changes requested by reviewers (if any), and ensure that this issue is being looked at by the assigned/reviewer(s).

@kbrock
Copy link
Copy Markdown
Member

kbrock commented Sep 10, 2025

@agrare Has this already been covered by #189 and friends?

@agrare agrare closed this Sep 12, 2025
@agrare agrare deleted the choice_rule_payload_validation branch September 12, 2025 15:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants