dry-validator Join the chat at https://gitter.im/dryrb/chat

Build Status Inline docs

A simple validator implemented in Ruby

Synopsis

# A simple validator
User = Struct.new(:name)
user = User.new('')
valid_user = User.new('Jack')

user_validator = Dry::Validator.new(name: { presence: true })
user_validator.call(user)
# => {:name=>[{:code=>"presence", :options=>true}]}

# Validate an embedded object using a nested rules hash
embedded_user_validator = Dry::Validator.new(
  user: {
    embedded: {
      name: { presence: true }
    }
  }
)
embedded_user_validator.call(user: user)
# => {:user=>[{:name=>[{:code=>"presence", :options=>true}]}]}

# Validate an embedded object using a nested validator
embedded_user_validator = Dry::Validator.new(
  user: {
    embedded: user_validator
  }
)
embedded_user_validator.call(user: user)
# => {:user=>[{:name=>[{:code=>"presence", :options=>true}]}]}

# Validate an array of objects using a nested rules hash
users_validator = Dry::Validator.new(
  users: {
    each: {
      name: { presence: true }
    }
  }
)
users_validator.call(users: [valid_user, user])
# => {:users=>[{}, {:name=>[{:code=>"presence", :options=>true}]}]}

# Validate an array of objects using a nested validator
users_validator = Dry::Validator.new(
  users: {
    each: user_validator
  }
)
users_validator.call(users: [valid_user, user])
# => {:users=>[{}, {:name=>[{:code=>"presence", :options=>true}]}]}

License

See LICENSE file.