Class: Castkit::Contract::Result

Inherits:
Object
  • Object
show all
Includes:
Cattri
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 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



26
27
28
29
30
31
32
# File 'lib/castkit/contract/result.rb', line 26

def initialize(contract, input, errors: {})
  super()

  cattri_variable_set(:contract, contract.to_sym.freeze)
  cattri_variable_set(:input, input.freeze)
  cattri_variable_set(:errors, errors.freeze)
end

Instance Method Details

#contractSymbol

Returns the name of the contract.

Returns:

  • (Symbol)

    the name of the contract



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

cattri :contract, nil, expose: :read

#errorsHash{Symbol => Object}

Returns the validation error hash.

Returns:

  • (Hash{Symbol => Object})

    the validation error hash



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

cattri :errors, {}, expose: :read

#failure?Boolean

Whether the validation failed with one or more errors.

Returns:

  • (Boolean)


51
52
53
# File 'lib/castkit/contract/result.rb', line 51

def failure?
  !success?
end

#inputHash{Symbol => Object}

Returns the validated input.

Returns:

  • (Hash{Symbol => Object})

    the validated input



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

cattri :input, nil, expose: :read

#inspectString

A debug-friendly representation of the validation result.

Returns:

  • (String)


37
38
39
# File 'lib/castkit/contract/result.rb', line 37

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)


44
45
46
# File 'lib/castkit/contract/result.rb', line 44

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



66
67
68
69
70
71
72
# File 'lib/castkit/contract/result.rb', line 66

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)


58
59
60
61
62
63
# File 'lib/castkit/contract/result.rb', line 58

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