PureXML/Tests/SchemaNestedDefinitionTests.swift
Mihaela Mihaljevic 282ceb57fc feat(schema): a nested simpleType or attributeGroup may not be named (localSimpleType)
A simpleType nested inside a restriction/list/union/element/attribute is the
anonymous localSimpleType (no name), and a nested attributeGroup is always a
reference; named simpleType and attributeGroup definitions are top-level only (a
direct child of schema or redefine). A nested definition declaring a name was
accepted; it is now rejected. A nested anonymous simpleType, an attributeGroup
reference, and a top-level named definition are unaffected.

XSTS: invalid schemas accepted 403 to 394, no other bucket raised.
2026-06-15 15:41:48 +02:00

49 lines
2.0 KiB
Swift

@testable import PureXML
import Testing
@Suite("XSD nested definitions may not be named (localSimpleType / attributeGroup ref)")
struct SchemaNestedDefinitionTests {
private let xsd = "xmlns:xs=\"http://www.w3.org/2001/XMLSchema\""
private func compiles(_ body: String) -> Bool {
(try? PureXML.Schema.Document("<xs:schema \(xsd)>\(body)</xs:schema>")) != nil
}
@Test("A nested simpleType with a name is rejected")
func test_nestedNamedSimpleTypeRejected() {
#expect(!compiles(
"<xs:simpleType name=\"parent\"><xs:restriction>"
+ "<xs:simpleType name=\"foo\"><xs:restriction base=\"xs:string\"/></xs:simpleType>"
+ "</xs:restriction></xs:simpleType>",
))
}
@Test("A nested attributeGroup definition with a name is rejected")
func test_nestedNamedAttributeGroupRejected() {
#expect(!compiles(
"<xs:attributeGroup name=\"G\">"
+ "<xs:attributeGroup name=\"abc\"><xs:attribute name=\"a\" type=\"xs:string\"/></xs:attributeGroup>"
+ "</xs:attributeGroup>",
))
}
@Test("A nested anonymous simpleType compiles")
func test_nestedAnonymousSimpleTypeAccepted() {
#expect(compiles(
"<xs:element name=\"e\"><xs:simpleType><xs:restriction base=\"xs:string\"/></xs:simpleType></xs:element>",
))
#expect(compiles(
"<xs:simpleType name=\"L\"><xs:list><xs:simpleType><xs:restriction base=\"xs:string\"/></xs:simpleType></xs:list></xs:simpleType>",
))
}
@Test("A nested attributeGroup reference and top-level named definitions compile")
func test_refAndTopLevelAccepted() {
#expect(compiles(
"<xs:attributeGroup name=\"G\"><xs:attribute name=\"a\" type=\"xs:string\"/></xs:attributeGroup>"
+ "<xs:complexType name=\"T\"><xs:attributeGroup ref=\"G\"/></xs:complexType>",
))
#expect(compiles("<xs:simpleType name=\"S\"><xs:restriction base=\"xs:string\"/></xs:simpleType>"))
}
}