Class: Dry::Validation::Rust::Contract
- Inherits:
-
Object
- Object
- Dry::Validation::Rust::Contract
- Defined in:
- lib/dry/validation/rust/contract.rb,
lib/dry/validation/rust/contract/result.rb,
lib/dry/validation/rust/contract/values.rb
Overview
A validation contract that combines a compiled schema with ordered rules.
Defined Under Namespace
Classes: OptionDefinition, Result, Values
Constant Summary collapse
- Undefined =
Object.new.freeze
Instance Attribute Summary collapse
-
#default_context ⇒ Hash
readonly
Returns context merged into every call to this contract.
Class Method Summary collapse
-
.build(options = {}) { ... } ⇒ Contract
Builds an anonymous contract instance, optionally configured by a block.
-
.config ⇒ Config
Returns this contract class's configuration.
-
.import_predicates_as_macros ⇒ Class
Enables supported predicates to be resolved as rule macros.
-
.inherited(child) ⇒ void
Copies schema configuration and macros when a contract is inherited.
-
.json(*external_schemas) { ... } ⇒ Schema
Defines or returns a JSON-mode schema for this contract.
-
.macro_registry ⇒ MacroRegistry
private
Returns this contract class's macro registry.
-
.option(name, default: Undefined, optional: false, **_options) ⇒ Class
Declares an injected contract option and its default behavior.
-
.option_definitions ⇒ Hash{Symbol => OptionDefinition}
Returns option definitions inherited by this contract class.
-
.own_rules ⇒ Array<Rule>
Returns rules declared directly on this contract class.
-
.params(*external_schemas) { ... } ⇒ Schema
Defines or returns a Params-mode schema for this contract.
-
.register_macro(name) { ... } ⇒ Class
Registers a macro available to rules on this contract class.
-
.rule(*specs) { ... } ⇒ Rule
Registers a validation rule for one or more schema paths.
-
.rules ⇒ Array<Rule>
Returns inherited and locally declared rules in execution order.
-
.schema(*external_schemas) { ... } ⇒ Schema
Defines or returns a schema-mode schema for this contract.
-
.schema_definition ⇒ Schema?
Returns the compiled schema declared by this class or an ancestor.
Instance Method Summary collapse
-
#[](input, context = {}) ⇒ Result
Validates input with bracket syntax; equivalent to #call.
-
#call(input, context = {}) ⇒ Result
Validates input and returns a finalized result, including rule failures.
-
#initialize(default_context: {}, **options) ⇒ Contract
constructor
Creates a contract with optional default context and declared options.
-
#inspect ⇒ String
Returns a diagnostic representation of the compiled contract.
-
#macro_registered?(name) ⇒ Boolean
private
Returns whether a macro can be resolved by this contract.
-
#resolve_macro(name) ⇒ Macro
private
Resolves a registered macro by name.
Constructor Details
#initialize(default_context: {}, **options) ⇒ Contract
Creates a contract with optional default context and declared options.
253 254 255 256 |
# File 'lib/dry/validation/rust/contract.rb', line 253 def initialize(default_context: {}, **) @default_context = default_context () end |
Instance Attribute Details
#default_context ⇒ Hash (readonly)
Returns context merged into every call to this contract.
245 246 247 |
# File 'lib/dry/validation/rust/contract.rb', line 245 def default_context @default_context end |
Class Method Details
.build(options = {}) { ... } ⇒ Contract
Builds an anonymous contract instance, optionally configured by a block.
186 187 188 |
# File 'lib/dry/validation/rust/contract.rb', line 186 def build( = {}, &) Class.new(self, &).new(**) end |
.config ⇒ Config
Returns this contract class's configuration.
41 42 43 |
# File 'lib/dry/validation/rust/contract.rb', line 41 def config @config ||= Config.new end |
.import_predicates_as_macros ⇒ Class
Enables supported predicates to be resolved as rule macros.
172 173 174 175 |
# File 'lib/dry/validation/rust/contract.rb', line 172 def import_predicates_as_macros @predicates_as_macros = true self end |
.inherited(child) ⇒ void
This method returns an undefined value.
Copies schema configuration and macros when a contract is inherited.
32 33 34 35 36 |
# File 'lib/dry/validation/rust/contract.rb', line 32 def inherited(child) super child.instance_variable_set(:@config, config.dup) child.instance_variable_set(:@macro_registry, MacroRegistry.new(macro_registry)) end |
.json(*external_schemas) { ... } ⇒ Schema
Defines or returns a JSON-mode schema for this contract.
67 68 69 |
# File 'lib/dry/validation/rust/contract.rb', line 67 def json(*external_schemas, &) define_schema(:json, external_schemas, &) end |
.macro_registry ⇒ MacroRegistry
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.
Returns this contract class's macro registry.
165 166 167 |
# File 'lib/dry/validation/rust/contract.rb', line 165 def macro_registry @macro_registry ||= MacroRegistry.new(Rust.global_macros) end |
.option(name, default: Undefined, optional: false, **_options) ⇒ Class
Declares an injected contract option and its default behavior.
127 128 129 130 131 132 133 134 135 136 |
# File 'lib/dry/validation/rust/contract.rb', line 127 def option(name, default: Undefined, optional: false, **) (@option_definitions ||= {})[name.to_sym] = OptionDefinition.new( name: name.to_sym, default: default, optional: optional ) attr_reader name self end |
.option_definitions ⇒ Hash{Symbol => OptionDefinition}
Returns option definitions inherited by this contract class.
141 142 143 144 |
# File 'lib/dry/validation/rust/contract.rb', line 141 def option_definitions inherited = superclass.respond_to?(:option_definitions) ? superclass.option_definitions : {} inherited.merge(@option_definitions ||= {}) end |
.own_rules ⇒ Array<Rule>
Returns rules declared directly on this contract class.
113 114 115 |
# File 'lib/dry/validation/rust/contract.rb', line 113 def own_rules @own_rules ||= [] end |
.params(*external_schemas) { ... } ⇒ Schema
Defines or returns a Params-mode schema for this contract.
54 55 56 |
# File 'lib/dry/validation/rust/contract.rb', line 54 def params(*external_schemas, &) define_schema(:params, external_schemas, &) end |
.register_macro(name) { ... } ⇒ Class
Registers a macro available to rules on this contract class.
Positional arguments are forwarded to the macro registry.
155 156 157 158 |
# File 'lib/dry/validation/rust/contract.rb', line 155 def register_macro(name, *, &) macro_registry.register(name, *, &) self end |
.rule(*specs) { ... } ⇒ Rule
Registers a validation rule for one or more schema paths.
94 95 96 97 98 99 100 |
# File 'lib/dry/validation/rust/contract.rb', line 94 def rule(*specs, &block) paths = specs.flat_map { |spec| Path.(spec) } ensure_valid_paths(paths) unless paths.empty? Rule.new(paths: paths, default_path: default_rule_path(specs, paths), block: block).tap do |new_rule| own_rules << new_rule end end |
.rules ⇒ Array<Rule>
Returns inherited and locally declared rules in execution order.
105 106 107 108 |
# File 'lib/dry/validation/rust/contract.rb', line 105 def rules inherited_rules = superclass.respond_to?(:rules) ? superclass.rules : [] [*inherited_rules, *own_rules] end |
.schema(*external_schemas) { ... } ⇒ Schema
Defines or returns a schema-mode schema for this contract.
80 81 82 |
# File 'lib/dry/validation/rust/contract.rb', line 80 def schema(*external_schemas, &) define_schema(:schema, external_schemas, &) end |
.schema_definition ⇒ Schema?
Returns the compiled schema declared by this class or an ancestor.
193 194 195 196 197 |
# File 'lib/dry/validation/rust/contract.rb', line 193 def schema_definition return @schema_definition if instance_variable_defined?(:@schema_definition) superclass.schema_definition if superclass.respond_to?(:schema_definition) end |
Instance Method Details
#[](input, context = {}) ⇒ Result
Validates input with bracket syntax; equivalent to #call.
299 300 301 |
# File 'lib/dry/validation/rust/contract.rb', line 299 def [](input, context = {}) call(input, context) end |
#call(input, context = {}) ⇒ Result
Validates input and returns a finalized result, including rule failures.
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 |
# File 'lib/dry/validation/rust/contract.rb', line 268 def call(input, context = {}) schema = self.class.schema_definition raise SchemaMissingError, "#{self.class} must define a schema" unless schema schema_result = schema.call(input) shared_context = default_context.merge(context) result = Result.new(schema_result, shared_context) schema_error_paths = schema_result.error_prefixes self.class.rules.each do |rule| if rule.each? execute_each(rule, result, shared_context) else next if rule.paths.any? { |path| dependency_error?(schema_error_paths, path) } execute_rule(rule, result, shared_context) end end result.finalize! end |
#inspect ⇒ String
Returns a diagnostic representation of the compiled contract.
327 328 329 |
# File 'lib/dry/validation/rust/contract.rb', line 327 def inspect "#<#{self.class} schema=#{self.class.schema_definition.inspect} rules=#{self.class.rules.inspect}>" end |
#macro_registered?(name) ⇒ Boolean
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.
Returns whether a macro can be resolved by this contract.
309 310 311 |
# File 'lib/dry/validation/rust/contract.rb', line 309 def macro_registered?(name) self.class.macro_registry.key?(name) end |
#resolve_macro(name) ⇒ Macro
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.
Resolves a registered macro by name.
320 321 322 |
# File 'lib/dry/validation/rust/contract.rb', line 320 def resolve_macro(name) self.class.macro_registry.fetch(name) end |