Skip to content

Instantly share code, notes, and snippets.

@k-hamada
Created April 9, 2018 06:46
Show Gist options
  • Save k-hamada/1f67554e0346a7e9ed1d1549b4309b70 to your computer and use it in GitHub Desktop.
Save k-hamada/1f67554e0346a7e9ed1d1549b4309b70 to your computer and use it in GitHub Desktop.
require 'bundler/inline'
gemfile(true) do
source 'https://rubygems.org'
gem 'dry-validation'
end
module MyPredicates1
include Dry::Logic::Predicates
predicate(:one?) do |value|
value == 1
end
end
module MyPredicates2
include Dry::Logic::Predicates
predicate(:two?) do |value|
value == 2
end
end
schema = Dry::Validation.Schema do
configure do
predicates(MyPredicates1)
predicates(MyPredicates2)
def self.messages
super.merge(
en: { errors: {
one?: '%{value} not 1',
two?: '%{value} not 2'
} }
)
end
end
required(:x).filled(:int?, :one?)
required(:x).filled(:int?, :two?)
end
p schema.({})
p schema.(x: 1)
p schema.(x: 2)
p schema.(x: 3)
@k-hamada
Copy link
Author

k-hamada commented Apr 9, 2018

~/.rbenv/versions/2.4.3/lib/ruby/gems/2.4.0/gems/dry-validation-0.11.1/lib/dry/validation/predicate_registry.rb:106:in `raise_unknown_predicate_error': +one?+ is not a valid predicate name (ArgumentError)
	from ~/.rbenv/versions/2.4.3/lib/ruby/gems/2.4.0/gems/dry-validation-0.11.1/lib/dry/validation/predicate_registry.rb:99:in `ensure_valid_predicate'
	from ~/.rbenv/versions/2.4.3/lib/ruby/gems/2.4.0/gems/dry-validation-0.11.1/lib/dry/validation/schema/value.rb:156:in `node'
	from ~/.rbenv/versions/2.4.3/lib/ruby/gems/2.4.0/gems/dry-validation-0.11.1/lib/dry/validation/schema/rule.rb:193:in `key'
	from ~/.rbenv/versions/2.4.3/lib/ruby/gems/2.4.0/gems/dry-validation-0.11.1/lib/dry/validation/schema/rule.rb:169:in `block in infer_predicates'
	from ~/.rbenv/versions/2.4.3/lib/ruby/gems/2.4.0/gems/dry-validation-0.11.1/lib/dry/validation/schema/rule.rb:163:in `map'
	from ~/.rbenv/versions/2.4.3/lib/ruby/gems/2.4.0/gems/dry-validation-0.11.1/lib/dry/validation/schema/rule.rb:163:in `infer_predicates'
	from ~/.rbenv/versions/2.4.3/lib/ruby/gems/2.4.0/gems/dry-validation-0.11.1/lib/dry/validation/schema/rule.rb:65:in `filled'
	from ./multi-predicates.rb:41:in `block in <main>'
	from ~/.rbenv/versions/2.4.3/lib/ruby/gems/2.4.0/gems/dry-validation-0.11.1/lib/dry/validation/schema/class_interface.rb:48:in `instance_exec'
	from ~/.rbenv/versions/2.4.3/lib/ruby/gems/2.4.0/gems/dry-validation-0.11.1/lib/dry/validation/schema/class_interface.rb:48:in `define'
	from ~/.rbenv/versions/2.4.3/lib/ruby/gems/2.4.0/gems/dry-validation-0.11.1/lib/dry/validation.rb:18:in `Schema'
  from ./multi-predicates.rb:26:in `<main>'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment