Sha256: 078d127d0bb551f39093d724b17eb40956575bb6b559c905daea4d4af9a747fc

Contents?: true

Size: 1.49 KB

Versions: 1

Compression:

Stored size: 1.49 KB

Contents

# Helps to validate xml against schemas contained in a WSDL
class WsdlValidator
  # @param [String] wsdl_url URL to where WSDL is stored
  def initialize(wsdl_url)
    @doc = Wasabi.document wsdl_url
    @schemas = @doc.parser.schemas.collect(&:to_s).join
  end

  # This is not the ideal approach. Ideally Nokogiri parser would be able to understand SOAP xsd as well
  # @return [Nokogiri::XML::Document] Retrieve root element from SOAP
  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

  # @param [String, Nokogiri::XML::NodeSet] xml
  # @return [Boolean] Whether xml is valid according to WSDL of class
  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
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
wsdl_validator-0.1.3 lib/wsdl_validator/wsdl_validator.rb