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.



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.



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.



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