diff --git a/CHANGELOG.md b/CHANGELOG.md index fd1e786..147771a 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 (#181, #147): the `xsi:type` not-derived check (`cvc-elt.4.3.2.1`) now covers ATOMIC simple substitutes, not only complex ones. An `xsi:type` naming an atomic type that is not derived from the element's declared type, such as a sibling restriction or the declared type's own ancestor, is rejected. The check still fires only when both the substitute and the declared type are recorded in the namespaced derivation backbone with no path between them, so a built-in substitute or declared type (absent from the backbone) and the ur-types stay silent (under-reject), avoiding any built-in-derivation false positive; a genuinely derived restriction substitute is accepted. invalid-instances-accepted 66 → 60, no false positive (valid-instances-rejected and valid-schemas-rejected held at 0), no other bucket moved. + - XSTS invalid-instances-accepted (#181, #147): an `xsi:type` naming a list or union simple type is now rejected unless the element's declared type is a ur-type (`anySimpleType`/`anyType`). A list or union type derives only from `anySimpleType` (XSD Part 2), so it cannot validly substitute for an element of any more specific atomic or complex type (`cvc-elt.4.3.2.1`); naming one on, say, an `xs:int`-declared element is invalid. A genuinely derived (restriction) substitute, a list/union under an `anySimpleType` declaration, and a declared type that is itself a list or union are all left alone, so no valid instance is newly rejected. The declared type is resolved through the element-ref / type-reference chain, so the check also fires for an element reached via `` whose global declaration is atomic. This widening became safe once the derivation backbone was keyed by namespaced identity (#186); the conservative atomic/built-in not-derived cases remain under-rejected pending the full type-identity relation. invalid-instances-accepted 81 → 66, no false positive (valid-instances-rejected and valid-schemas-rejected held at 0), no other bucket moved. - XSTS invalid-instances-accepted (#186, #147): the instance-validity derivation and substitution tables are now keyed by namespaced identity (`{ns}local`) instead of bare local name, so two same-local-name types or elements in different namespaces no longer collide. `DerivationTables` keeps its bare-keyed fields for the schema-validity machinery (Particle-Valid-Restriction and the consistency rules) and gains parallel namespaced fields built in the same pass; only the instance consumers (the `xsi:type` blocked/not-derived/abstract checks, the substitution-group closure, and `referenceParticle`) read the namespaced tables, reducing a `type:{ns}local` reference, a bare name, or an element ref to a `{ns}local` key. A key miss stays permissive (no constraint), so the re-keying cannot over-reject; the single-document corpus behaviour is unchanged. With the namespaced backbone in place, a schema's `blockDefault` now supplies the `{disallowed substitutions}` (`block`) of every element and complex type that states none (own `block` overrides; `block=""` clears; intersected with the methods each component admits), without one namespace's defaults shadowing another's. invalid-instances-accepted 89 → 81, no false positive (valid-instances-rejected and valid-schemas-rejected held at 0; an adversarial review exercised same- and cross-namespace substitutions, no-targetNamespace keys, transitive substitution groups, and blockDefault override cases), no other bucket moved. diff --git a/Sources/Schema/ComplexValidatorDerivation.swift b/Sources/Schema/ComplexValidatorDerivation.swift index 4cb4fbd..31fadb9 100644 --- a/Sources/Schema/ComplexValidatorDerivation.swift +++ b/Sources/Schema/ComplexValidatorDerivation.swift @@ -87,6 +87,17 @@ extension PureXML.Schema.ComplexValidator { } } + /// Whether a substitute type participates in the not-derived check: a complex + /// type, or an ATOMIC simple type. A list or union substitute is excluded here + /// (it is handled by ``listUnionSubstitutionError``). + static func isAtomicOrComplex(_ type: PureXML.Schema.ElementType) -> Bool { + switch type { + case .complex: true + case let .simple(simple): !isListOrUnion(simple) + case .typeReference: false + } + } + /// The error when an element's declared type is an abstract complex type and /// the element supplies no `xsi:type` to name a concrete derived type. An /// abstract type cannot itself be the type of an instance element. @@ -141,13 +152,14 @@ extension PureXML.Schema.ComplexValidator { } /// cvc-elt.4.3.2.1: an `xsi:type` must resolve to a type DERIVED from the - /// element's declared type. This flags ONLY the case where both the declared - /// type and the substitute resolve to COMPLEX types recorded in the derivation - /// backbone and there is no derivation path between them (a different branch of - /// the hierarchy). It deliberately does NOT touch a simple/list/union substitute, - /// a substitute or declared type absent from the backbone, or the ur-types: there - /// the relationship cannot be confirmed from the complex-type backbone alone, so - /// the rule stays silent (under-reject) rather than risk a false positive. + /// element's declared type. This flags the case where both the declared type and + /// the substitute resolve to complex or atomic-simple types recorded in the + /// derivation backbone and there is no derivation path between them (a different + /// branch of the hierarchy, including a substitute that is the declared type's + /// own ancestor). A list or union substitute is handled separately by + /// ``listUnionSubstitutionError``; a substitute or declared type ABSENT from the + /// backbone (a built-in, or the ur-types) leaves the relationship unconfirmable, + /// so the rule stays silent (under-reject) rather than risk a false positive. func notDerivedSubstitutionError( declared: PureXML.Schema.ElementType, child: PureXML.Model.Element, @@ -157,7 +169,8 @@ extension PureXML.Schema.ComplexValidator { guard case let .typeReference(reference) = declared, let substituteLabel = Self.xsiTypeName(child), let substituteRef = Self.xsiTypeReference(child, namespaceBindings: namespaceBindings), - case .complex? = Self.resolveNamedType(substituteRef, in: types) + let substituteType = Self.resolveNamedType(substituteRef, in: types), + Self.isAtomicOrComplex(substituteType) else { return nil } let substituteKey = Self.derivationKey(fromReference: substituteRef) let declaredName = resolvedDeclaredTypeName(reference) diff --git a/Tests/SchemaListUnionXsiTypeTests.swift b/Tests/SchemaListUnionXsiTypeTests.swift index ed638b8..48b6c43 100644 --- a/Tests/SchemaListUnionXsiTypeTests.swift +++ b/Tests/SchemaListUnionXsiTypeTests.swift @@ -58,4 +58,29 @@ struct SchemaListUnionXsiTypeTests { let xml = #"1 2"# #expect(try !schema.validate(xml).isEmpty) } + + /// The not-derived check also covers ATOMIC substitutes recorded in the + /// backbone: an `xsi:type` naming a sibling type, or the declared type's own + /// ancestor, is rejected; a genuinely derived restriction is accepted. + @Test("an atomic xsi:type not derived from the declared type is rejected") + func test_atomicNotDerivedRejected() throws { + let schema = try PureXML.Schema.Document(""" + + + + + + + + """) + func doc(_ type: String) -> String { + #"5"# + } + // A sibling (also restricts Base) is not derived from A: rejected. + #expect(try !schema.validate(doc("Sibling")).isEmpty) + // The declared type's own ancestor Base is not derived from A: rejected. + #expect(try !schema.validate(doc("Base")).isEmpty) + // A genuine restriction of A is validly derived: accepted. + #expect(try schema.validate(doc("Derived")).isEmpty) + } } diff --git a/Tests/XSTSSuiteTests.swift b/Tests/XSTSSuiteTests.swift index 3cc8191..fdef64d 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: xsi:type must derive from the declared type (133 -> 107); block="substitution" - /// bars members even for an untyped head (107 -> 101); ur-type `xsd:anyType` wildcards are `lax` - /// (101 -> 95); identity fields compare in the decimal value space (95 -> 94); attribute wildcard - /// is the INTERSECTION of its sources (94 -> 89); `blockDefault` supplies a missing `block` after - /// re-keying derivation by namespace (89 -> 81); a list/union xsi:type is not derived from a - /// non-ur declared type, resolved through an element-ref chain (81 -> 66). - private let knownInstanceInvalidAccepted = 66 + /// Instance bucket (133 -> 60), 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; a complex type's attribute wildcard is the + /// INTERSECTION of its `anyAttribute` sources. + private let knownInstanceInvalidAccepted = 60 @Test("Every XSTS case behaves: compile, reject, validate, invalidate") func test_suite() throws {