Skip to content

Instantly share code, notes, and snippets.

@sean-abbott
Last active February 21, 2022 22:42
Show Gist options
  • Save sean-abbott/38d286d1da155a051b0d3cbadcefa8e4 to your computer and use it in GitHub Desktop.
Save sean-abbott/38d286d1da155a051b0d3cbadcefa8e4 to your computer and use it in GitHub Desktop.
Validation ideas
[1] pry(main)> load "validation_demo.rb"
=> true
[2] pry(main)> # The finance method
[3] pry(main)> he = HashExample.new
=> #<HashExample:0x0000564aaaebc558>
[4] pry(main)> he.check(true)
Airhorn would say All conditions are true!
=> true
[5] pry(main)> he.check(false)
Airhorn would say Something failed! I don't know what! Figure it out!
=> false
[6] pry(main)> # The FA && method
[7] pry(main)> ae = AndExample.new
=> #<AndExample:0x0000564aaa091320>
[8] pry(main)> ae.check(true)
=> true
[9] pry(main)> ae.check(false)
=> false
[10] pry(main)> # A validated take on the finance method
[11] pry(main)> vhe = ValidHashExample.new
=> #<ValidHashExample:0x0000564aaa2eede8>
[12] pry(main)> vhe.check(true)
All good
=> true
[13] pry(main)> vhe.check(false)
Airhorn would say List["Condition 1 was passed false", "Condition 2 was passed false"]
=> false
[14] pry(main)> # Finally one possible new way of doing things
[15] pry(main)> vae = ValidAndExample.new
=> #<ValidAndExample:0x0000564aaa7df668>
[16] pry(main)> params1 = {"b": true}
=> {:b=>true}
[17] pry(main)> params2 = {"b": false}
=> {:b=>false}
[18] pry(main)> params3 = {"business_rule_outcomes": true}
=> {:business_rule_outcomes=>true}
[19] pry(main)> params4 = {"business_rule_outcomes": false}
=> {:business_rule_outcomes=>false}
[20] pry(main)> vae.check(params1)
=> {:enhanced_b=>"b was true"}
[21] pry(main)> vae.check(params2)
=> {:enhanced_b=>"b was false"}
[22] pry(main)> vae.check(params3.merge(params1))
=> {:validation_results=>Dry::Monads::Result::Failure, :enhanced_b=>"b was true"}
[23] pry(main)> vae.check(params3.merge(params2))
=> {:validation_results=>List["Condition 1 was passed false", "Condition 2 was passed false"], :enhanced_b=>"b was false"}
[24] pry(main)> vae.check(params4.merge(params1))
=> {:enhanced_b=>"b was true"}
[25] pry(main)> vae.check(params4.merge(params2))
=> {:enhanced_b=>"b was false"}
require 'dry/monads'
module Conditions
def cond1?(b)
b
end
def cond2?(b)
b
end
def cond3?
true
end
end
module ValidConditions
include Dry::Monads[:list, :result, :validated, :do]
# The extra layer here is a little annoying.
# The attempt to cast a success with any failures immediates returns the failure
# Another option would be to get the result from the List::Validated and check the type
# or a rubyist might come up with other solutions.
def run_validations(b)
cond1, cond2, cond3 = yield List::Validated[
cond1?(b),
cond2?(b),
cond3?
].traverse.to_result
Success({
cond1: cond1,
cond2: cond2,
cond3: cond3
})
end
def cond1?(b)
b ? Valid(b) : Invalid("Condition 1 was passed false")
end
def cond2?(b)
b ? Valid(b) : Invalid("Condition 2 was passed false")
end
def cond3?
Valid(true)
end
end
class HashExample
include Conditions
def check(b)
conditions = {
cond1: cond1?(b),
cond2: cond2?(b),
cond3: cond3?
}
if conditions.values.all?
message = "All conditions are true!"
out = true
else
message = "Something failed! I don't know what! Figure it out!"
out = false
end
puts "Airhorn would say #{message}"
out
end
end
class ValidHashExample
include ValidConditions
# this is just for folks to play with
def validated_list(b)
yield List::Validated[
cond1?(b),
cond2?(b),
cond3?
].traverse.to_result
end
def check(b)
result = run_validations(b)
message = result.failure? ? "Airhorn would say #{result.failure}" : "All good"
puts message
result.success?
end
end
class AndExample
include Conditions
def check(b)
cond1?(b) && cond2?(b) && cond3?
end
end
# since the and exapmle doesn't even airhorn errors, I thought I'd offer a potential option
# based on the spotrates class that makes the call-ish.
class ValidAndExample
include ValidConditions
def check(params)
input = params[:b]
valid = run_validations(input)
result = {}
if params[:business_rule_outcomes]
result[:validation_results] = valid.failure? ? valid.failure : Failure
end
result[:enhanced_b] = "b was #{params[:b]}"
result
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment