Class: Nfe::Helpers::SchemaValidator

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Naming
Defined in:
lib/nfe/helpers/schema_validator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(schema_path, xml) ⇒ SchemaValidator

Returns a new instance of SchemaValidator.



8
9
10
11
12
# File 'lib/nfe/helpers/schema_validator.rb', line 8

def initialize(schema_path, xml)
  @document = Nokogiri::XML(xml)
  @xsd = Nokogiri::XML::Schema(File.open(schema_path))
  @errors = ActiveModel::Errors.new(self)
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



6
7
8
# File 'lib/nfe/helpers/schema_validator.rb', line 6

def errors
  @errors
end

Instance Method Details

#valid?Boolean

Returns:

  • (Boolean)


14
15
16
17
18
19
20
21
# File 'lib/nfe/helpers/schema_validator.rb', line 14

def valid?
  @xsd.validate(@document).each do |error|
    message = error.message.gsub("{http://www.portalfiscal.inf.br/nfe}", "")
    @errors.add(:base, message) unless @errors.added? :base, message
  end

  @errors.empty?
end