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: 13419629c459d2ee88077db274555ff9fdca0e8bc316a108c807ebb7419a58b1
4
- data.tar.gz: fc9b9f1d442fdcd8fa0055be53f6268467c7122e8c916962e40b8a622321807f
3
+ metadata.gz: 2992312b17ca6365a073615318db447cca51e3aa2c7f7c9279565faa370cbdc3
4
+ data.tar.gz: 7b41def8578756a2e1e36a693a5b36172359ddbc170007a26f0848ed1a30eb85
5
5
  SHA512:
6
- metadata.gz: 6175ce456fc919dd13b3eed5f081664a7bdd4326490d8a3cea79e95c848cefd670fbd39d523266410bc19861ca248f4091c2de0c945e48109352e7303ce29024
7
- data.tar.gz: 3cf9f3519e927d623567b85b232a2407ae3e9454bed140feed8e2bf9f84cf47389803c5e01383f848a3accc39cc7d0f2ef4b57b1cc3dc4903c995f5a8f6305e3
6
+ metadata.gz: 21cf0a02683ad8ccefce980343448195a40fdf48c49ec7420b606d3ee8543366d3e5e4cefef62381c6a5b9a9e341c0d8f67b27c75a976618a9db4f54996d6b80
7
+ data.tar.gz: 40a5bb7ec659bda3d112b710146ad4a6b9e678dab938bc76dbcaadc2a970cfbb8b2f464edf8579a2e21ef82ea2f8d84f55fc09923dd3d661ee4bf3debe59f911
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Lutaml
4
4
  module Model
5
- VERSION = "0.8.177"
5
+ VERSION = "0.8.178"
6
6
  end
7
7
  end
@@ -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). Nokogiri is
10
- # required lazily so models that do not use XSD validation keep working
11
- # on platforms/adapters without it (Opal, ox-only installs).
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 the " \
35
- "nokogiri gem, which is not available under Opal."
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
- ensure_nokogiri!
39
- # Strict parsing: libxml2 recovery would otherwise repair malformed
40
- # input and report it as schema-valid. NONET remains on by default.
41
- document = ::Nokogiri::XML(xml, &:strict)
42
- paths.flat_map do |path|
43
- schema_for(path)
44
- .validate(document)
45
- .map { |error| Error::SchemaValidationError.new(error.message, path) }
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) do
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 the " \
60
- "nokogiri gem; add `gem \"nokogiri\"` to your Gemfile"
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!, :schema_for
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(Nokogiri::XML::SyntaxError)
93
+ end.to raise_error(Moxml::ParseError, /Line: 1/)
91
94
  end
92
95
  end
93
96
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lutaml-model
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.177
4
+ version: 0.8.178
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ribose Inc.