Class: Validator

Inherits:
Object
  • Object
show all
Defined in:
app/services/validator.rb

Overview

Validates XML against the MODSulator schema.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(schema_file = nil) ⇒ Validator

Creates a new Validator, using either a provided XML Schema file or the built-in one.

Parameters:

  • schema_file (defaults to: nil)

    Full path to the desired .xsd file. If none is given, the built-in file will be used.



8
9
10
11
12
# File 'app/services/validator.rb', line 8

def initialize(schema_file = nil)
  schema_file ||= File.expand_path('../modsulator.xsd', __FILE__)

  @schema = Nokogiri::XML::Schema(File.open(schema_file))
end

Instance Attribute Details

#schemaObject (readonly)

The Nokogiri::XML::Schema instance used for validation.



4
5
6
# File 'app/services/validator.rb', line 4

def schema
  @schema
end

Instance Method Details

#validate_xml_doc(doc) ⇒ Object

Validates an XML document.

Parameters:

  • doc

    An instance of Nokogiri::XML::Document

Returns:

  • An array containing holds Nokogiri::XML::SyntaxError elements. If this array has length zero, the document is valid.



29
30
31
32
# File 'app/services/validator.rb', line 29

def validate_xml_doc(doc)
  return doc.errors if(doc.errors.length > 0)
  return @schema.validate(doc)
end

#validate_xml_string(xml) ⇒ Object

Validates an XML string.

Parameters:

  • xml

    An XML document as a string.

Returns:

  • An array containing holds Nokogiri::XML::SyntaxError elements. If this array has length zero, the document is valid.



19
20
21
22
# File 'app/services/validator.rb', line 19

def validate_xml_string(xml)
  xml_doc = Nokogiri::XML(xml)
  return validate_xml_doc(xml_doc)
end