Class: Dry::Transaction::Extra::Steps::Valid

Inherits:
Object
  • Object
show all
Defined in:
lib/dry/transaction/extra/steps/valid.rb

Overview

Runs a dry-schema or dry-validation Contract, either passed to the step directly, or returned from the step method. It runs the validator on the input arguments, and returns Success on the validator output, or the Failure with errors returned from the validator.

valid :validate_params

def validate_params(params)

Dry::Schema.Params do
  required(:name).filled(:string)
  required(:email).filled(:string)
end

end

This is essentially equivalent to:

step :validate_params

def validate_params(params)

Dry::Schema.Params do
  required(:name).filled(:string)
  required(:email).filled(:string)
end.call(params).to_monad

end

valid ParamsValidator

Examples:

Validator defined in step implementation

Validator provided to the step

Defined Under Namespace

Modules: DSL

Instance Method Summary collapse

Instance Method Details

#call(operation, _options, args) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/dry/transaction/extra/steps/valid.rb', line 42

def call(operation, _options, args)
  if args.size > 1 || (!args[0].is_a?(Hash) && !args[0].nil?)
    raise ArgumentError,
          "the valid step only works with hash/keyword arguments"
  end

  # The operation is a callable that returns a schema/contract, which
  # itself needs to be called
  result = operation.call.call(args[0])
  if result.success?
    Success(result.to_h)
  else
    Failure(result)
  end
end