Skip to content

Instantly share code, notes, and snippets.

@eduardodeoh
Last active May 25, 2016 18:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eduardodeoh/f26735d2edb771f5bd1149035ac337c8 to your computer and use it in GitHub Desktop.
Save eduardodeoh/f26735d2edb771f5bd1149035ac337c8 to your computer and use it in GitHub Desktop.
dry-validation tests
module ControlKpValidator
# Validation Flow that i need in sequence:
# 1 - uniqueness validation from hash keys (key_phrase) presents here:
# key_phrases: [ { key_phrase: "ph1", weight: 5.3 }, { key_phrase: "ph1", weight: 6.2 } ]
# 2 - value from hash keys (weight) must be between 0 and 4
# key_phrases: [ { key_phrase: "ph1", weight: 5.3 }, { key_phrase: "ph1", weight: 6.2 } ]
# 3 - hash keys "weight" sum must be less than an external value
SCHEMA1 = Dry::Validation.Schema do
configure do
config.namespace = :control_kp
config.messages_file = 'config/locales/validations/errors.yml'
config.input_processor = :sanitizer
def unique?(value)
value.group_by { |kp| kp[:key_phrase] }.values.select { |kp| kp.size > 1 }.empty?
end
end
required(:name).filled
required(:control_id).filled
required(:user_id).filled
required(:implementation).filled
# I need to validate that this array of hashes, not contains hash keys with repeated values, like:
# [ {key_phrase: "ph1"}, {key_phrase: "ph1"} ]
required(:key_phrases).value(:unique?)
# After validation above, I need to validate that each key "weight" inside each hash, has a value between 0 and 4
required(:key_phrases).each do
schema do
required(:weight).filled(gteq?: 0, lteq?: 4)
required(:key_phrase).filled
end
end
end
SCHEMA2 = Dry::Validation.Schema do
configure do
config.namespace = :bap_control_kp
config.messages_file = 'config/locales/validations/errors.yml'
config.input_processor = :sanitizer
def unique?(value)
value.group_by { |kp| kp[:key_phrase] }.values.select { |kp| kp.size > 1 }.empty?
end
end
required(:name).filled
required(:control_id).filled
required(:user_id).filled
required(:implementation).filled
required(:key_phrases).each do
schema do
required(:weight).filled
required(:key_phrase).filled
end
end
# I need to validate that this array of hashes, not contains hash keys with repeated values, like:
# [ {key_phrase: "ph1"}, {key_phrase: "ph1"} ]
# After validation above, I need to validate that each key "weight" inside each hash, has a value between 0 and 4
rule(key_phrases: [[:key_phrases, :weight], :key_phrases]) do |weight, key_phrases|
key_phrases.when(:unique?) do
weight.gteq?(0) & weight.lteq?(4)
end
end
end
end
en:
errors:
rules:
control_kp:
rules:
key_phrases:
unique?: "repeated key_phrase value"
weight:
weight_valid?: "invalid weight value"
module JsonPayload
def example_repeated_keys
{
name: "name",
user_id: "01dd7006-8b96-48e8-954e-5a9da4cd72e0",
bap_control_id: "01dd7006-8b96-48e8-954e-5a9da4cd72e0",
implementation: "imp lang",
key_phrases: [
{
key_phrase: "ph1",
weight: 5.3
},
{
key_phrase: "ph1",
weight: 6.2
}
]
}
end
def example_normal
{
name: "name",
user_id: "01dd7006-8b96-48e8-954e-5a9da4cd72e0",
bap_control_id: "01dd7006-8b96-48e8-954e-5a9da4cd72e0",
implementation: "imp lang",
key_phrases: [
{
key_phrase: "ph1",
weight: 5.3
},
{
key_phrase: "ph2",
weight: 6.2
}
]
}
end
end
include ControlKpValidator
include JsonPayload
SCHEMA1.(example_normal).messages# expect errors here, but only {}
SCHEMA1.(example_repeated_keys).messages # expect errors here, but only {}
SCHEMA2.(example_normal).messages # expect errors here, but
#TypeError: no implicit conversion of Symbol into Integer
#from /Users/eduardo/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/dry-logic-0.2.3/lib/dry/logic/evaluator.rb:29:in `[]'
SCHEMA2.(example_repeated_keys).messages # expect errors here, but
#TypeError: no implicit conversion of Symbol into Integer
#from /Users/eduardo/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/dry-logic-0.2.3/lib/dry/logic/evaluator.rb:29:in `[]'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment