PureXML/Tests/SchemaAbstractXsiTypeTests.swift
Mihaela Mihaljevic 7e765a3bc1 fix(schema): reject an xsi:type that names an abstract type; invalid-instances-accepted 34 to 32 (#147)
An abstract type cannot be the type of an instance element (cvc-elt.4.3.2), so an
instance xsi:type naming an abstract type is invalid as a substitution even when
the element's declared type would otherwise admit it (typeDef01101, xsi:type on an
anyType-declared element). The validator only checked for an abstract DECLARED
type with no xsi:type; abstractXsiTypeError now also rejects an abstract xsi:type
substitute (matched against the namespaced abstract-type set). A concrete type is
unaffected.

invalid-instances-accepted 34 to 32, no false positive (valid-instances-rejected
and valid-schemas-rejected held at 0; a valid instance never names an abstract
type via xsi:type), no other bucket moved.
2026-06-18 15:58:08 +02:00

34 lines
1.2 KiB
Swift

@testable import PureXML
import Testing
/// An `xsi:type` may not name an abstract type: an abstract type cannot be the
/// type of an instance element (cvc-elt.4.3.2), so it is invalid as a substitution
/// even when the declared type would otherwise admit it. A concrete derived type
/// is accepted.
@Suite("abstract xsi:type")
struct SchemaAbstractXsiTypeTests {
private func schema() throws -> PureXML.Schema.Document {
try PureXML.Schema.Document("""
<xs:schema xmlns="u" xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="u">
<xs:element name="root"/>
<xs:complexType name="Real"/>
<xs:complexType name="Virtual" abstract="true"/>
</xs:schema>
""")
}
private func doc(_ type: String) -> String {
#"<root xmlns="u" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="\#(type)"/>"#
}
@Test("an abstract xsi:type is rejected")
func test_abstractRejected() throws {
#expect(try !schema().validate(doc("Virtual")).isEmpty)
}
@Test("a concrete xsi:type is accepted")
func test_concreteAccepted() throws {
#expect(try schema().validate(doc("Real")).isEmpty)
}
}