Class: Nfcom::Validators::XmlValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/nfcom/validators/xml_validator.rb

Constant Summary collapse

SCHEMA_PATH =
File.join(__dir__, '../../schemas/nfcom_v1.00.xsd')

Instance Method Summary collapse

Instance Method Details

#validar(xml) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/nfcom/validators/xml_validator.rb', line 8

def validar(xml)
  # TODO: Implementar validação contra XSD
  # Por enquanto, apenas valida se é XML válido
  doc = Nokogiri::XML(xml)

  if doc.errors.any?
    erros = doc.errors.map(&:message).join(', ')
    raise Errors::ValidationError, "XML inválido: #{erros}"
  end

  # TODO: Validar contra schema XSD quando disponível
  # xsd = Nokogiri::XML::Schema(File.read(SCHEMA_PATH))
  # erros = xsd.validate(doc)
  # raise Errors::ValidationError, erros.map(&:message).join(', ') if erros.any?

  true
rescue Nokogiri::XML::SyntaxError => e
  raise Errors::XmlError, "Erro de sintaxe no XML: #{e.message}"
end