Module: Dry::Validation::Contract::ClassInterface

Includes:
Macros::Registrar
Included in:
Dry::Validation::Contract
Defined in:
lib/dry/validation/contract/class_interface.rb

Overview

Contract’s class interface

Instance Method Summary collapse

Methods included from Macros::Registrar

#register_macro

Instance Method Details

#__schema__Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



155
156
157
# File 'lib/dry/validation/contract/class_interface.rb', line 155

def __schema__
  @__schema__ if defined?(@__schema__)
end

#build(options = EMPTY_HASH, &block) ⇒ Contract

A shortcut that can be used to define contracts that won’t be reused or inherited

Examples:

my_contract = Dry::Validation::Contract.build do
  params do
    required(:name).filled(:string)
  end
end

my_contract.call(name: "Jane")


150
151
152
# File 'lib/dry/validation/contract/class_interface.rb', line 150

def build(options = EMPTY_HASH, &block)
  Class.new(self, &block).new(options)
end

#configConfig

Configuration

Examples:

class MyContract < Dry::Validation::Contract
  config.messages.backend = :i18n
end


64
65
66
# File 'lib/dry/validation/contract/class_interface.rb', line 64

def config
  @config ||= Validation::Config.new
end

#inherited(klass) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



49
50
51
52
# File 'lib/dry/validation/contract/class_interface.rb', line 49

def inherited(klass)
  super
  klass.instance_variable_set('@config', config.dup)
end

#json(&block) ⇒ Dry::Schema::JSON

Define a JSON schema for your contract

This type of schema is suitable for JSON data



97
98
99
# File 'lib/dry/validation/contract/class_interface.rb', line 97

def json(&block)
  define(:JSON, &block)
end

#macrosMacros::Container

Return macros registered for this class



73
74
75
# File 'lib/dry/validation/contract/class_interface.rb', line 73

def macros
  config.macros
end

#messagesDry::Schema::Messages

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return messages configured for this class



175
176
177
# File 'lib/dry/validation/contract/class_interface.rb', line 175

def messages
  @messages ||= Schema::Messages.setup(config.messages)
end

#params(&block) ⇒ Dry::Schema::Params

Define a params schema for your contract

This type of schema is suitable for HTTP parameters



85
86
87
# File 'lib/dry/validation/contract/class_interface.rb', line 85

def params(&block)
  define(:Params, &block)
end

#rule(*keys, &block) ⇒ Rule

Define a rule for your contract

Examples:

using a symbol

rule(:age) do
  failure('must be at least 18') if values[:age] < 18
end

using a path to a value and a custom predicate

rule('address.street') do
  failure('please provide a valid street address') if valid_street?(values[:street])
end


128
129
130
131
132
133
134
# File 'lib/dry/validation/contract/class_interface.rb', line 128

def rule(*keys, &block)
  ensure_valid_keys(*keys)

  Rule.new(keys: keys, block: block).tap do |rule|
    rules << rule
  end
end

#rulesArray<Rule>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return rules defined in this class



164
165
166
167
168
# File 'lib/dry/validation/contract/class_interface.rb', line 164

def rules
  @rules ||= EMPTY_ARRAY
    .dup
    .concat(superclass.respond_to?(:rules) ? superclass.rules : EMPTY_ARRAY)
end

#schema(&block) ⇒ Dry::Schema::Processor

Define a plain schema for your contract

This type of schema does not offer coercion out of the box



109
110
111
# File 'lib/dry/validation/contract/class_interface.rb', line 109

def schema(&block)
  define(:schema, &block)
end