Skip to content

Instantly share code, notes, and snippets.

@k-hamada
Created April 11, 2018 10:41
Show Gist options
  • Save k-hamada/89f890e7b90d03b47e8a8a1feed3f7d6 to your computer and use it in GitHub Desktop.
Save k-hamada/89f890e7b90d03b47e8a8a1feed3f7d6 to your computer and use it in GitHub Desktop.
require 'bundler/inline'
gemfile(true) do
source 'https://rubygems.org'
gem 'dry-validation'
end
schema = Dry::Validation.Schema do
optional(:y).filled(:int?)
optional(:z).filled(:odd?)
optional(:x).filled(:int?)
.when(eql?: 1) {
required(:y).filled(:odd?)
}
end
p schema.({})
p schema.({x: 1})
p schema.({x: 2})
p schema.({x: 1, y: 1})
p schema.({x: 2, y: 1})
p schema.({x: 1, y: 2})
p schema.({x: 2, y: 2})
p schema.({z: 1})
p schema.({z: 2})
@k-hamada
Copy link
Author

#<Dry::Validation::Result output={} errors={}>
#<Dry::Validation::Result output={:x=>1} errors={:y=>["is missing"]}>
#<Dry::Validation::Result output={:x=>2} errors={}>
#<Dry::Validation::Result output={:x=>1, :y=>1} errors={}>
#<Dry::Validation::Result output={:x=>2, :y=>1} errors={}>
#<Dry::Validation::Result output={:x=>1, :y=>2} errors={:y=>["must be odd"]}>
#<Dry::Validation::Result output={:x=>2, :y=>2} errors={}>
#<Dry::Validation::Result output={:z=>1} errors={}>
#<Dry::Validation::Result output={:z=>2} errors={:z=>["must be odd"]}>

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