Class: Lutaml::Xml::Schema::Xsd::ValidationResult

Inherits:
Model::Serializable show all
Defined in:
lib/lutaml/xml/schema/xsd/validation_result.rb

Overview

Result of validation with error collection

Constant Summary

Constants included from Model::Serialize

Model::Serialize::DEFAULT_VALUE_MAP, Model::Serialize::INTERNAL_ATTRIBUTES, Model::Serialize::LAZY_EMPTY_COLLECTION

Instance Attribute Summary

Attributes included from Model::Serialize

#lutaml_parent, #lutaml_register, #lutaml_root

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Model::Serialize

#attr_value, #attribute_exist?, #extract_register_id, included, #init_deserialization_state, #initialize, #key_exist?, #key_value, #method_missing, #prepare_instance_format_options, #pretty_print_instance_variables, register_format_mapping_method, register_from_format_method, register_to_format_method, #respond_to_missing?, #to_format, #to_yaml_hash, #using_default?, #using_default_for, #validate_attribute!, #validate_root_mapping!, #value_map, #value_set_for

Methods included from Model::Liquefiable

included, #to_liquid

Methods included from Model::Validation

#format_element_sequences, #order_names, #validate, #validate_helper, #validate_sequence!

Methods included from Model::ComparableModel

#already_compared?, #attributes_hash, #calculate_hash, #comparison_key, #eql?, #hash, included, #same_class?

Methods included from Model::Serialize::Builder

#initialize, #mixed_content?

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Lutaml::Model::Serialize

Class Method Details

.failure(errors) ⇒ ValidationResult

Create failure result with errors

Parameters:

Returns:



29
30
31
# File 'lib/lutaml/xml/schema/xsd/validation_result.rb', line 29

def self.failure(errors)
  new(valid: false, errors: errors)
end

.successValidationResult

Create success result

Returns:



22
23
24
# File 'lib/lutaml/xml/schema/xsd/validation_result.rb', line 22

def self.success
  new(valid: true, errors: [])
end

Instance Method Details

#error_countInteger

Get error count

Returns:

  • (Integer)


47
48
49
# File 'lib/lutaml/xml/schema/xsd/validation_result.rb', line 47

def error_count
  (errors || []).size
end

#error_messagesArray<String>

Get all error messages

Returns:

  • (Array<String>)


53
54
55
# File 'lib/lutaml/xml/schema/xsd/validation_result.rb', line 53

def error_messages
  (errors || []).map(&:message)
end

#errors_for(field) ⇒ Array<ValidationError>

Get errors for specific field

Parameters:

  • field (Symbol, String)

    Field name

Returns:



60
61
62
63
# File 'lib/lutaml/xml/schema/xsd/validation_result.rb', line 60

def errors_for(field)
  field_str = field.to_s
  (errors || []).select { |e| e.field == field_str }
end

#invalid?Boolean

Check if validation failed

Returns:

  • (Boolean)


41
42
43
# File 'lib/lutaml/xml/schema/xsd/validation_result.rb', line 41

def invalid?
  !valid
end

#to_sString

Format as human-readable string

Returns:

  • (String)


67
68
69
70
71
72
73
74
75
# File 'lib/lutaml/xml/schema/xsd/validation_result.rb', line 67

def to_s
  return "Valid" if valid?

  lines = ["Validation failed with #{error_count} error(s):"]
  errors.each_with_index do |error, idx|
    lines << "  #{idx + 1}. #{error}"
  end
  lines.join("\n")
end

#valid?Boolean

Check if validation passed

Returns:

  • (Boolean)


35
36
37
# File 'lib/lutaml/xml/schema/xsd/validation_result.rb', line 35

def valid?
  valid
end

#validate!Object

Raise error if validation failed



79
80
81
# File 'lib/lutaml/xml/schema/xsd/validation_result.rb', line 79

def validate!
  raise ValidationFailedError.new(self) if invalid?
end