From 7e765a3bc13cdad293576cc99d58625030e22af3 Mon Sep 17 00:00:00 2001 From: Mihaela Mihaljevic Date: Thu, 18 Jun 2026 15:58:08 +0200 Subject: [PATCH] fix(schema): reject an xsi:type that names an abstract type; invalid-instances-accepted 34 to 32 (#147) An abstract type cannot be the type of an instance element (cvc-elt.4.3.2), so an instance xsi:type naming an abstract type is invalid as a substitution even when the element's declared type would otherwise admit it (typeDef01101, xsi:type on an anyType-declared element). The validator only checked for an abstract DECLARED type with no xsi:type; abstractXsiTypeError now also rejects an abstract xsi:type substitute (matched against the namespaced abstract-type set). A concrete type is unaffected. invalid-instances-accepted 34 to 32, no false positive (valid-instances-rejected and valid-schemas-rejected held at 0; a valid instance never names an abstract type via xsi:type), no other bucket moved. --- CHANGELOG.md | 2 ++ .../Schema/ComplexValidatorDerivation.swift | 20 +++++++++++ Tests/SchemaAbstractXsiTypeTests.swift | 33 +++++++++++++++++++ Tests/XSTSSuiteTests.swift | 4 +-- 4 files changed, 57 insertions(+), 2 deletions(-) create mode 100644 Tests/SchemaAbstractXsiTypeTests.swift diff --git a/CHANGELOG.md b/CHANGELOG.md index 806e562..fc6af39 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed +- XSTS invalid-instances-accepted (#147): an instance `xsi:type` may not name an abstract type. An abstract type cannot be the type of an instance element (cvc-elt.4.3.2), so it is invalid as a substitution even when the declared type would otherwise admit it (typeDef01101); a concrete type is unaffected. This complements the existing check for an abstract DECLARED type with no `xsi:type`. invalid-instances-accepted 34 → 32, no false positive (valid-instances-rejected and valid-schemas-rejected held at 0), no other bucket moved. + - XSTS invalid-instances-accepted (#147): an element with a `fixed` value constraint and a mixed content type must carry its fixed character value with no element children (cvc-elt.5.2.2.2.1). The fixed check validated only the concatenated text, so a mixed `fixed` element whose text matched but which also contained child elements (ctZ013) was accepted; element children are now rejected. An empty element (which takes the fixed value) and text equal to the fixed value remain valid. invalid-instances-accepted 37 → 34, no false positive (valid-instances-rejected and valid-schemas-rejected held at 0), no other bucket moved. - XSTS invalid-instances-accepted (#147): a `simpleContent` derivation whose base is another complex type with simpleContent now resolves through the chain to the underlying simple type, so the element's text is validated against it. A type restricting or extending a complex type that ultimately derives from a built-in (e.g. `Test` restricts `Test2` which extends `xs:int`) validates its content as that built-in rather than as unconstrained string; `baseTD00101`'s non-int text is now rejected. The base resolution recurses through `complexTypeNodes`, accumulating facets, with a cycle guard. invalid-instances-accepted 39 → 37, no false positive (valid-instances-rejected and valid-schemas-rejected held at 0), no other bucket moved. diff --git a/Sources/Schema/ComplexValidatorDerivation.swift b/Sources/Schema/ComplexValidatorDerivation.swift index 814489d..f8e00e6 100644 --- a/Sources/Schema/ComplexValidatorDerivation.swift +++ b/Sources/Schema/ComplexValidatorDerivation.swift @@ -17,10 +17,30 @@ extension PureXML.Schema.ComplexValidator { if let blocked = blockedSubstitutionError(declared: declared, child: child, at: path, namespaceBindings: namespaceBindings) { return blocked } if let notDerived = notDerivedSubstitutionError(declared: declared, child: child, at: path, namespaceBindings: namespaceBindings) { return notDerived } if let listUnion = listUnionSubstitutionError(declared: declared, child: child, at: path, namespaceBindings: namespaceBindings) { return listUnion } + if let abstractType = abstractXsiTypeError(child: child, at: path, namespaceBindings: namespaceBindings) { return abstractType } if case let .typeReference(name) = declared { return abstractTypeError(named: name, child: child, at: path) } return nil } + /// The error when an instance `xsi:type` names an ABSTRACT type: an abstract + /// type cannot be the type of an instance element (cvc-elt.4.3.2), so it may not + /// be used as a substitution. Distinct from ``abstractTypeError``, which covers + /// an abstract DECLARED type with no `xsi:type`. + func abstractXsiTypeError( + child: PureXML.Model.Element, + at path: [PureXML.Validation.PathKey], + namespaceBindings: [String: String], + ) -> PureXML.Validation.ValidationError? { + guard let label = Self.xsiTypeName(child), + let reference = Self.xsiTypeReference(child, namespaceBindings: namespaceBindings), + abstractTypes.contains(Self.derivationKey(fromReference: reference)) + else { return nil } + return PureXML.Validation.ValidationError( + reason: "xsi:type '\(label)' names an abstract type and may not be used as a substitution", + at: path, + ) + } + /// cvc-elt.4.3.2.1 for a list or union `xsi:type`: a list or union simple type /// is derived only from `anySimpleType`, so it can validly stand in only for an /// element whose declared type is a ur-type (`anySimpleType`/`anyType`). Naming a diff --git a/Tests/SchemaAbstractXsiTypeTests.swift b/Tests/SchemaAbstractXsiTypeTests.swift new file mode 100644 index 0000000..3413f17 --- /dev/null +++ b/Tests/SchemaAbstractXsiTypeTests.swift @@ -0,0 +1,33 @@ +@testable import PureXML +import Testing + +/// An `xsi:type` may not name an abstract type: an abstract type cannot be the +/// type of an instance element (cvc-elt.4.3.2), so it is invalid as a substitution +/// even when the declared type would otherwise admit it. A concrete derived type +/// is accepted. +@Suite("abstract xsi:type") +struct SchemaAbstractXsiTypeTests { + private func schema() throws -> PureXML.Schema.Document { + try PureXML.Schema.Document(""" + + + + + + """) + } + + private func doc(_ type: String) -> String { + #""# + } + + @Test("an abstract xsi:type is rejected") + func test_abstractRejected() throws { + #expect(try !schema().validate(doc("Virtual")).isEmpty) + } + + @Test("a concrete xsi:type is accepted") + func test_concreteAccepted() throws { + #expect(try schema().validate(doc("Real")).isEmpty) + } +} diff --git a/Tests/XSTSSuiteTests.swift b/Tests/XSTSSuiteTests.swift index c381610..ccdc350 100644 --- a/Tests/XSTSSuiteTests.swift +++ b/Tests/XSTSSuiteTests.swift @@ -150,13 +150,13 @@ struct XSTSSuiteTests { private let knownSchemaValidRejected = 0 private let knownSchemaInvalidAccepted = 69 private let knownInstanceValidRejected = 0 - /// Instance bucket (133 -> 34), per-step deltas in CHANGELOG.md: xsi:type must derive from the + /// Instance bucket (133 -> 32), per-step deltas in CHANGELOG.md: xsi:type must derive from the /// declared type (complex, then atomic and list/union, resolved through the element-ref chain); /// block/blockDefault (incl. "substitution" and an untyped head) bar substitutions after the /// derivation backbone was re-keyed by namespace; ur-type `xsd:anyType` wildcards are `lax`; /// identity fields compare in the decimal value space and take a declared default/fixed when /// absent or empty; a complex type's attribute wildcard is the INTERSECTION of its sources. - private let knownInstanceInvalidAccepted = 34 + private let knownInstanceInvalidAccepted = 32 @Test("Every XSTS case behaves: compile, reject, validate, invalidate") func test_suite() throws {