Class: WsdlValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/wsdl_validator/wsdl_validator.rb,
lib/wsdl_validator/error.rb,
lib/wsdl_validator/version.rb

Overview

Helps to validate xml against schemas contained in a WSDL

Defined Under Namespace

Classes: Error

Constant Summary collapse

VERSION =
'0.1.8'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(wsdl_url) ⇒ WsdlValidator

Parse WSDL storing authentication and all schemas from it

Parameters:

  • wsdl_url (String)

    URL to where WSDL is stored or location to where file is stored



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/wsdl_validator/wsdl_validator.rb', line 15

def initialize(wsdl_url)
  self.basic_auth = extract_auth(wsdl_url)
  self.doc = Wasabi.document wsdl_url
  self.show_schemas = parse_wsdl_schemas
  self.schemas = Nokogiri::XML::Schema(show_schemas)
rescue Wasabi::Resolver::HTTPError => resolver
  raise WsdlValidator::Error, "Unauthorized for basic auth #{basic_auth}" if resolver.response.code == 401
  raise resolver
rescue Nokogiri::XML::SyntaxError => syntax_error
  puts "Error for " + show_schemas
  raise syntax_error
end

Instance Attribute Details

#basic_authObject

Returns the value of attribute basic_auth.



10
11
12
# File 'lib/wsdl_validator/wsdl_validator.rb', line 10

def basic_auth
  @basic_auth
end

#docObject

Returns the value of attribute doc.



11
12
13
# File 'lib/wsdl_validator/wsdl_validator.rb', line 11

def doc
  @doc
end

#schemasObject

Nokogiri::XML::Schema


9
10
11
# File 'lib/wsdl_validator/wsdl_validator.rb', line 9

def schemas
  @schemas
end

#show_schemasObject

String

Schemas found within WSDL



7
8
9
# File 'lib/wsdl_validator/wsdl_validator.rb', line 7

def show_schemas
  @show_schemas
end

Instance Method Details

#errors_for(xml) ⇒ Array

Returns a list of syntax errors. Empty list indicates valid xml

Parameters:

  • xml (String, Nokogiri::XML::NodeSet)

Returns:

  • (Array)

    List of Nokogiri::XML::SyntaxError objects



43
44
45
46
47
48
49
# File 'lib/wsdl_validator/wsdl_validator.rb', line 43

def errors_for(xml)
  raise "Incorrect type #{xml.class}" unless [String, Nokogiri::XML::Document, Nokogiri::XML::NodeSet].include? xml.class
  xml_under_test = Nokogiri::XML(xml.to_s)
  soap_envelope = xml_under_test.children.find { |e| e.name == 'Envelope' }
  xml_under_test = extract_root_from_soap(soap_envelope) if soap_envelope
  schemas.validate(xml_under_test)
end

#extract_root_from_soap(envelope) ⇒ Nokogiri::XML::Document

Note:

This is not the ideal approach. Ideally Nokogiri parser would be able to understand SOAP xsd as well

Gets the namespaces from the SOAP Envelope & Body element, adds them to the root element underneath the body and returns that element.

Returns:

  • (Nokogiri::XML::Document)

    Retrieve root element from SOAP



32
33
34
35
36
37
38
# File 'lib/wsdl_validator/wsdl_validator.rb', line 32

def extract_root_from_soap(envelope)
  body = envelope.children.find { |child| child.name == 'Body' }
  root_element = body.children.reject { |child| child.is_a?(Nokogiri::XML::Text) }.first
  envelope.namespaces.each { |namespace, value| root_element[namespace] = value }
  body.namespaces.each { |namespace, value| root_element[namespace] = value }
  Nokogiri::XML root_element.to_s # Convert to Xml Document
end

#valid?(xml) ⇒ Boolean, Exception Also known as: validate

Returns Whether xml is valid according to WSDL of class.

Parameters:

  • xml (String, Nokogiri::XML::NodeSet)

Returns:

  • (Boolean, Exception)

    Whether xml is valid according to WSDL of class

Raises:



53
54
55
56
57
# File 'lib/wsdl_validator/wsdl_validator.rb', line 53

def valid?(xml)
  validator = errors_for xml
  raise WsdlValidator::Error, validator.join unless validator.empty?
  true
end