From ce8de3ac639e1a2e3d0c43acdb7f556708fbb00b Mon Sep 17 00:00:00 2001 From: Keenan Brock Date: Tue, 17 Sep 2024 20:33:08 -0400 Subject: [PATCH] For Choice validation, use instance variables and not payload All classes currently populate the instance variables and then validate the state --- lib/floe/workflow/states/choice.rb | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/lib/floe/workflow/states/choice.rb b/lib/floe/workflow/states/choice.rb index 11780ab8..9f898d5a 100644 --- a/lib/floe/workflow/states/choice.rb +++ b/lib/floe/workflow/states/choice.rb @@ -9,10 +9,9 @@ class Choice < Floe::Workflow::State def initialize(workflow, name, payload) super - validate_state!(workflow) - - @choices = payload["Choices"].map.with_index { |choice, i| ChoiceRule.build(workflow, name + ["Choices", i.to_s], choice) } + @choices = payload["Choices"]&.map&.with_index { |choice, i| ChoiceRule.build(workflow, name + ["Choices", i.to_s], choice) } @default = payload["Default"] + validate_state!(workflow) @input_path = Path.new(payload.fetch("InputPath", "$")) @output_path = Path.new(payload.fetch("OutputPath", "$")) @@ -45,12 +44,12 @@ def validate_state!(workflow) end def validate_state_choices! - missing_field_error!("Choices") unless payload.key?("Choices") - invalid_field_error!("Choices", nil, "must be a non-empty array") unless payload["Choices"].kind_of?(Array) && !payload["Choices"].empty? + missing_field_error!("Choices") if @choices.nil? + invalid_field_error!("Choices", nil, "must be a non-empty array") unless @choices.kind_of?(Array) && !@choices.empty? end def validate_state_default!(workflow) - invalid_field_error!("Default", payload["Default"], "is not found in \"States\"") if payload["Default"] && !workflow_state?(payload["Default"], workflow) + invalid_field_error!("Default", @default, "is not found in \"States\"") if @default && !workflow_state?(@default, workflow) end end end