Class: Castkit::Contract::Result

Inherits:
Object
  • Object
show all
Defined in:
lib/castkit/contract/result.rb

Overview

Represents the result of a contract validation.

Provides access to the validation outcome, including whether it succeeded or failed, and includes the full list of errors if any.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(contract, input, errors: {}) ⇒ Result

Initializes a new result object.

Parameters:

  • contract (Symbol, String)

    the name of the contract

  • input (Hash{Symbol => Object})

    the validated input

  • errors (Hash{Symbol => Object}) (defaults to: {})

    the validation errors



24
25
26
27
28
# File 'lib/castkit/contract/result.rb', line 24

def initialize(contract, input, errors: {})
  @contract = contract.to_sym.freeze
  @input = input.freeze
  @errors = errors.freeze
end

Instance Attribute Details

#contractSymbol (readonly)

Returns the name of the contract.

Returns:

  • (Symbol)

    the name of the contract



11
12
13
# File 'lib/castkit/contract/result.rb', line 11

def contract
  @contract
end

#errorsHash{Symbol => Object} (readonly)

Returns the validation error hash.

Returns:

  • (Hash{Symbol => Object})

    the validation error hash



17
18
19
# File 'lib/castkit/contract/result.rb', line 17

def errors
  @errors
end

#inputHash{Symbol => Object} (readonly)

Returns the validated input.

Returns:

  • (Hash{Symbol => Object})

    the validated input



14
15
16
# File 'lib/castkit/contract/result.rb', line 14

def input
  @input
end

Instance Method Details

#failure?Boolean

Whether the validation failed with one or more errors.

Returns:

  • (Boolean)


47
48
49
# File 'lib/castkit/contract/result.rb', line 47

def failure?
  !success?
end

#inspectString

A debug-friendly representation of the validation result.

Returns:

  • (String)


33
34
35
# File 'lib/castkit/contract/result.rb', line 33

def inspect
  "#<#{self.class.name} contract=#{contract.inspect} success=#{success?} errors=#{errors.inspect}>"
end

#success?Boolean

Whether the validation passed with no errors.

Returns:

  • (Boolean)


40
41
42
# File 'lib/castkit/contract/result.rb', line 40

def success?
  errors.empty?
end

#to_hashHash{Symbol => Object} Also known as: to_h

Returns the input validation and error hash.

Returns:

  • (Hash{Symbol => Object})

    the input validation and error hash



62
63
64
65
66
67
68
# File 'lib/castkit/contract/result.rb', line 62

def to_hash
  @to_hash ||= {
    contract: contract,
    input: input,
    errors: errors
  }.freeze
end

#to_sString

A readable string representation of the validation result.

Returns:

  • (String)


54
55
56
57
58
59
# File 'lib/castkit/contract/result.rb', line 54

def to_s
  return "[Castkit] Contract validation passed for #{contract}" if success?

  parsed_errors = errors.map { |k, v| "  #{k}: #{v.inspect}" }.join("\n")
  "[Castkit] Contract validation failed for #{contract}:\n#{parsed_errors}"
end