validate_xml_xsi 0.1.4 → 0.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7ea7f8a1a26dc8803332c448e4ab3609d9699f116f33ab3636a3cec9cf47c86b
4
- data.tar.gz: 9423b6a6cccd460ef5949d39340bd7330e898e9791160e747e70014763cc4ddc
3
+ metadata.gz: 761ad7abcb22af7828d754ff0b1ec2bfdd67e48e8edb82a1d306c05c3b9b3391
4
+ data.tar.gz: ad4000467221dbb6937c1ea9103d8b109666fe17b15f1480937ea7915b37797f
5
5
  SHA512:
6
- metadata.gz: 4ac1fdf7c33249fe3146fbb296dd10ec60f0a20b9cd9cbccf527fd408b66b3ed09c6d8a435c49b91f6c0e34ec452422d5d7cb2b8dab7045ba2e769e5035981f0
7
- data.tar.gz: 7c3b944868fdc9545a30b50a571e1cc5a30e6a75889a1a861bfe3964da56e02af8650d3290180b1dabcfdfd53b9819116a0ba8a7b44dbb6c5a63ed82c15f3a83
6
+ metadata.gz: df5c7aa8152d8353a26285efc3d183e9a9f7813bbad785596a53ab52cf5903360b5ed76b56fe87762a08aa870a721bbad5fc82495a2b51b53d9f3d2f6d2a0b0a
7
+ data.tar.gz: f3e9dbb10e87fc26bef8ae1011ac27aa39148df13fa10055754f728539cb2eccdf830d065e8305e1b37be9bc137587370cb7d667f7d9cef7d574692bb7832679
data/CHANGELOG.md CHANGED
@@ -1,3 +1,15 @@
1
+ ## v0.2.2
2
+
3
+ * Break out parsing of the schema from validation so we can reference the schema if we need to.
4
+
5
+ ## v0.2.1
6
+
7
+ * Update namespace to XML_XSI in bin/validate_xml_xsi utility
8
+
9
+ ## v0.2.0
10
+
11
+ * Change namespace to XML_XSI and rename/promote Validation::Error to Schema::ValidationError < StandardError.
12
+
1
13
  ## v0.1.4
2
14
 
3
15
  * Change module XML, Validation, & Schema to be classes instead.
data/bin/validate_xml_xsi CHANGED
@@ -3,7 +3,7 @@ require 'validate_xml_xsi'
3
3
 
4
4
  while ARGV.size > 0
5
5
  fname = ARGV.shift
6
- errors = XML::Schema::validate(fname)
6
+ errors = XML_XSI::Schema::validate(fname)
7
7
  if errors.empty?
8
8
  puts "XML Schema check complete - NO ERRORS!"
9
9
  else
@@ -1,18 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
2
  require 'nokogiri'
3
3
 
4
- class XML
5
- class Validation
6
- class Error
7
- Params = [:type, :file, :line, :column, :message, :err]
8
- attr_reader *Params, :description
9
- def initialize(*args)
10
- Params.each.with_index { |param, idx| instance_variable_set("@#{param.to_s}", args[idx]) }
11
- @description = "#{@type.to_s} ERROR [#{@file}:#{@line}:#{@column}]: #{@message}".freeze
12
- end
13
- end
14
- end
15
-
4
+ class XML_XSI
16
5
  def self.parse(obj)
17
6
  filename = nil
18
7
  obj = File::read(filename = obj) if obj.is_a?(String) && File::exist?(obj)
@@ -23,9 +12,21 @@ class XML
23
12
  end
24
13
 
25
14
  class Schema
26
- def self.validate(xml_doc)
27
- xml_doc = XML::parse(xml_doc) unless xml_doc.is_a?(Nokogiri::XML::Document)
28
- errors = []
15
+ class DocumentError < StandardError; end
16
+ class ValidationError < StandardError
17
+ Params = [:type, :file, :line, :column, :message, :err]
18
+ attr_reader *Params, :description
19
+ def initialize(*args)
20
+ Params.each.with_index { |param, idx| instance_variable_set("@#{param.to_s}", args[idx]) }
21
+ @description = "#{@type.to_s} ERROR [#{@file}:#{@line}:#{@column}]: #{@message}".freeze
22
+ super(@description)
23
+ end
24
+ end
25
+
26
+ def self.parse_schema(xml_doc)
27
+ unless xml_doc.is_a?(Nokogiri::XML::Document)
28
+ raise DocumentError.new("not a Nokogiri::XML::Document (class: #{xml_doc.class.name})!"
29
+ end
29
30
  ## Build an all-in-one XSD document that imports all of the separate schema locations
30
31
  xsd_doc = "<?xml version=\"1.0\"?>\n"
31
32
  xsd_doc << "<xsd:schema targetNamespace=\"http://www.w3.orig/XML/1998/namespace\"\n" \
@@ -38,7 +39,7 @@ class XML
38
39
  elem['xsi:schemaLocation'].scan(/(\S+)\s+(\S+)/).each do |ns_set|
39
40
  if ns_loc = schemata_by_ns[ns_set.first]
40
41
  unless ns_loc.eql?(ns_set.last)
41
- error "MISMATCHING XMLNS XSI: #{ns_set.first} -> #{ns_loc} VS #{ns_set.last}"
42
+ raise DocumentError.new("MISMATCHING XMLNS XSI: #{ns_set.first} -> #{ns_loc} VS #{ns_set.last}")
42
43
  end
43
44
  else
44
45
  schemata_by_ns[ns_set.first] = ns_set.last
@@ -49,16 +50,25 @@ class XML
49
50
  xsd_doc << " <xsd:import namespace=\"#{ns_href}\" schemaLocation=\"#{ns_file}\"/>\n"
50
51
  end
51
52
  xsd_doc << "</xsd:schema>\n"
52
- xsd = Nokogiri::XML.Schema(xsd_doc)
53
+ Nokogiri::XML::Schema.new(xsd_doc)
54
+ end
55
+
56
+ def self.validate(xml_doc, xsd = nil)
57
+ xml_doc = XML_XSI::parse(xml_doc) unless xml_doc.is_a?(Nokogiri::XML::Document)
58
+ errors = []
59
+ unless xsd.nil? || xsd.is_a?(Nokogiri::XML::Schema)
60
+ raise DocumentError.new("Invalid XSD - not a Nokogiri::XML::Schema (class: #{xsd.class.name})!")
61
+ end
62
+ xsd = self.parse_schema(xml_doc) if xsd.nil?
53
63
  xsd.errors.each do |err|
54
64
  err_msg = (/ERROR: /.match(err.message)) ? $' : err.message
55
- errors << XML::Validation::Error.new(:XSD, err.file, err.line, err.column, err_msg, err)
65
+ errors << ValidationError.new(:XSD, err.file, err.line, err.column, err_msg, err)
56
66
  end
57
67
  errs = xsd.validate(xml_doc)
58
68
  errs.each do |err|
59
69
  err_msg = (/ERROR: /.match(err.message)) ? $' : err.message
60
70
  fname = (err.file.nil?) ? xml_doc.filename : err.file
61
- errors << XML::Validation::Error.new(:XML, fname, err.line, err.column, err_msg, err)
71
+ errors << ValidationError.new(:XML, fname, err.line, err.column, err_msg, err)
62
72
  end
63
73
  errors
64
74
  end
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |spec|
2
2
  spec.name = "validate_xml_xsi"
3
- spec.version = "0.1.4"
3
+ spec.version = "0.2.2"
4
4
  spec.authors = ["David Hansen"]
5
5
  spec.email = ["david@hansen4.net"]
6
6
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: validate_xml_xsi
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Hansen
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-03-08 00:00:00.000000000 Z
11
+ date: 2022-03-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler