Class: WsdlValidator

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

Overview

Helps to validate xml against schemas contained in a WSDL

Constant Summary collapse

VERSION =
'0.1.3'.freeze

Instance Method Summary collapse

Constructor Details

#initialize(wsdl_url) ⇒ WsdlValidator

Returns a new instance of WsdlValidator.

Parameters:

  • wsdl_url (String)

    URL to where WSDL is stored



4
5
6
7
# File 'lib/wsdl_validator/wsdl_validator.rb', line 4

def initialize(wsdl_url)
  @doc = Wasabi.document wsdl_url
  @schemas = @doc.parser.schemas.collect(&:to_s).join
end

Instance Method Details

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

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

Returns:

  • (Nokogiri::XML::Document)

    Retrieve root element from SOAP



11
12
13
14
15
16
17
# File 'lib/wsdl_validator/wsdl_validator.rb', line 11

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

Returns Whether xml is valid according to WSDL of class.

Parameters:

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

Returns:

  • (Boolean)

    Whether xml is valid according to WSDL of class



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/wsdl_validator/wsdl_validator.rb', line 21

def valid?(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
  xsd = Nokogiri::XML::Schema(@schemas)
  validator = xsd.validate(xml_under_test)
  validator.each { |error| puts error.message }
  return false unless validator.empty?
  true
end