Class: Dry::Validation::Contract
- Inherits:
-
Object
- Object
- Dry::Validation::Contract
- Extended by:
- Initializer, ClassInterface
- Defined in:
- lib/dry/validation/contract.rb,
lib/dry/validation/contract/class_interface.rb,
lib/dry/validation/extensions/predicates_as_macros.rb
Overview
Extension to use dry-logic predicates as macros.
Defined Under Namespace
Modules: ClassInterface
Instance Attribute Summary collapse
-
#config ⇒ Config
readonly
Contract's configuration object.
-
#default_context ⇒ Hash
readonly
Default context for rules.
-
#macros ⇒ Macros::Container
readonly
Configured macros.
- #message_resolver ⇒ Messages::Resolver readonly private
- #rules ⇒ Hash readonly private
- #schema ⇒ Dry::Schema::Params, ... readonly private
Class Method Summary collapse
- .__schema__ ⇒ Object extended from ClassInterface private
-
.build(options = EMPTY_HASH, &block) ⇒ Contract
extended
from ClassInterface
A shortcut that can be used to define contracts that won't be reused or inherited.
-
.config ⇒ Config
extended
from ClassInterface
Configuration.
-
.import_predicates_as_macros ⇒ Object
Make macros available for self and its descendants.
- .inherited(klass) ⇒ Object extended from ClassInterface private
-
.json(*external_schemas, &block) ⇒ Dry::Schema::JSON, NilClass
extended
from ClassInterface
Define a JSON schema for your contract.
-
.macros ⇒ Macros::Container
extended
from ClassInterface
Return macros registered for this class.
-
.messages ⇒ Dry::Schema::Messages
extended
from ClassInterface
private
Return messages configured for this class.
-
.params(*external_schemas, &block) ⇒ Dry::Schema::Params, NilClass
extended
from ClassInterface
Define a params schema for your contract.
-
.register_macro(name, *args, &block) ⇒ self
extended
from Macros::Registrar
Register a macro.
-
.rule(*keys, &block) ⇒ Rule
extended
from ClassInterface
Define a rule for your contract.
-
.rules ⇒ Array<Rule>
extended
from ClassInterface
private
Return rules defined in this class.
-
.schema(*external_schemas, &block) ⇒ Dry::Schema::Processor, NilClass
extended
from ClassInterface
Define a plain schema for your contract.
Instance Method Summary collapse
-
#call(input, context = EMPTY_HASH) ⇒ Result
Apply the contract to an input.
-
#inspect ⇒ String
Return a nice string representation.
Instance Attribute Details
#config ⇒ Config (readonly)
Returns Contract's configuration object.
55 |
# File 'lib/dry/validation/contract.rb', line 55 option :config, default: -> { self.class.config } |
#default_context ⇒ Hash (readonly)
Returns Default context for rules.
66 |
# File 'lib/dry/validation/contract.rb', line 66 option :default_context, default: -> { EMPTY_HASH } |
#macros ⇒ Macros::Container (readonly)
Returns Configured macros.
61 |
# File 'lib/dry/validation/contract.rb', line 61 option :macros, default: -> { config.macros } |
#message_resolver ⇒ Messages::Resolver (readonly)
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.
81 |
# File 'lib/dry/validation/contract.rb', line 81 option :message_resolver, default: -> { Messages::Resolver.new() } |
#rules ⇒ Hash (readonly)
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.
76 |
# File 'lib/dry/validation/contract.rb', line 76 option :rules, default: -> { self.class.rules } |
#schema ⇒ Dry::Schema::Params, ... (readonly)
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.
71 |
# File 'lib/dry/validation/contract.rb', line 71 option :schema, default: -> { self.class.__schema__ || raise(SchemaMissingError, self.class) } |
Class Method Details
.__schema__ ⇒ Object Originally defined in module ClassInterface
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.
.build(options = EMPTY_HASH, &block) ⇒ Contract Originally defined in module ClassInterface
A shortcut that can be used to define contracts that won't be reused or inherited
.config ⇒ Config Originally defined in module ClassInterface
Configuration
.import_predicates_as_macros ⇒ Object
Make macros available for self and its descendants.
58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/dry/validation/extensions/predicates_as_macros.rb', line 58 def self.import_predicates_as_macros registry = PredicateRegistry.new PredicateRegistry::WHITELIST.each do |name| register_macro(name) do |macro:| predicate_args = [*macro.args, value] = registry.(name, predicate_args) key.failure(name, ) unless registry.(name, predicate_args) end end end |
.inherited(klass) ⇒ Object Originally defined in module ClassInterface
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.
.json(*external_schemas, &block) ⇒ Dry::Schema::JSON, NilClass Originally defined in module ClassInterface
Define a JSON schema for your contract
This type of schema is suitable for JSON data
.macros ⇒ Macros::Container Originally defined in module ClassInterface
Return macros registered for this class
.messages ⇒ Dry::Schema::Messages Originally defined in module ClassInterface
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
.params(*external_schemas, &block) ⇒ Dry::Schema::Params, NilClass Originally defined in module ClassInterface
Define a params schema for your contract
This type of schema is suitable for HTTP parameters
.register_macro(name, *args, &block) ⇒ self Originally defined in module Macros::Registrar
Register a macro
.rule(*keys, &block) ⇒ Rule Originally defined in module ClassInterface
Define a rule for your contract
.rules ⇒ Array<Rule> Originally defined in module ClassInterface
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
.schema(*external_schemas, &block) ⇒ Dry::Schema::Processor, NilClass Originally defined in module ClassInterface
Define a plain schema for your contract
This type of schema does not offer coercion out of the box
Instance Method Details
#call(input, context = EMPTY_HASH) ⇒ Result
Apply the contract to an input
rubocop: disable Metrics/AbcSize
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/dry/validation/contract.rb', line 92 def call(input, context = EMPTY_HASH) validate_input_type(input) context_map = Concurrent::Map.new.tap do |map| default_context.each { |key, value| map[key] = value } context.each { |key, value| map[key] = value } end Result.new(schema.(input), context_map) do |result| rules.each do |rule| next if rule.keys.any? { |key| error?(result, key) } rule_result = rule.(self, result) rule_result.failures.each do |failure| result.add_error(.(**failure)) end end end end |
#inspect ⇒ String
Return a nice string representation
119 120 121 |
# File 'lib/dry/validation/contract.rb', line 119 def inspect %(#<#{self.class} schema=#{schema.inspect} rules=#{rules.inspect}>) end |