Class: JsonStructure::ValidationResult

Inherits:
Object
  • Object
show all
Defined in:
lib/jsonstructure/validation_result.rb

Overview

Represents the result of a validation operation

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#errorsObject (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.message_str,
      location: ffi_error.location_hash
    )
  end

  new(valid, errors)
ensure
  FFI.js_result_cleanup(result_ptr) if result_ptr
end

Instance Method Details

#error_messagesObject



58
59
60
# File 'lib/jsonstructure/validation_result.rb', line 58

def error_messages
  @errors.select(&:error?).map(&:message)
end

#invalid?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/jsonstructure/validation_result.rb', line 54

def invalid?
  !@valid
end

#to_sObject



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

Returns:

  • (Boolean)


50
51
52
# File 'lib/jsonstructure/validation_result.rb', line 50

def valid?
  @valid
end

#warning_messagesObject



62
63
64
# File 'lib/jsonstructure/validation_result.rb', line 62

def warning_messages
  @errors.select(&:warning?).map(&:message)
end