lutaml-model 0.8.177 → 0.8.178
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:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 2992312b17ca6365a073615318db447cca51e3aa2c7f7c9279565faa370cbdc3
|
|
4
|
+
data.tar.gz: 7b41def8578756a2e1e36a693a5b36172359ddbc170007a26f0848ed1a30eb85
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 21cf0a02683ad8ccefce980343448195a40fdf48c49ec7420b606d3ee8543366d3e5e4cefef62381c6a5b9a9e341c0d8f67b27c75a976618a9db4f54996d6b80
|
|
7
|
+
data.tar.gz: 40a5bb7ec659bda3d112b710146ad4a6b9e678dab938bc76dbcaadc2a970cfbb8b2f464edf8579a2e21ef82ea2f8d84f55fc09923dd3d661ee4bf3debe59f911
|
data/lib/lutaml/model/version.rb
CHANGED
|
@@ -6,9 +6,18 @@ module Lutaml
|
|
|
6
6
|
module Xml
|
|
7
7
|
# Validates a serialized XML string against an XSD schema file.
|
|
8
8
|
#
|
|
9
|
-
# Backs the `validate_xml_with` class macro (issue #264).
|
|
10
|
-
#
|
|
11
|
-
# on
|
|
9
|
+
# Backs the `validate_xml_with` class macro (issue #264). Routing is
|
|
10
|
+
# engine-agnostic through moxml (#1184): schemas compile and validate
|
|
11
|
+
# on the CONFIGURED engine — `Moxml::XSD.supported?` answers without
|
|
12
|
+
# raising, and engines without a validator (ox, oga, rexml) fall back
|
|
13
|
+
# to the historical Nokogiri path, keeping its requirement error when
|
|
14
|
+
# the gem is absent. The leptris engine carries a validator, with the
|
|
15
|
+
# pending targetNamespace-enforcement gap tracked upstream
|
|
16
|
+
# (leptris/leptris-ruby#679).
|
|
17
|
+
#
|
|
18
|
+
# Strict parsing: libxml2 recovery would otherwise repair malformed
|
|
19
|
+
# input and report it as schema-valid. Malformed input raises
|
|
20
|
+
# Moxml::ParseError (the normalized class; line and column carried).
|
|
12
21
|
#
|
|
13
22
|
# Compiled schemas are memoized by absolute path: the XSD files are
|
|
14
23
|
# constant for the life of the process, so each is read and compiled once
|
|
@@ -17,10 +26,8 @@ module Lutaml
|
|
|
17
26
|
# Not to be confused with Schema::Xsd::SchemaValidator, which checks
|
|
18
27
|
# that an XSD document itself is well-formed XSD.
|
|
19
28
|
module XsdValidator
|
|
20
|
-
# Concurrent (and a working Nokogiri) are unavailable under Opal, so
|
|
21
|
-
# the memoization store is only built off-Opal. validate raises a clear
|
|
22
|
-
# NotImplementedError under Opal before this store is ever consulted.
|
|
23
29
|
@schemas = ::Concurrent::Map.new unless RUBY_ENGINE == "opal"
|
|
30
|
+
@nokogiri_schemas = ::Concurrent::Map.new unless RUBY_ENGINE == "opal"
|
|
24
31
|
|
|
25
32
|
# @param xml [String] an XML document
|
|
26
33
|
# @param schema_paths [Array<String>] paths to XSD schema files
|
|
@@ -31,36 +38,74 @@ module Lutaml
|
|
|
31
38
|
|
|
32
39
|
if RUBY_ENGINE == "opal"
|
|
33
40
|
raise NotImplementedError,
|
|
34
|
-
"XSD schema validation (validate_xml_with) requires
|
|
35
|
-
"
|
|
41
|
+
"XSD schema validation (validate_xml_with) requires an " \
|
|
42
|
+
"XML engine with schema validation, which is not " \
|
|
43
|
+
"available under Opal."
|
|
36
44
|
end
|
|
37
45
|
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
+
if engine_supports_xsd?
|
|
47
|
+
document = context.parse(xml, strict: true)
|
|
48
|
+
paths.flat_map do |path|
|
|
49
|
+
schema_for(path)
|
|
50
|
+
.validate(document)
|
|
51
|
+
.map { |error| Error::SchemaValidationError.new(error.message, path) }
|
|
52
|
+
end
|
|
53
|
+
else
|
|
54
|
+
ensure_nokogiri!
|
|
55
|
+
begin
|
|
56
|
+
document = ::Nokogiri::XML(xml, &:strict)
|
|
57
|
+
rescue ::Nokogiri::XML::SyntaxError => e
|
|
58
|
+
# One error contract across lanes: the fallback normalizes
|
|
59
|
+
# to the same Moxml::ParseError the engine path raises.
|
|
60
|
+
raise Moxml::ParseError.new(e.message, line: e.line,
|
|
61
|
+
column: e.column)
|
|
62
|
+
end
|
|
63
|
+
paths.flat_map do |path|
|
|
64
|
+
nokogiri_schema_for(path)
|
|
65
|
+
.validate(document)
|
|
66
|
+
.map { |error| Error::SchemaValidationError.new(error.message, path) }
|
|
67
|
+
end
|
|
46
68
|
end
|
|
47
69
|
end
|
|
48
70
|
|
|
71
|
+
# The configured engine as a moxml backend symbol. Config's plain
|
|
72
|
+
# symbol (nokogiri, leptris, ...) — NOT the AdapterResolver's
|
|
73
|
+
# configured type, whose FIXED_ADAPTER_TYPE sentinel is internal
|
|
74
|
+
# and must never reach Moxml (the closed #1183 leak).
|
|
75
|
+
def self.backend
|
|
76
|
+
Lutaml::Model::Config.xml_adapter_type || :nokogiri
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
def self.engine_supports_xsd?
|
|
80
|
+
Moxml::XSD.supported?(context)
|
|
81
|
+
rescue Moxml::AdapterError, LoadError
|
|
82
|
+
false
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
def self.context
|
|
86
|
+
@context ||= Moxml.new(backend)
|
|
87
|
+
end
|
|
88
|
+
|
|
49
89
|
def self.schema_for(path)
|
|
50
|
-
@schemas.compute_if_absent(path)
|
|
51
|
-
::Nokogiri::XML::Schema(File.read(path))
|
|
52
|
-
end
|
|
90
|
+
@schemas.compute_if_absent(path) { context.xsd_file(path) }
|
|
53
91
|
end
|
|
54
92
|
|
|
55
93
|
def self.ensure_nokogiri!
|
|
56
94
|
require "nokogiri"
|
|
57
95
|
rescue LoadError
|
|
58
96
|
raise Error::XmlConfigurationError,
|
|
59
|
-
"XSD schema validation (validate_xml_with) requires
|
|
60
|
-
"
|
|
97
|
+
"XSD schema validation (validate_xml_with) requires an " \
|
|
98
|
+
"XML engine with schema validation; add `gem \"nokogiri\"` " \
|
|
99
|
+
"to your Gemfile"
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
def self.nokogiri_schema_for(path)
|
|
103
|
+
@nokogiri_schemas.compute_if_absent(path) do
|
|
104
|
+
::Nokogiri::XML::Schema(File.read(path))
|
|
105
|
+
end
|
|
61
106
|
end
|
|
62
107
|
|
|
63
|
-
private_class_method :ensure_nokogiri!, :
|
|
108
|
+
private_class_method :ensure_nokogiri!, :nokogiri_schema_for
|
|
64
109
|
end
|
|
65
110
|
end
|
|
66
111
|
end
|
|
@@ -185,6 +185,16 @@ RSpec.describe "validate_xml_with XSD constructs" do
|
|
|
185
185
|
end
|
|
186
186
|
|
|
187
187
|
it "rejects a document missing the target namespace" do
|
|
188
|
+
# leptris's XSD compiler does not yet enforce targetNamespace
|
|
189
|
+
# qualification (leptris/leptris-ruby#679); the cell gates on the
|
|
190
|
+
# engine until the engine fix lands. The default nokogiri lane
|
|
191
|
+
# enforces it and runs the assertion.
|
|
192
|
+
engine = Lutaml::Model::Config.xml_adapter_type&.to_s
|
|
193
|
+
if engine == "leptris"
|
|
194
|
+
skip "leptris targetNamespace enforcement pending " \
|
|
195
|
+
"leptris/leptris-ruby#679"
|
|
196
|
+
end
|
|
197
|
+
|
|
188
198
|
xml = "<address><city>Berlin</city></address>"
|
|
189
199
|
|
|
190
200
|
expect(XsdNamespacedDoc.validate_xml(xml)).not_to be_empty
|
|
@@ -84,10 +84,13 @@ RSpec.describe "validate_xml_with" do
|
|
|
84
84
|
end.to raise_error(Lutaml::Model::ValidationError, /age/)
|
|
85
85
|
end
|
|
86
86
|
|
|
87
|
+
# Malformed input raises instead of recovering; the error class is
|
|
88
|
+
# moxml's normalized ParseError (engine-independent, line and column
|
|
89
|
+
# carried) now that validation routes through the moxml layer (#1184).
|
|
87
90
|
it "raises a parse error for malformed XML instead of recovering" do
|
|
88
91
|
expect do
|
|
89
92
|
XsdValidatedPerson.validate_xml("<person><name>Alice</name>")
|
|
90
|
-
end.to raise_error(
|
|
93
|
+
end.to raise_error(Moxml::ParseError, /Line: 1/)
|
|
91
94
|
end
|
|
92
95
|
end
|
|
93
96
|
|