fix(schema): enforce document-scoped xs:ID uniqueness and xs:IDREF resolution
xs:ID, xs:IDREF, and xs:IDREFS were validated only lexically (as NCNames); the document-wide rules that every ID is unique and every IDREF/IDREFS item matches some ID were not enforced, so duplicate IDs and dangling references passed. The tree validator now records ID and reference values during its typed walk over elements and attributes (a new IDTracker), and reports duplicate IDs and unresolved references after the whole document is seen, so a forward reference resolves against an ID declared later. XSTS: invalid instances accepted 179 to 166. Baseline updated; regression tests cover unique IDs, a forward reference, a duplicate ID, an unresolved IDREF, and IDREFS item resolution. The ID/IDREF helpers live in a ComplexValidator extension to keep the type within the file/body length limits. Refs #147, #129.
This commit is contained in:
parent
7f82627403
commit
77db89397b
@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||
|
||||
### Fixed
|
||||
|
||||
- Document-scoped `xs:ID`/`xs:IDREF` validation is now enforced (#147). `xs:ID`, `xs:IDREF`, and `xs:IDREFS` were checked only lexically (as NCNames); the document-wide constraints, that every ID is unique and every IDREF/IDREFS item matches some ID, were not. The tree validator now records ID and reference values during its typed walk (elements and attributes) and reports duplicate IDs and unresolved references once the whole document is seen, so a forward reference resolves against a later ID. XSTS: invalid instances accepted 179 to 166.
|
||||
- The regex `\w` and `\W` escapes now follow the XSD definition (#147). `\w` was the Perl class (alphabetic, decimal, underscore); XSD defines it as `[#x0000-#x10FFFF]-[\p{P}\p{Z}\p{C}]`, so symbols and marks are word characters and the connector `_` (Pc) is not. `\W` correspondingly matched symbols it should not. XSTS: invalid instances accepted 231 to 210. (Internal regex fixtures that assumed `_` was a word character were corrected.)
|
||||
- `block="substitution"` on a substitution-group head is now enforced (#147). The substitution filter only dropped members whose type derived from the head's type by a blocked method (`extension`/`restriction`); the `substitution` keyword, which forbids substitution-group substitution outright regardless of the member's type, was ignored, so a same-type member still substituted under a substitution block. XSTS: invalid instances accepted 238 to 231, valid schemas rejected 75 to 74.
|
||||
- Element `default` and `fixed` values are honored on empty elements (#147). An empty element with a value constraint takes that value, so the empty content is valid even when the type would reject the empty string; the constraint value (not the empty string) is what is validated against the type, including any `xsi:type` override. A restriction's `enumeration` now also replaces the inherited enumeration rather than unioning with it (the new set is the value space; `pattern` still accumulates and is ANDed), which a value-constraint case under `xsi:type` exposed. XSTS: valid instances rejected 395 to 369, invalid instances accepted 240 to 238.
|
||||
|
||||
@ -30,6 +30,10 @@ public extension PureXML.Schema {
|
||||
let elementBlock: [String: Set<DerivationMethod>]
|
||||
/// Each named complex type's base and derivation method.
|
||||
let typeDerivation: [String: TypeDerivation]
|
||||
/// Document-scoped xs:ID/xs:IDREF accumulator, filled during the typed walk
|
||||
/// and reported by `idErrors()` once the whole tree has been seen. See the
|
||||
/// ID/IDREF extension for `idErrors()` and `recordIDs(_:value:at:)`.
|
||||
let idTracker = IDTracker()
|
||||
|
||||
public init(
|
||||
types: [String: ElementType] = [:],
|
||||
@ -122,6 +126,7 @@ public extension PureXML.Schema {
|
||||
if let fixed = use.valueConstraint?.fixedValue, match.value != fixed {
|
||||
errors.append(XSDFailure(reason: "attribute '\(use.name.localName)' is fixed and must be '\(fixed)'", at: path))
|
||||
}
|
||||
recordIDs(use.type, value: match.value, at: path)
|
||||
} else if use.required {
|
||||
errors.append(XSDFailure(reason: "missing required attribute '\(use.name.localName)'", at: path))
|
||||
}
|
||||
@ -271,6 +276,7 @@ public extension PureXML.Schema {
|
||||
if let error = simple.validate(effective) {
|
||||
errors.append(XSDFailure(reason: "'\(child.name.localName)': \(error)", at: path))
|
||||
}
|
||||
recordIDs(simple, value: text, at: path)
|
||||
errors += elementFixedErrors(child, at: path)
|
||||
case let .complex(complex):
|
||||
errors.append(contentsOf: validate(child, against: complex, at: path))
|
||||
|
||||
23
Sources/Schema/ComplexValidatorIDs.swift
Normal file
23
Sources/Schema/ComplexValidatorIDs.swift
Normal file
@ -0,0 +1,23 @@
|
||||
extension PureXML.Schema.ComplexValidator {
|
||||
/// The document-scoped ID uniqueness and IDREF resolution errors gathered
|
||||
/// during validation. Call after validating the document root.
|
||||
func idErrors() -> [PureXML.Validation.ValidationError] {
|
||||
idTracker.errors()
|
||||
}
|
||||
|
||||
/// Records a value for document-scoped ID/IDREF checking when its type is
|
||||
/// xs:ID, xs:IDREF, or xs:IDREFS. Values are whitespace-collapsed (these types
|
||||
/// are NCName-based); an IDREFS value contributes one reference per list item.
|
||||
func recordIDs(_ type: PureXML.Schema.SimpleType, value: String, at path: [PureXML.Validation.PathKey]) {
|
||||
let normalized = PureXML.Schema.SimpleType.process(value, whiteSpace: .collapse)
|
||||
if type.isID {
|
||||
idTracker.recordID(normalized, at: path)
|
||||
} else if type.isIDReference {
|
||||
idTracker.recordReference(normalized, at: path)
|
||||
} else if type.isIDReferenceList {
|
||||
for token in normalized.split(separator: " ") {
|
||||
idTracker.recordReference(String(token), at: path)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
53
Sources/Schema/IDTracker.swift
Normal file
53
Sources/Schema/IDTracker.swift
Normal file
@ -0,0 +1,53 @@
|
||||
extension PureXML.Schema {
|
||||
/// Accumulates `xs:ID` and `xs:IDREF`/`xs:IDREFS` values across an instance
|
||||
/// document and reports the document-scoped violations XSD requires: every ID
|
||||
/// is unique across the document, and every IDREF/IDREFS item matches some ID.
|
||||
/// These are not simple-type (lexical) checks; they need the whole document,
|
||||
/// so the tree validator records here during its typed walk and reports after.
|
||||
final class IDTracker {
|
||||
private var seenIDs: Set<String> = []
|
||||
private var duplicates: [PureXML.Validation.ValidationError] = []
|
||||
private var references: [(value: String, path: [PureXML.Validation.PathKey])] = []
|
||||
|
||||
func recordID(_ value: String, at path: [PureXML.Validation.PathKey]) {
|
||||
if !seenIDs.insert(value).inserted {
|
||||
duplicates.append(.init(reason: "duplicate ID '\(value)'", at: path))
|
||||
}
|
||||
}
|
||||
|
||||
func recordReference(_ value: String, at path: [PureXML.Validation.PathKey]) {
|
||||
references.append((value, path))
|
||||
}
|
||||
|
||||
/// Duplicate-ID errors plus an error for every reference with no matching
|
||||
/// ID. Resolution runs after the whole document is seen, so a reference may
|
||||
/// point at an ID declared later.
|
||||
func errors() -> [PureXML.Validation.ValidationError] {
|
||||
var result = duplicates
|
||||
for reference in references where !seenIDs.contains(reference.value) {
|
||||
result.append(.init(reason: "IDREF '\(reference.value)' has no matching ID", at: reference.path))
|
||||
}
|
||||
return result
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension PureXML.Schema.SimpleType {
|
||||
/// Whether this is `xs:ID` (or an atomic restriction of it).
|
||||
var isID: Bool {
|
||||
if case .atomic = variety { return base == .id }
|
||||
return false
|
||||
}
|
||||
|
||||
/// Whether this is `xs:IDREF` (or an atomic restriction of it).
|
||||
var isIDReference: Bool {
|
||||
if case .atomic = variety { return base == .idref }
|
||||
return false
|
||||
}
|
||||
|
||||
/// Whether this is `xs:IDREFS` (a list whose item type is `xs:IDREF`).
|
||||
var isIDReferenceList: Bool {
|
||||
if case let .list(item) = variety { return item.base == .idref }
|
||||
return false
|
||||
}
|
||||
}
|
||||
@ -63,7 +63,7 @@ public extension PureXML.Validation {
|
||||
guard case let .element(root) = context.subject, let declaration = context.document.rootDeclaration else {
|
||||
return []
|
||||
}
|
||||
return PureXML.Schema.ComplexValidator(
|
||||
let validator = PureXML.Schema.ComplexValidator(
|
||||
types: context.document.types,
|
||||
nillableElements: context.document.nillableElements,
|
||||
elementConstraints: context.document.elementConstraints,
|
||||
@ -72,7 +72,11 @@ public extension PureXML.Validation {
|
||||
elementBlock: context.document.elementBlock,
|
||||
typeDerivation: context.document.typeDerivation,
|
||||
)
|
||||
.validate(root, as: declaration, at: [.element(root.name.description)])
|
||||
var errors = validator.validate(root, as: declaration, at: [.element(root.name.description)])
|
||||
// Document-scoped ID uniqueness and IDREF resolution, gathered
|
||||
// during the typed walk above, reported once the tree is seen.
|
||||
errors += validator.idErrors()
|
||||
return errors
|
||||
},
|
||||
when: { $0.codingPath.isEmpty },
|
||||
)
|
||||
|
||||
52
Tests/SchemaIDIDREFTests.swift
Normal file
52
Tests/SchemaIDIDREFTests.swift
Normal file
@ -0,0 +1,52 @@
|
||||
@testable import PureXML
|
||||
import Testing
|
||||
|
||||
/// Document-scoped xs:ID/xs:IDREF validation (#147, XSTS idConstrDefs set):
|
||||
/// every xs:ID value is unique across the document, and every xs:IDREF/xs:IDREFS
|
||||
/// item matches some ID. These had been checked only lexically (as NCNames).
|
||||
@Suite("ID and IDREF document constraints")
|
||||
struct SchemaIDIDREFTests {
|
||||
private var schema: PureXML.Schema.Document {
|
||||
get throws {
|
||||
try PureXML.Schema.Document("""
|
||||
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
|
||||
<xs:element name="root">
|
||||
<xs:complexType>
|
||||
<xs:choice minOccurs="0" maxOccurs="unbounded">
|
||||
<xs:element name="id" type="xs:ID"/>
|
||||
<xs:element name="ref" type="xs:IDREF"/>
|
||||
<xs:element name="refs" type="xs:IDREFS"/>
|
||||
</xs:choice>
|
||||
</xs:complexType>
|
||||
</xs:element>
|
||||
</xs:schema>
|
||||
""")
|
||||
}
|
||||
}
|
||||
|
||||
@Test("Unique IDs and a resolving reference validate")
|
||||
func test_valid() throws {
|
||||
#expect(try schema.validate("<root><id>a</id><id>b</id><ref>a</ref></root>").isEmpty)
|
||||
}
|
||||
|
||||
@Test("A forward IDREF resolves against an ID declared later")
|
||||
func test_forwardReference() throws {
|
||||
#expect(try schema.validate("<root><ref>a</ref><id>a</id></root>").isEmpty)
|
||||
}
|
||||
|
||||
@Test("A duplicate ID is rejected")
|
||||
func test_duplicateID() throws {
|
||||
#expect(try !schema.validate("<root><id>a</id><id>a</id></root>").isEmpty)
|
||||
}
|
||||
|
||||
@Test("An IDREF with no matching ID is rejected")
|
||||
func test_unresolvedReference() throws {
|
||||
#expect(try !schema.validate("<root><id>a</id><ref>x</ref></root>").isEmpty)
|
||||
}
|
||||
|
||||
@Test("IDREFS resolves each item; one unmatched item fails")
|
||||
func test_idrefs() throws {
|
||||
#expect(try schema.validate("<root><id>a</id><id>b</id><refs>a b</refs></root>").isEmpty)
|
||||
#expect(try !schema.validate("<root><id>a</id><refs>a x</refs></root>").isEmpty)
|
||||
}
|
||||
}
|
||||
@ -27,9 +27,11 @@ struct SchemaListFacetTests {
|
||||
#expect(try !doc.validate("<v>alpha beta gamma delta</v>").isEmpty) // 4 items: invalid
|
||||
}
|
||||
|
||||
@Test("minLength and maxLength on IDREFS count items")
|
||||
func test_idrefsMinMax() throws {
|
||||
let doc = try schema("xs:IDREFS", #"<xs:minLength value="2"/><xs:maxLength value="3"/>"#)
|
||||
/// ENTITIES would drag in entity declarations and IDREFS in document-scoped
|
||||
/// reference resolution; NMTOKENS isolates the list length facet itself.
|
||||
@Test("minLength and maxLength on a built-in list count items")
|
||||
func test_listMinMax() throws {
|
||||
let doc = try schema("xs:NMTOKENS", #"<xs:minLength value="2"/><xs:maxLength value="3"/>"#)
|
||||
#expect(try doc.validate("<v>a b</v>").isEmpty) // 2 items: valid
|
||||
#expect(try doc.validate("<v>a b c</v>").isEmpty) // 3 items: valid
|
||||
#expect(try !doc.validate("<v>a</v>").isEmpty) // 1 item: invalid
|
||||
|
||||
@ -31,7 +31,7 @@ struct XSTSSuiteTests {
|
||||
private let knownSchemaValidRejected = 72
|
||||
private let knownSchemaInvalidAccepted = 2461
|
||||
private let knownInstanceValidRejected = 338
|
||||
private let knownInstanceInvalidAccepted = 179
|
||||
private let knownInstanceInvalidAccepted = 166
|
||||
|
||||
@Test("Every XSTS case behaves: compile, reject, validate, invalidate")
|
||||
func test_suite() throws {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user