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
- #__schema__ ⇒ Object private
-
#build(options = EMPTY_HASH, &block) ⇒ Contract
A shortcut that can be used to define contracts that won't be reused or inherited.
-
#config ⇒ Config
Configuration.
- #inherited(klass) ⇒ Object private
-
#json(*external_schemas, &block) ⇒ Dry::Schema::JSON, NilClass
Define a JSON schema for your contract.
-
#macros ⇒ Macros::Container
Return macros registered for this class.
-
#messages ⇒ Dry::Schema::Messages
private
Return messages configured for this class.
-
#params(*external_schemas, &block) ⇒ Dry::Schema::Params, NilClass
Define a params schema for your contract.
-
#register_macro(name, *args, &block) ⇒ self
included
from Macros::Registrar
Register a macro.
-
#rule(*keys, &block) ⇒ Rule
Define a rule for your contract.
-
#rules ⇒ Array<Rule>
private
Return rules defined in this class.
-
#schema(*external_schemas, &block) ⇒ Dry::Schema::Processor, NilClass
Define a plain schema for your contract.
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.
130 131 132 |
# File 'lib/dry/validation/contract/class_interface.rb', line 130 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
125 126 127 |
# File 'lib/dry/validation/contract/class_interface.rb', line 125 def build( = EMPTY_HASH, &block) Class.new(self, &block).new(**) end |
#config ⇒ Config
Configuration
39 40 41 |
# File 'lib/dry/validation/contract/class_interface.rb', line 39 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.
24 25 26 27 |
# File 'lib/dry/validation/contract/class_interface.rb', line 24 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
72 73 74 |
# File 'lib/dry/validation/contract/class_interface.rb', line 72 def json(*external_schemas, &block) define(:JSON, external_schemas, &block) end |
#macros ⇒ Macros::Container
Return macros registered for this class
48 49 50 |
# File 'lib/dry/validation/contract/class_interface.rb', line 48 def macros config.macros end |
#messages ⇒ Dry::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
150 151 152 |
# File 'lib/dry/validation/contract/class_interface.rb', line 150 def @messages ||= Schema::Messages.setup(config.) 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
60 61 62 |
# File 'lib/dry/validation/contract/class_interface.rb', line 60 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
#rule(*keys, &block) ⇒ Rule
Define a rule for your contract
103 104 105 106 107 108 109 |
# File 'lib/dry/validation/contract/class_interface.rb', line 103 def rule(*keys, &block) ensure_valid_keys(*keys) if __schema__ Rule.new(keys: keys, block: block).tap do |rule| rules << rule end end |
#rules ⇒ Array<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
139 140 141 142 143 |
# File 'lib/dry/validation/contract/class_interface.rb', line 139 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
84 85 86 |
# File 'lib/dry/validation/contract/class_interface.rb', line 84 def schema(*external_schemas, &block) define(:schema, external_schemas, &block) end |