Class: Dry::Validation::Values

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/dry/validation/values.rb

Overview

A convenient wrapper for data processed by schemas

Values are available within the rule blocks. They act as hash-like objects and expose a convenient API for accessing data.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Values

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 a new instance of Values.



27
28
29
# File 'lib/dry/validation/values.rb', line 27

def initialize(data)
  @data = data
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ 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.



85
86
87
88
89
90
91
# File 'lib/dry/validation/values.rb', line 85

def method_missing(meth, *args, &block)
  if data.respond_to?(meth)
    data.public_send(meth, *args, &block)
  else
    super
  end
end

Instance Attribute Details

#dataHash (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.

Schema’s result output

Returns:

  • (Hash)


24
25
26
# File 'lib/dry/validation/values.rb', line 24

def data
  @data
end

Instance Method Details

#[](*args) ⇒ Object

Read from the provided key

Examples:

rule(:age) do
  key.failure('must be > 18') if values[:age] <= 18
end

Parameters:

  • key (Symbol)

Returns:

  • (Object)


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/dry/validation/values.rb', line 43

def [](*args)
  return data.dig(*args) if args.size > 1

  case (key = args[0])
  when Symbol, String, Array, Hash
    keys = Schema::Path[key].to_a

    return data.dig(*keys) unless keys.last.is_a?(Array)

    last = keys.pop
    vals = self.class.new(data.dig(*keys))
    vals.fetch_values(*last) { nil }
  else
    raise ArgumentError, '+key+ must be a valid path specification'
  end
end

#key?(key, hash = data) ⇒ Boolean

Returns:

  • (Boolean)


61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/dry/validation/values.rb', line 61

def key?(key, hash = data)
  return hash.key?(key) if key.is_a?(Symbol)

  Schema::Path[key].reduce(hash) do |a, e|
    if e.is_a?(Array)
      result = e.all? { |k| key?(k, a) }
      return result
    else
      return false unless a.is_a?(Array) ? (e >= 0 && e < a.size) : a.key?(e)
    end
    a[e]
  end

  true
end

#respond_to_missing?(meth, include_private = false) ⇒ 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:

  • (Boolean)


78
79
80
# File 'lib/dry/validation/values.rb', line 78

def respond_to_missing?(meth, include_private = false)
  super || data.respond_to?(meth, include_private)
end