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 +4 -4
- data/CHANGELOG.md +12 -0
- data/bin/validate_xml_xsi +1 -1
- data/lib/validate_xml_xsi.rb +29 -19
- data/validate_xml_xsi.gemspec +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 761ad7abcb22af7828d754ff0b1ec2bfdd67e48e8edb82a1d306c05c3b9b3391
|
4
|
+
data.tar.gz: ad4000467221dbb6937c1ea9103d8b109666fe17b15f1480937ea7915b37797f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
data/lib/validate_xml_xsi.rb
CHANGED
@@ -1,18 +1,7 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
require 'nokogiri'
|
3
3
|
|
4
|
-
class
|
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
|
-
|
27
|
-
|
28
|
-
|
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
|
-
|
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
|
-
|
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 <<
|
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 <<
|
71
|
+
errors << ValidationError.new(:XML, fname, err.line, err.column, err_msg, err)
|
62
72
|
end
|
63
73
|
errors
|
64
74
|
end
|
data/validate_xml_xsi.gemspec
CHANGED
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.
|
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-
|
11
|
+
date: 2022-03-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|