Class: Dry::Validation::Rust::Schema::Result

Inherits:
Data
  • Object
show all
Defined in:
lib/dry/validation/rust/schema/result.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#error_prefixesObject (readonly)

Returns the value of attribute error_prefixes

Returns:

  • (Object)

    the current value of error_prefixes



7
8
9
# File 'lib/dry/validation/rust/schema/result.rb', line 7

def error_prefixes
  @error_prefixes
end

#messagesObject (readonly)

Returns the value of attribute messages

Returns:

  • (Object)

    the current value of messages



7
8
9
# File 'lib/dry/validation/rust/schema/result.rb', line 7

def messages
  @messages
end

#outputObject (readonly) Also known as: to_h

Returns the value of attribute output

Returns:

  • (Object)

    the current value of output



7
8
9
# File 'lib/dry/validation/rust/schema/result.rb', line 7

def output
  @output
end

Class Method Details

.buildObject



9
# File 'lib/dry/validation/rust/schema/result.rb', line 9

alias_method :build, :new

.new(output = nil, messages = nil, **kwargs) ⇒ Result

Creates a structural validation result and caches its schema-error path index.

Parameters:

  • output (Hash) (defaults to: nil)

    coerced schema output

  • messages (Array<Message>) (defaults to: nil)

    schema validation failures

Returns:

  • (Result)

    immutable schema validation result



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/dry/validation/rust/schema/result.rb', line 17

def new(output = nil, messages = nil, **kwargs)
  unknown = kwargs.keys - %i[output messages]
  unless unknown.empty?
    raise ArgumentError,
          "unknown keyword#{'s' if unknown.length > 1}: #{unknown.map(&:inspect).join(', ')}"
  end

  output = kwargs.fetch(:output) if kwargs.key?(:output)
  messages = kwargs.fetch(:messages) if kwargs.key?(:messages)
  prefixes = PathTrie.new
  messages.each { |message| prefixes.add(message.path) }

  build(output, messages, prefixes.freeze)
end

Instance Method Details

#[](key) ⇒ Object



56
57
58
# File 'lib/dry/validation/rust/schema/result.rb', line 56

def [](key)
  output[key]
end

#error?(spec) ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
54
# File 'lib/dry/validation/rust/schema/result.rb', line 51

def error?(spec)
  path = Path.parse(spec)
  messages.any? { |message| Path.prefix?(message.path, path) }
end

#errors(options = {}) ⇒ Object



47
48
49
# File 'lib/dry/validation/rust/schema/result.rb', line 47

def errors(options = {})
  MessageSet.new(messages, options).with(options)
end

#failure?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/dry/validation/rust/schema/result.rb', line 43

def failure?
  !success?
end

#key?(key) ⇒ Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/dry/validation/rust/schema/result.rb', line 60

def key?(key)
  output.key?(key)
end

#success?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/dry/validation/rust/schema/result.rb', line 39

def success?
  messages.empty?
end