fix(schema): compare identity-constraint fields across the decimal value space; invalid-instances-accepted 95 to 94 (#147)

An identity constraint (unique/key/keyref) compares its field values in their
value space. Two fields typed by different members of the decimal-derived
numeric family denote the same value when their numbers are equal, so a
decimal "1" and an unsignedByte "1" (supplied here via xsi:type) collide for a
unique. FieldValue equality previously required the two declared base types to
match exactly and reported them unequal when they differed, so the duplicate
went undetected.

Add BuiltinType.isDecimalDerived (decimal plus the integer family, which share
one value space) and compare those values as DecimalValue. The unification is
confined to that family: float and double are distinct primitive spaces and a
number is never equal to a string, so no valid instance is newly rejected.

XSTS invalid-instances-accepted 95 to 94, no false positive (valid-instances-
rejected and valid-schemas-rejected held at 0 across the whole corpus), no
other bucket moved.
This commit is contained in:
Mihaela Mihaljevic 2026-06-18 11:01:05 +02:00
parent 94c6950ae3
commit cadfb0d871
5 changed files with 79 additions and 3 deletions

View File

@ -21,6 +21,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed
- XSTS invalid-instances-accepted (#147): identity-constraint (`unique`/`key`/`keyref`) field values are now compared across the decimal-derived numeric family, so two fields typed by different members of that family with the same number collide. A field typed `xs:decimal` with value `1` and a field typed `xs:unsignedByte` with value `1` (e.g. supplied via `xsi:type`) are the same value and violate a `unique`, where previously a difference in declared base type made them compare unequal. The unification is confined to `xs:decimal` and the integer types, which share one value space; distinct primitive spaces (a number versus a string, or `float` versus `double`) are never unified, so no valid instance is newly rejected. invalid-instances-accepted 95 → 94, no false positive (valid-instances-rejected and valid-schemas-rejected held at 0), no other bucket moved.
- XSTS invalid-instances-accepted (#146, #147): the ur-type `xsd:anyType` now processes its element and attribute wildcards as `lax`, not `skip` (XSD 1.0 §3.4.7). A child element or attribute of an untyped element (or an element typed `xs:anyType`) that has a global declaration is validated against it; undeclared content is still admitted. Previously such content was skipped entirely, so an invalid declared child (e.g. a `duration`-typed attribute with value `"P"` reached through `<xsd:anyAttribute/>`) was accepted. The same change required making the wildcard lookup of a global element declaration namespace-exact: a global is always in the schema target namespace, so an unqualified instance element matches only a no-namespace global (the qualified key `element:{}local` already covers it); the previous bare-local-name fallback conflated namespaces and would have wrongly validated an unqualified child against a same-named namespaced declaration. invalid-instances-accepted 101 → 95, no false positive (valid-instances-rejected and valid-schemas-rejected held at 0; the spec's own `errC007` anyType-lax case validates correctly), no other bucket moved.
- XSTS invalid-instances-accepted (#147): `block="substitution"` (or a block set containing it, including `#all`) on a substitution-group head now bars every member from substituting even when the head is an abstract, untyped element. The substitution block is type-independent, but `filterSubstitutions` previously applied it only to a head that also had a `type`, so an untyped head's members still substituted. invalid-instances-accepted 107 → 101, no false positive (valid-instances-rejected and valid-schemas-rejected held at 0), no other bucket moved.

View File

@ -98,6 +98,15 @@ public extension PureXML.Schema {
return false
}
/// Whether this type's value space is the `decimal` value space: `decimal`
/// itself or any of the integer family (which derive from it). Values from
/// two such types compare in the one shared value space, so `1` (decimal)
/// equals `1` (unsignedByte). `float` and `double` are distinct primitive
/// value spaces and are deliberately excluded.
var isDecimalDerived: Bool {
self == .decimal || Self.integerLike.contains(self)
}
var primitive: Primitive {
if let primitive = Self.stringLike[self] { return primitive }
if Self.integerLike.contains(self) { return .integer }

View File

@ -36,11 +36,29 @@ private struct FieldValue: Equatable {
if lhsType.base == rhsType.base {
return lhsType.valueMatches(lhs.string, literal: rhs.string)
}
return false
return Self.numericallyEqual(lhs.string, lhsType.base, rhs.string, rhsType.base)
}
return lhs.string == rhs.string
}
/// Whether two values denote the same number across the decimal-derived
/// numeric family. `decimal` and the integer types share one value space, so
/// `1` (decimal) equals `1` (unsignedByte). Distinct primitive spaces (a
/// number versus a string, or `float`/`double`) are excluded, so they never
/// compare equal here.
private static func numericallyEqual(
_ left: String,
_ leftBase: PureXML.Schema.BuiltinType,
_ right: String,
_ rightBase: PureXML.Schema.BuiltinType,
) -> Bool {
guard leftBase.isDecimalDerived, rightBase.isDecimalDerived,
let leftValue = PureXML.Schema.DecimalValue(left, allowFraction: true),
let rightValue = PureXML.Schema.DecimalValue(right, allowFraction: true)
else { return false }
return leftValue == rightValue
}
private var qnameValue: (uri: String?, local: String)? {
let trimmed = string.trimmingXMLWhitespace()
guard trimmed.contains(":") else { return nil }

View File

@ -0,0 +1,45 @@
@testable import PureXML
import Testing
/// Identity-constraint field values are compared in their shared value space. Two
/// fields typed by different members of the decimal-derived numeric family (e.g.
/// `xs:decimal` and `xs:unsignedByte`, selected via `xsi:type`) denote the same
/// value when their numbers are equal, so `1` and `1` collide for a `unique`.
/// Values in distinct primitive spaces never collide.
@Suite("Identity-constraint cross-type value equality")
struct SchemaIdentityValueEqualityTests {
/// Mirrors the W3C idF015 shape: an `xsd:anyType` element whose instances carry
/// `xsi:type` numeric overrides, gathered by a `unique`.
private func schema() throws -> PureXML.Schema.Document {
try PureXML.Schema.Document("""
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
<xsd:element name="root">
<xsd:complexType><xsd:sequence>
<xsd:element ref="uid" maxOccurs="unbounded"/>
</xsd:sequence></xsd:complexType>
<xsd:unique name="u"><xsd:selector xpath=".//uid"/><xsd:field xpath="."/></xsd:unique>
</xsd:element>
<xsd:element name="uid" type="xsd:anyType"/>
</xsd:schema>
""")
}
private func doc(_ decimalValue: String, _ unsignedValue: String) -> String {
"""
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<uid xsi:type="xsd:decimal">\(decimalValue)</uid>
<uid xsi:type="xsd:unsignedByte">\(unsignedValue)</uid>
</root>
"""
}
@Test("decimal and unsignedByte with the same number collide for unique")
func test_crossNumericTypesCollide() throws {
// 1 (decimal) and 1 (unsignedByte) are one value in the decimal value space.
#expect(try !schema().validate(doc("1", "1")).isEmpty)
// 1.0 (decimal) and 1 (unsignedByte) likewise.
#expect(try !schema().validate(doc("1.0", "1")).isEmpty)
// Distinct numbers do not collide.
#expect(try schema().validate(doc("1", "2")).isEmpty)
}
}

View File

@ -153,8 +153,10 @@ struct XSTSSuiteTests {
/// Instance bucket: xsi:type must derive from the declared type (cvc-elt.4.3.2.1,
/// 133 -> 107); block="substitution" bars all members whether or not the head is
/// typed (107 -> 101); the ur-type `xsd:anyType` processes its wildcards as `lax`
/// (XSD 1.0 §3.4.7), validating declared children/attributes (101 -> 95).
private let knownInstanceInvalidAccepted = 95
/// (XSD 1.0 §3.4.7), validating declared children/attributes (101 -> 95);
/// identity-constraint fields compare across the decimal-derived numeric family
/// (1 decimal equals 1 unsignedByte, 95 -> 94).
private let knownInstanceInvalidAccepted = 94
@Test("Every XSTS case behaves: compile, reject, validate, invalidate")
func test_suite() throws {