Created
December 14, 2015 17:10
-
-
Save larribas/49caeba0f24777ebe17b 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 '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