Class: Ticketbai::DocumentValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/ticketbai/document_validator.rb

Constant Summary collapse

SUPPORTED_VALIDATORS =
[Operations::Issuance::OPERATION_NAME, Operations::Annulment::OPERATION_NAME].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(document:, validator_type:) ⇒ DocumentValidator

Returns a new instance of DocumentValidator.

Parameters:

  • document (String)

    The xml document to be validated

  • validator_type (String)

    The validator name to be used to validate the document (.xsd file)

Raises:

  • (ArgumentError)


11
12
13
14
15
16
# File 'lib/ticketbai/document_validator.rb', line 11

def initialize(document:, validator_type:)
  raise ArgumentError, 'Unsupported validator' unless SUPPORTED_VALIDATORS.include? validator_type.downcase.to_sym

  @document = document
  @validator_type = validator_type.downcase.to_sym
end

Instance Attribute Details

#documentObject

Returns the value of attribute document.



3
4
5
# File 'lib/ticketbai/document_validator.rb', line 3

def document
  @document
end

#validator_typeObject

Returns the value of attribute validator_type.



3
4
5
# File 'lib/ticketbai/document_validator.rb', line 3

def validator_type
  @validator_type
end

Instance Method Details

#validateObject

Raises an exception with the validation error(s)



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/ticketbai/document_validator.rb', line 21

def validate
  errors = []

  xsd_validator.validate(Nokogiri::XML(@document)).each do |error|
    errors << error.message
  end

  return unless errors.any?

  raise Ticketbai::TBAIFileError.new(
    errors.uniq.join(','),
    'TBAIFile'
  )
end