Created
April 9, 2018 06:46
-
-
Save k-hamada/1f67554e0346a7e9ed1d1549b4309b70 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Author
k-hamada
commented
Apr 9, 2018
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment