diff --git a/CHANGELOG.md b/CHANGELOG.md index 9f81ef5..b959257 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/Sources/Schema/ComplexValidator.swift b/Sources/Schema/ComplexValidator.swift index a35a1dd..ad0f3a7 100644 --- a/Sources/Schema/ComplexValidator.swift +++ b/Sources/Schema/ComplexValidator.swift @@ -30,6 +30,10 @@ public extension PureXML.Schema { let elementBlock: [String: Set] /// 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)) diff --git a/Sources/Schema/ComplexValidatorIDs.swift b/Sources/Schema/ComplexValidatorIDs.swift new file mode 100644 index 0000000..610a01a --- /dev/null +++ b/Sources/Schema/ComplexValidatorIDs.swift @@ -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) + } + } + } +} diff --git a/Sources/Schema/IDTracker.swift b/Sources/Schema/IDTracker.swift new file mode 100644 index 0000000..90e6e5e --- /dev/null +++ b/Sources/Schema/IDTracker.swift @@ -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 = [] + 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 + } +} diff --git a/Sources/Schema/XSDValidations.swift b/Sources/Schema/XSDValidations.swift index 9030a19..7bfb4a7 100644 --- a/Sources/Schema/XSDValidations.swift +++ b/Sources/Schema/XSDValidations.swift @@ -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 }, ) diff --git a/Tests/SchemaIDIDREFTests.swift b/Tests/SchemaIDIDREFTests.swift new file mode 100644 index 0000000..bcb93fe --- /dev/null +++ b/Tests/SchemaIDIDREFTests.swift @@ -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(""" + + + + + + + + + + + + """) + } + } + + @Test("Unique IDs and a resolving reference validate") + func test_valid() throws { + #expect(try schema.validate("aba").isEmpty) + } + + @Test("A forward IDREF resolves against an ID declared later") + func test_forwardReference() throws { + #expect(try schema.validate("aa").isEmpty) + } + + @Test("A duplicate ID is rejected") + func test_duplicateID() throws { + #expect(try !schema.validate("aa").isEmpty) + } + + @Test("An IDREF with no matching ID is rejected") + func test_unresolvedReference() throws { + #expect(try !schema.validate("ax").isEmpty) + } + + @Test("IDREFS resolves each item; one unmatched item fails") + func test_idrefs() throws { + #expect(try schema.validate("aba b").isEmpty) + #expect(try !schema.validate("aa x").isEmpty) + } +} diff --git a/Tests/SchemaListFacetTests.swift b/Tests/SchemaListFacetTests.swift index 0a4caf9..c01ff42 100644 --- a/Tests/SchemaListFacetTests.swift +++ b/Tests/SchemaListFacetTests.swift @@ -27,9 +27,11 @@ struct SchemaListFacetTests { #expect(try !doc.validate("alpha beta gamma delta").isEmpty) // 4 items: invalid } - @Test("minLength and maxLength on IDREFS count items") - func test_idrefsMinMax() throws { - let doc = try schema("xs:IDREFS", #""#) + /// 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", #""#) #expect(try doc.validate("a b").isEmpty) // 2 items: valid #expect(try doc.validate("a b c").isEmpty) // 3 items: valid #expect(try !doc.validate("a").isEmpty) // 1 item: invalid diff --git a/Tests/XSTSSuiteTests.swift b/Tests/XSTSSuiteTests.swift index 6882afb..945bd45 100644 --- a/Tests/XSTSSuiteTests.swift +++ b/Tests/XSTSSuiteTests.swift @@ -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 {