The identity-constraint location walk handles arbitrary selector depth, but the existing assertions only exercised a single relative step. Add a nested selector (group/item) whose duplicate sits in the second group, so the offender is a grandchild and an intermediate level needs a sibling index: list/group[2]/item. Locks the multi-level path construction.
134 lines
5.5 KiB
Swift
134 lines
5.5 KiB
Swift
@testable import PureXML
|
|
import Testing
|
|
|
|
@Suite("XSD identity constraints")
|
|
struct XSDIdentityTests {
|
|
private func validate(_ xsd: String, _ xml: String) throws -> [PureXML.Validation.ValidationError] {
|
|
try PureXML.Schema.Document(xsd).validate(xml)
|
|
}
|
|
|
|
private let head = "<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">"
|
|
|
|
@Test("xs:unique rejects a duplicated field value")
|
|
func test_unique() throws {
|
|
let xsd = """
|
|
\(head)
|
|
<xs:element name="list">
|
|
<xs:complexType>
|
|
<xs:sequence><xs:element name="item" type="xs:string" maxOccurs="unbounded"/></xs:sequence>
|
|
</xs:complexType>
|
|
<xs:unique name="byId">
|
|
<xs:selector xpath="item"/>
|
|
<xs:field xpath="@id"/>
|
|
</xs:unique>
|
|
</xs:element>
|
|
</xs:schema>
|
|
"""
|
|
#expect(try validate(xsd, "<list><item id=\"1\"/><item id=\"2\"/></list>").isEmpty)
|
|
#expect(try !validate(xsd, "<list><item id=\"1\"/><item id=\"1\"/></list>").isEmpty)
|
|
// unique tolerates a missing field
|
|
#expect(try validate(xsd, "<list><item/><item/></list>").isEmpty)
|
|
}
|
|
|
|
@Test("xs:key requires every field to be present and distinct")
|
|
func test_key() throws {
|
|
let xsd = """
|
|
\(head)
|
|
<xs:element name="list">
|
|
<xs:complexType>
|
|
<xs:sequence><xs:element name="item" type="xs:string" maxOccurs="unbounded"/></xs:sequence>
|
|
</xs:complexType>
|
|
<xs:key name="itemKey">
|
|
<xs:selector xpath="item"/>
|
|
<xs:field xpath="@id"/>
|
|
</xs:key>
|
|
</xs:element>
|
|
</xs:schema>
|
|
"""
|
|
#expect(try validate(xsd, "<list><item id=\"1\"/><item id=\"2\"/></list>").isEmpty)
|
|
#expect(try !validate(xsd, "<list><item id=\"1\"/><item id=\"1\"/></list>").isEmpty)
|
|
// a key field may not be absent
|
|
#expect(try !validate(xsd, "<list><item id=\"1\"/><item/></list>").isEmpty)
|
|
}
|
|
|
|
@Test("xs:keyref requires a matching key value")
|
|
func test_keyref() throws {
|
|
let xsd = """
|
|
\(head)
|
|
<xs:element name="orders">
|
|
<xs:complexType>
|
|
<xs:sequence>
|
|
<xs:element name="product" type="xs:string" maxOccurs="unbounded"/>
|
|
<xs:element name="line" type="xs:string" maxOccurs="unbounded"/>
|
|
</xs:sequence>
|
|
</xs:complexType>
|
|
<xs:key name="prodKey">
|
|
<xs:selector xpath="product"/>
|
|
<xs:field xpath="@code"/>
|
|
</xs:key>
|
|
<xs:keyref name="lineRef" refer="prodKey">
|
|
<xs:selector xpath="line"/>
|
|
<xs:field xpath="@product"/>
|
|
</xs:keyref>
|
|
</xs:element>
|
|
</xs:schema>
|
|
"""
|
|
let valid = "<orders><product code=\"A\"/><product code=\"B\"/><line product=\"A\"/></orders>"
|
|
let dangling = "<orders><product code=\"A\"/><line product=\"Z\"/></orders>"
|
|
#expect(try validate(xsd, valid).isEmpty)
|
|
// The dangling keyref is located at the offending <line>, not <orders>.
|
|
let failure = try #require(validate(xsd, dangling).first)
|
|
#expect(failure.codingPath.map(\.stringValue) == ["orders", "line"])
|
|
#expect(String(describing: failure).hasSuffix("at path: orders/line"))
|
|
}
|
|
|
|
@Test("Identity-constraint errors are located at the constraining element")
|
|
func test_identityErrorsCarryPath() throws {
|
|
let xsd = """
|
|
\(head)
|
|
<xs:element name="list">
|
|
<xs:complexType>
|
|
<xs:sequence><xs:element name="item" type="xs:string" maxOccurs="unbounded"/></xs:sequence>
|
|
</xs:complexType>
|
|
<xs:key name="itemKey">
|
|
<xs:selector xpath="item"/>
|
|
<xs:field xpath="@id"/>
|
|
</xs:key>
|
|
</xs:element>
|
|
</xs:schema>
|
|
"""
|
|
// The error locates the actual offending element (the duplicate item[2]),
|
|
// not just the element that declares the key.
|
|
let errors = try validate(xsd, "<list><item id=\"1\"/><item id=\"1\"/></list>")
|
|
let failure = try #require(errors.first)
|
|
#expect(failure.codingPath.map(\.stringValue) == ["list", "item"])
|
|
#expect(String(describing: failure).hasSuffix("at path: list/item[2]"))
|
|
}
|
|
|
|
@Test("A nested selector locates the offender at full depth with an intermediate index")
|
|
func test_identityErrorDeepPath() throws {
|
|
let xsd = """
|
|
\(head)
|
|
<xs:element name="list">
|
|
<xs:complexType>
|
|
<xs:sequence>
|
|
<xs:element name="group" maxOccurs="unbounded">
|
|
<xs:complexType>
|
|
<xs:sequence><xs:element name="item" type="xs:string" maxOccurs="unbounded"/></xs:sequence>
|
|
</xs:complexType>
|
|
</xs:element>
|
|
</xs:sequence>
|
|
</xs:complexType>
|
|
<xs:key name="k"><xs:selector xpath="group/item"/><xs:field xpath="@id"/></xs:key>
|
|
</xs:element>
|
|
</xs:schema>
|
|
"""
|
|
// The duplicate id "1" appears in the second group, so the offender is
|
|
// list/group[2]/item: a grandchild, with the index on the intermediate group.
|
|
let xml = "<list><group><item id=\"1\"/></group><group><item id=\"1\"/></group></list>"
|
|
let failure = try #require(validate(xsd, xml).first)
|
|
#expect(failure.codingPath.map(\.stringValue) == ["list", "group", "item"])
|
|
#expect(String(describing: failure).hasSuffix("at path: list/group[2]/item"))
|
|
}
|
|
}
|