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] Choice rule payload validation #189

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
16 changes: 8 additions & 8 deletions lib/floe/workflow/choice_rule/data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ class ChoiceRule
class Data < Floe::Workflow::ChoiceRule
TYPES = ["String", "Numeric", "Boolean", "Timestamp", "Present", "Null"].freeze
COMPARES = ["Equals", "LessThan", "GreaterThan", "LessThanEquals", "GreaterThanEquals", "Matches"].freeze
OPERATIONS = TYPES.each_with_object({}) { |dt, a| a[dt] = "is_#{dt.downcase}?".to_sym } \
.merge(COMPARES.each_with_object({}) { |op, a| a[op] = "#{op.downcase}?".to_sym }).freeze
# e.g.: (Is)(String), (Is)(Present)
TYPE_CHECK = /^(Is)(#{TYPES.join("|")})$/.freeze
# e.g.: (String)(LessThan)(Path), (Numeric)(GreaterThanEquals)()
OPERATION = /^(#{(TYPES - %w[Null Present]).join("|")})(#{COMPARES.join("|")})(Path)?$/.freeze

attr_reader :variable, :compare_key, :operation, :type, :compare_predicate, :path
attr_reader :variable, :compare_key, :operator, :type, :compare_predicate, :path

def initialize(_workflow, _name, payload)
super
Expand All @@ -25,7 +27,7 @@ def true?(context, input)

lhs = variable_value(context, input)
rhs = compare_value(context, input)
send(operation, lhs, rhs)
send(OPERATIONS[operator], lhs, rhs)
end

private
Expand Down Expand Up @@ -110,23 +112,21 @@ def parse_compare_key
# e.g. (String)(GreaterThan)(Path)
if (match_values = OPERATION.match(key))
@compare_key = key
@type, operator, @path = match_values.captures
@operation = "#{operator.downcase}?".to_sym
@type, @operator, @path = match_values.captures
@compare_predicate = parse_predicate(type)
break
end
# e.g. (Is)(String)
if (match_value = TYPE_CHECK.match(key))
@compare_key = key
_operator, type = match_value.captures
_is, @operator = match_value.captures
# type: nil means no runtime type checking.
@type = @path = nil
@operation = "is_#{type.downcase}?".to_sym
@compare_predicate = parse_predicate("Boolean")
break
end
end
parser_error!("requires a compare key") if compare_key.nil? || operation.nil?
parser_error!("requires a compare key") if compare_key.nil? || operator.nil?
end

# parse predicate at initilization time
Expand Down Expand Up @@ -173,7 +173,7 @@ def fetch_path(field_name, field_path, context, input)
# if we have runtime checking, check against that type
# otherwise assume checking a TYPE_CHECK predicate and check against Boolean
def correct_type?(value, data_type)
send("is_#{data_type.downcase}?".to_sym, value)
send(OPERATIONS[data_type], value)
end
end
end
Expand Down