Class: Secretariat::Validator

Inherits:
Object
  • Object
show all
Defined in:
lib/secretariat/validator.rb

Constant Summary collapse

SCHEMATRON =
[
  '../../schemas/zugferd_1/ZUGFeRD1p0.sch',
  '../../schemas/factur-x_1/Factur-X_1.07.2_EN16931.sch'
]
SCHEMA =
[
  '../../schemas/zugferd_1/ZUGFeRD1p0.xsd',
  '../../schemas/factur-x_1/Factur-X_1.07.2_EN16931.xsd'
]
SCHEMA_DIR =
[
  '../../schemas/zugferd_1',
  '../../schemas/factur-x_1/'
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io_or_str, version: 1) ⇒ Validator

Returns a new instance of Validator.



42
43
44
45
# File 'lib/secretariat/validator.rb', line 42

def initialize(io_or_str, version: 1)
  @doc = Nokogiri.XML(io_or_str)
  @version = version
end

Instance Attribute Details

#docObject

Returns the value of attribute doc.



41
42
43
# File 'lib/secretariat/validator.rb', line 41

def doc
  @doc
end

#versionObject

Returns the value of attribute version.



41
42
43
# File 'lib/secretariat/validator.rb', line 41

def version
  @version
end

Instance Method Details

#schemaObject



47
48
49
# File 'lib/secretariat/validator.rb', line 47

def schema
  Nokogiri::XML.Schema open(File.join(__dir__, SCHEMA[version - 1]))
end

#schematron_pathObject



51
52
53
# File 'lib/secretariat/validator.rb', line 51

def schematron_path
  File.join(__dir__, SCHEMATRON[version - 1])
end

#validate_against_schemaObject



55
56
57
# File 'lib/secretariat/validator.rb', line 55

def validate_against_schema
  schema.validate(doc)
end

#validate_against_schematronObject



59
60
61
62
63
64
65
66
67
68
# File 'lib/secretariat/validator.rb', line 59

def validate_against_schematron
  Dir.mktmpdir do |dir|
    docpath = File.join(dir, 'doc.xml')
    File.write(docpath, doc, mode: 'wb')
    jarpath = File.join(__dir__, '../..', 'bin', 'schxslt-cli.jar')
    out = `java -jar #{jarpath} -v -d #{docpath} -s #{schematron_path}`
    return [] if out.start_with?("[valid]")
    return process_schematron_errors(out)
  end
end