Class: Canon::Validators::XmlValidator

Inherits:
BaseValidator show all
Defined in:
lib/canon/validators/xml_validator.rb

Overview

Validator for XML input

Validates XML input using Nokogiri’s strict parsing mode. Raises detailed ValidationError with line and column information when malformed XML is detected.

Class Method Summary collapse

Methods inherited from BaseValidator

extract_location

Class Method Details

.validate!(input) ⇒ void

This method returns an undefined value.

Validate XML input

Parameters:

  • input (String)

    The XML string to validate

Raises:



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/canon/validators/xml_validator.rb', line 19

def self.validate!(input)
  return if input.nil? || input.strip.empty?

  # Parse with strict error handling
  Nokogiri::XML(input) do |config|
    config.strict.nonet
  end
rescue Nokogiri::XML::SyntaxError => e
  location = extract_location(e)
  raise Canon::ValidationError.new(
    e.message.split("\n").first,
    format: :xml,
    line: location[:line],
    column: location[:column],
    details: extract_details(e),
  )
end