Module: Dry::Transaction::Extra::ValidationDSL
- Defined in:
- lib/dry/transaction/extra/validation_dsl.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#validate(contract = nil) ⇒ Object
Allows you to declare a class-level validator, and run it as the first step of the Transaction.
Class Method Details
.extended(klass) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/dry/transaction/extra/validation_dsl.rb', line 7 def self.extended(klass) klass.extend Dry::Core::ClassAttributes # The Dry::Validation Contract to run as the first step in the # Transaction. This exposes it publicly, so you can run it outside # the context of the Transction. This is useful if, for example, you # want to run the transaction in a job, but want to check if the # arguments are valid before enqueueing the job. # # @example # # class MyTransaction # validate do # params do # required(:name).filled(:string) # end # end # end # # MyTransaction.validator.new.call(name: "Jane") # # => #<Dry::Validation::Result{name: "Jane"} errors={}> klass.defines :validator # Allows overriding the default validation contract class. This is useful if you want to # use a different Contract class with a different configuration. # # @example # # module MyApp # module Types # include Dry.Types() # # Container = Dry::Schema::TypeContainer.new # Container.register("params.email", String.constrained(format: /@/)) # end # # class Contract < Dry::Validation::Contract # config.types = Types::Container # end # end # # module ApplicationTransaction # include Dry::Transaction # include Dry::Transaction::Extra # # load_extensions :validation # # validation_contract_class MyApp::Contract # end # # class MyTransaction # include ApplicationTransaction # # validate do # params do # # Now the custom `:email` type is available in this schema # required(:email).filled(:email) # end # end # end klass.defines :validation_contract_class require "dry/validation" Dry::Validation.load_extensions(:monads) end |
Instance Method Details
#validate(contract = nil) ⇒ Object
Allows you to declare a class-level validator, and run it as the first step of the Transaction.
class CreateUser
include Dry::Transaction
include Dry::Transaction::Extra
load_extensions :validation
validate do
params do
required(:name).filled(:string)
optional(:email).maybe(:string)
end
end
end
class NewUserContract < Dry::Validation::Contract
params do
required(:name).filled(:string)
optional(:email).maybe(:string)
end
end
class CreateUser
include Dry::Transaction
include Dry::Transaction::Extra
load_extensions :validation
validate NewUserContract
end
107 108 109 110 111 |
# File 'lib/dry/transaction/extra/validation_dsl.rb', line 107 def validate(contract = nil, &) validator(contract || Class.new(validation_contract_class || Dry::Validation::Contract, &)) valid(validator.new, name: "validate") end |