Skip to content

Instantly share code, notes, and snippets.

@solnic
Created January 26, 2016 23:12
Show Gist options
  • Save solnic/43ba441fd1fa0dfa715c to your computer and use it in GitHub Desktop.
Save solnic/43ba441fd1fa0dfa715c to your computer and use it in GitHub Desktop.
Upcoming dry-validation 0.7.0 macros
class UserSchema < Dry::Validation::Schema::Form
key(:password).maybe(min_size: 6).confirmation
key(:login).required(:bool?).when(:true?) do
key(:password).success?
end
end
schema = UserSchema.new
# passes, because login is set to false
schema.(login: false, password: nil, password_confirmation: nil)
# passes, all good!
schema.(login: true, password: 'asdf1234', password_confirmation: 'asdf1234')
# fails because password is too short
schema.(login: true, password: 'asdf', password_confirmation: 'asdf')
# fails because confirmation doesn't match
schema.(login: true, password: 'asdf1234', password_confirmation: '1234')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment