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

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.



123
124
125
# File 'lib/dry/validation/contract/class_interface.rb', line 123

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")

Returns:



118
119
120
# File 'lib/dry/validation/contract/class_interface.rb', line 118

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

Returns:



32
33
34
# File 'lib/dry/validation/contract/class_interface.rb', line 32

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.



17
18
19
20
# File 'lib/dry/validation/contract/class_interface.rb', line 17

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

#json(*external_schemas, &block) ⇒ Dry::Schema::JSON, NilClass

Define a JSON schema for your contract

This type of schema is suitable for JSON data

Returns:

  • (Dry::Schema::JSON, NilClass)

See Also:



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

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

#macrosMacros::Container

Return macros registered for this class

Returns:



41
42
43
# File 'lib/dry/validation/contract/class_interface.rb', line 41

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

Returns:

  • (Dry::Schema::Messages)


143
144
145
# File 'lib/dry/validation/contract/class_interface.rb', line 143

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

#params(*external_schemas, &block) ⇒ Dry::Schema::Params, NilClass

Define a params schema for your contract

This type of schema is suitable for HTTP parameters

Returns:

  • (Dry::Schema::Params, NilClass)

See Also:



53
54
55
# File 'lib/dry/validation/contract/class_interface.rb', line 53

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

#register_macro(name, *args, &block) ⇒ self Originally defined in module Macros::Registrar

Register a macro

Examples:

register a global macro

Dry::Validation.register_macro(:even_numbers) do
  key.failure('all numbers must be even') unless values[key_name].all?(&:even?)
end

register a contract macro

class MyContract < Dry::Validation::Contract
  register_macro(:even_numbers) do
    key.failure('all numbers must be even') unless values[key_name].all?(&:even?)
  end
end

Parameters:

  • name (Symbol)

    The name of the macro

  • args (Array)

    Optional default positional arguments for the macro

Returns:

  • (self)

See Also:

#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

Returns:



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

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

  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

Returns:



132
133
134
135
136
# File 'lib/dry/validation/contract/class_interface.rb', line 132

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

#schema(*external_schemas, &block) ⇒ Dry::Schema::Processor, NilClass

Define a plain schema for your contract

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

Returns:

  • (Dry::Schema::Processor, NilClass)

See Also:



77
78
79
# File 'lib/dry/validation/contract/class_interface.rb', line 77

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