Module: FacturX::Validation::Schema

Defined in:
lib/factur_x/validation/schema.rb

Overview

Structural validation against the official Factur-X 1.0.9 EN 16931 XSD.

Constant Summary collapse

ROOT =
File.expand_path("../schemas/Factur-X_EN16931.xsd", __dir__)

Class Method Summary collapse

Class Method Details

.compiledObject

Compiling the schema costs far more than validating against it, so the imports are resolved once and the result kept for the process lifetime.



32
33
34
35
36
# File 'lib/factur_x/validation/schema.rb', line 32

def compiled
  @compiled ||= Nokogiri::XML::Schema.from_document(
    Nokogiri::XML(File.read(ROOT), ROOT)
  )
end

.valid?(xml) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/factur_x/validation/schema.rb', line 19

def valid?(xml)
  validate(xml).empty?
end

.validate(xml) ⇒ Object



14
15
16
17
# File 'lib/factur_x/validation/schema.rb', line 14

def validate(xml)
  document = xml.is_a?(Nokogiri::XML::Document) ? xml : Nokogiri::XML(xml)
  compiled.validate(document).map(&:message)
end

.validate!(xml) ⇒ Object

Raises:



23
24
25
26
27
28
# File 'lib/factur_x/validation/schema.rb', line 23

def validate!(xml)
  violations = validate(xml)
  raise SchemaError, violations unless violations.empty?

  xml
end