Skip to content

Instantly share code, notes, and snippets.

@larribas
Created December 14, 2015 17:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save larribas/49caeba0f24777ebe17b to your computer and use it in GitHub Desktop.
Save larribas/49caeba0f24777ebe17b to your computer and use it in GitHub Desktop.
require 'dry-validation'
class Schema < Dry::Validation::Schema
group :node do
key(:name) { |name| name.str? and name.filled? }
key(:children) do |children|
children.array? do
children.each { |child| child.group?(:node) }
end
end
end
end
schema = Schema.new
puts schema.call({
name: 'Right',
children: [
{name: 'Right Son' children: []},
{name: 'Right Daughter', children: []}
]
}).messages
# => []
schema.call({
name: 'Wrong',
children: [
{name: 1, children: nil},
]
}).messages
# => [... one of the children does not comply with the rules]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment