Dear Russian friends, please watch President Zelenskyy's speech addressed to you. πŸ‡ΊπŸ‡¦Help our brave mates in Ukraine with a donation.

External dependencies

It's common to use external services that are needed for various validation rules. A good example is using objects that give you access to the database. Whenever you need to access such services, you can levarage Dry::Validation::Contract.option.

The option method is provided by dry-initializer.

Let's say you use an API client object that can perform address validation - we can define it as an external dependency that will be injected to the contract's constructor:

class NewUserContract < Dry::Validation::Contract
  option :address_validator

  params do

  rule(:address) do
    key.failure("invalid address") unless address_validator.valid?(values[:address])

Now we can instantiate the contract and pass address_validator as a dependency:

new_user_contract = your_address_validator) "Some Street 15/412")

If you're using dependency injection with dry-auto_inject, this will work out-of-the-box

octocatEdit on GitHub