Class: JsonStructure::ValidationResult
- Inherits:
-
Object
- Object
- JsonStructure::ValidationResult
- Defined in:
- lib/jsonstructure/validation_result.rb
Overview
Represents the result of a validation operation
Instance Attribute Summary collapse
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
Class Method Summary collapse
-
.from_ffi(result_ptr) ⇒ Object
private
Creates a ValidationResult from an FFI JSResult struct.
Instance Method Summary collapse
- #error_messages ⇒ Object
-
#initialize(valid, errors = []) ⇒ ValidationResult
constructor
A new instance of ValidationResult.
- #invalid? ⇒ Boolean
- #to_s ⇒ Object
- #valid? ⇒ Boolean
- #warning_messages ⇒ Object
Constructor Details
#initialize(valid, errors = []) ⇒ ValidationResult
Returns a new instance of ValidationResult.
45 46 47 48 |
# File 'lib/jsonstructure/validation_result.rb', line 45 def initialize(valid, errors = []) @valid = valid @errors = errors end |
Instance Attribute Details
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
43 44 45 |
# File 'lib/jsonstructure/validation_result.rb', line 43 def errors @errors end |
Class Method Details
.from_ffi(result_ptr) ⇒ Object
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.
Creates a ValidationResult from an FFI JSResult struct
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/jsonstructure/validation_result.rb', line 77 def self.from_ffi(result_ptr) result = FFI::JSResult.new(result_ptr) valid = result[:valid] errors = result.errors_array.map do |ffi_error| ValidationError.new( code: ffi_error[:code], severity: ffi_error[:severity], path: ffi_error.path_str, message: ffi_error., location: ffi_error.location_hash ) end new(valid, errors) ensure FFI.js_result_cleanup(result_ptr) if result_ptr end |
Instance Method Details
#error_messages ⇒ Object
58 59 60 |
# File 'lib/jsonstructure/validation_result.rb', line 58 def @errors.select(&:error?).map(&:message) end |
#invalid? ⇒ Boolean
54 55 56 |
# File 'lib/jsonstructure/validation_result.rb', line 54 def invalid? !@valid end |
#to_s ⇒ Object
66 67 68 69 70 71 72 73 |
# File 'lib/jsonstructure/validation_result.rb', line 66 def to_s if valid? 'Validation succeeded' else "Validation failed with #{@errors.count} error(s):\n" + @errors.map { |e| " - #{e}" }.join("\n") end end |
#valid? ⇒ Boolean
50 51 52 |
# File 'lib/jsonstructure/validation_result.rb', line 50 def valid? @valid end |
#warning_messages ⇒ Object
62 63 64 |
# File 'lib/jsonstructure/validation_result.rb', line 62 def @errors.select(&:warning?).map(&:message) end |