PureXML/Tests/SchemaAnySimpleTypeFacetTests.swift
Mihaela Mihaljevic f8ba6fef5e fix(schema): reject a constraining facet on anySimpleType-based simpleContent
A constraining facet may not be applied where a simpleContent restriction's base
chain resolves to xs:anySimpleType, the ur simple type, which carries no
constraining facets (XSD Part 2 3.2.1); stZ010 (a complex type extending
xs:anySimpleType, restricted by another that adds minLength) is now rejected.

The base chain is followed through local complex types' own simpleContent bases;
it stops at any other built-in, a simple type, or an external/unresolved base, and
a restriction supplying its own nested simpleType base is skipped (the facet
applies to that), so a facet on a string-, int-, or other real-typed simpleContent
stays accepted.

XSTS invalid-schemas-accepted 49 -> 48, no false positive (valid-schemas-rejected
and valid-instances-rejected held at 0; an adversarial critic probed 12 valid
schemas across every resolver branch and confirmed FP-safety), no other bucket
moved.
2026-06-18 19:27:28 +02:00

47 lines
2.1 KiB
Swift

@testable import PureXML
import Testing
/// A constraining facet may not be applied where a `simpleContent` restriction's base
/// chain resolves to `xs:anySimpleType`, the ur simple type, which carries no
/// constraining facets (XSD Part 2 §3.2.1, W3C stZ010). A base that bottoms at a real
/// built-in or a user simple type still admits its applicable facets.
@Suite("anySimpleType simpleContent facet applicability")
struct SchemaAnySimpleTypeFacetTests {
private func compile(_ schema: String) throws -> PureXML.Schema.Document {
try PureXML.Schema.Document(schema)
}
private func chain(base: String, facet: String) -> String {
"<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'>"
+ "<xs:complexType name='t1'><xs:simpleContent><xs:extension base='\(base)'/></xs:simpleContent></xs:complexType>"
+ "<xs:complexType name='t2'><xs:simpleContent><xs:restriction base='t1'>\(facet)</xs:restriction></xs:simpleContent></xs:complexType>"
+ "</xs:schema>"
}
@Test("a facet on an anySimpleType-based simpleContent restriction is rejected")
func test_facetOnAnySimpleTypeRejected() throws {
#expect(throws: (any Error).self) {
try compile(chain(base: "xs:anySimpleType", facet: "<xs:minLength value='1'/>"))
}
}
@Test("minLength on a string-based simpleContent restriction is valid")
func test_facetOnStringAccepted() throws {
_ = try compile(chain(base: "xs:string", facet: "<xs:minLength value='1'/>"))
}
@Test("maxInclusive on an int-based simpleContent restriction is valid")
func test_facetOnIntAccepted() throws {
_ = try compile(chain(base: "xs:int", facet: "<xs:maxInclusive value='5'/>"))
}
@Test("an anySimpleType extension with no facet restriction is valid")
func test_anySimpleTypeWithoutFacetAccepted() throws {
_ = try compile(
"<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'>"
+ "<xs:complexType name='t1'><xs:simpleContent><xs:extension base='xs:anySimpleType'/></xs:simpleContent></xs:complexType>"
+ "</xs:schema>",
)
}
}