Class: Dry::Validation::Rust::Evaluator

Inherits:
Object
  • Object
show all
Defined in:
lib/dry/validation/rust/evaluator.rb

Overview

The evaluation context for contract rules.

Inside a rule block, self is an Evaluator instance. It exposes the validated values, the rule path, error collectors, and the mutable per-call context.

Examples:

Add a failure for the rule value

rule(:age) { key.failure("must be at least 18") if value < 18 }

Instance Attribute Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name) ⇒ Object (private)

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.



287
288
289
290
291
# File 'lib/dry/validation/rust/evaluator.rb', line 287

def method_missing(name, ...)
  return contract.__send__(name, ...) if contract.respond_to?(name, true)

  super
end

Instance Attribute Details

#indexInteger? (readonly)

Returns the collection index for a rule.each evaluation.

Examples:

Add an error for the second collection member

rule(:tags).each { key.failure("is reserved") if index == 1 && value == "admin" }

Returns:

  • (Integer, nil)


58
59
60
# File 'lib/dry/validation/rust/evaluator.rb', line 58

def index
  @index
end

#valuesContract::Values (readonly)

Returns all validated output available to the rule.

Examples:

Compare two validated values

rule(:password_confirmation) { key.failure("does not match") if value != values[:password] }

Returns:



44
45
46
# File 'lib/dry/validation/rust/evaluator.rb', line 44

def values
  @values
end

Instance Method Details

#baseFailures

Returns the failure collector for base-level messages.

Examples:

Add a contract-wide error

rule { base.failure("cannot be approved") }

Returns:

  • (Failures)

    collector that adds base-level rule failures



129
130
131
# File 'lib/dry/validation/rust/evaluator.rb', line 129

def base
  @base_failures
end

#base_rule_error?Boolean

Returns whether a base-level rule error exists.

Examples:

Check whether this rule added a base failure

rule { base.failure("cannot continue") unless base_rule_error? }

Returns:

  • (Boolean)


201
202
203
# File 'lib/dry/validation/rust/evaluator.rb', line 201

def base_rule_error?
  !base.empty? || result.base_rule_error?
end

#key(path = default_path) ⇒ Failures

Returns the failure collector for a path, defaulting to the rule path.

Examples:

Add an error at the rule's default path

rule(:email) { key.failure("is already taken") }

Add an error at a different path

rule(:password) { key(:password_confirmation).failure("does not match") }

Parameters:

  • path (Symbol, String, Array, Hash) (defaults to: default_path)

    key or supported path specification

Returns:

  • (Failures)

    collector that adds rule failures at the path



119
120
121
122
# File 'lib/dry/validation/rust/evaluator.rb', line 119

def key(path = default_path)
  normalized = Path.parse(path)
  @key_failures[normalized] ||= Failures.new(normalized)
end

#key?(name = value_path) ⇒ Boolean

Returns whether validated output contains a key or path.

Examples:

Require a related value

rule(:password) { key.failure("needs confirmation") unless key?(:password_confirmation) }

Parameters:

  • name (Symbol, String, Array, Hash) (defaults to: value_path)

    key or supported path specification

Returns:

  • (Boolean)


160
161
162
# File 'lib/dry/validation/rust/evaluator.rb', line 160

def key?(name = value_path)
  Path.key?(values.data, name)
end

#rule_error?(path = nil) ⇒ Boolean

Returns whether a rule error exists at a path or the default path.

Examples:

Avoid adding a duplicate rule error

rule(:email) { key.failure("is blocked") unless rule_error? }

Parameters:

  • path (Symbol, String, Array, Hash, nil) (defaults to: nil)

    path to check, or the default rule path

Returns:

  • (Boolean)


188
189
190
191
192
193
194
# File 'lib/dry/validation/rust/evaluator.rb', line 188

def rule_error?(path = nil)
  if path
    result.rule_error?(path)
  else
    !key(default_path).empty?
  end
end

#schema_error?(path) ⇒ Boolean Also known as: error?

Returns whether the schema has an error at a path.

Examples:

Skip a dependent check after a schema failure

rule(:age) { key.failure("is too young") unless schema_error?(:age) || value >= 18 }

Parameters:

  • path (Symbol, String, Array, Hash)

    key or supported path specification

Returns:

  • (Boolean)


170
171
172
# File 'lib/dry/validation/rust/evaluator.rb', line 170

def schema_error?(path)
  result.schema_error?(path)
end

#valueObject?

Returns the value at the rule's primary path, or nil when absent.

Examples:

Reject a value in a rule

rule(:age) { key.failure("must be at least 18") if value < 18 }

Returns:

  • (Object, nil)


149
150
151
152
# File 'lib/dry/validation/rust/evaluator.rb', line 149

def value
  raw = Path.fetch(values.data, value_path)
  raw.equal?(Path::Undefined) ? nil : raw
end