Skip to content

Commit

Permalink
For Choice validation, use instance variables and not payload
Browse files Browse the repository at this point in the history
All classes currently populate the instance variables and then validate the state
  • Loading branch information
kbrock committed Sep 18, 2024
1 parent 415d9e0 commit ce8de3a
Showing 1 changed file with 5 additions and 6 deletions.
11 changes: 5 additions & 6 deletions lib/floe/workflow/states/choice.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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", "$"))
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit ce8de3a

Please sign in to comment.