Class: Castkit::Contract::Result
- Inherits:
-
Object
- Object
- Castkit::Contract::Result
- 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
-
#contract ⇒ Symbol
readonly
The name of the contract.
-
#errors ⇒ Hash{Symbol => Object}
readonly
The validation error hash.
-
#input ⇒ Hash{Symbol => Object}
readonly
The validated input.
Instance Method Summary collapse
-
#failure? ⇒ Boolean
Whether the validation failed with one or more errors.
-
#initialize(contract, input, errors: {}) ⇒ Result
constructor
Initializes a new result object.
-
#inspect ⇒ String
A debug-friendly representation of the validation result.
-
#success? ⇒ Boolean
Whether the validation passed with no errors.
-
#to_hash ⇒ Hash{Symbol => Object}
(also: #to_h)
The input validation and error hash.
-
#to_s ⇒ String
A readable string representation of the validation result.
Constructor Details
#initialize(contract, input, errors: {}) ⇒ Result
Initializes a new result object.
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
#contract ⇒ Symbol (readonly)
Returns the name of the contract.
11 12 13 |
# File 'lib/castkit/contract/result.rb', line 11 def contract @contract end |
#errors ⇒ Hash{Symbol => Object} (readonly)
Returns the validation error hash.
17 18 19 |
# File 'lib/castkit/contract/result.rb', line 17 def errors @errors end |
#input ⇒ Hash{Symbol => Object} (readonly)
Returns 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.
47 48 49 |
# File 'lib/castkit/contract/result.rb', line 47 def failure? !success? end |
#inspect ⇒ String
A debug-friendly representation of the validation result.
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.
40 41 42 |
# File 'lib/castkit/contract/result.rb', line 40 def success? errors.empty? end |
#to_hash ⇒ Hash{Symbol => Object} Also known as: to_h
Returns 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_s ⇒ String
A readable string representation of the validation result.
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 |