diff --git a/CHANGELOG.md b/CHANGELOG.md index 8559a98..8cbd287 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 +- Particle Valid (Restriction) now checks an element's type, not just its name (NameAndTypeOK, #158). When a restriction maps a same-named element onto a base element, the restricting type must derive from the base's; renaming an `xs:string` element to `xs:int` under restriction was accepted because only the names were compared. The check resolves both element types (following a type reference through the compiled type table) and, when both are atomic simple types, requires the restricting type's built-in base to derive from the base's; a complex, list, union, untyped, or unresolvable type is left alone, so no valid restriction is rejected. XSTS: invalid schemas accepted 621 to 620, no other bucket raised (valid schemas rejected held at 72). - An element/attribute `default`/`fixed` value must be a valid value of its `type` (Attribute/Element Locally Valid, XSD 1.0, #158). A `fixed="abc"` on an `xs:integer` or a `default="1 2"` on an `xs:int` was accepted because the value constraint was never checked at compile time. The value is now validated against its type when the type resolves to a built-in datatype (namespace-aware: only a `type` in the XSD namespace), normalising whitespace per the type as XSD requires (so `default=" 5 "` on an integer is valid); a named or inline user type is left to its own rules, so no valid value constraint is rejected. XSTS: invalid schemas accepted 638 to 621, no other bucket raised (valid schemas rejected held at 72). - A constraining facet must apply to its simple type's variety (XSD 1.0 Part 2, #158). A `list` may be constrained only by `length`/`minLength`/`maxLength`, `pattern`, `enumeration`, and `whiteSpace`; a `union` only by `pattern` and `enumeration`. A value-bound facet (`maxInclusive`, `totalDigits`, ...) constrains an atomic value space, so restricting a list (`list of integer`) by `maxInclusive` was wrongly accepted. The base's variety is resolved from the raw tree (the check runs before the types are compiled), namespace-aware so a user type that shares a built-in's local name does not flip the variety: a base in the XSD namespace is a built-in (only the list datatypes are list-variety), a base in the schema's own target namespace resolves to a declared global, and an external or unresolvable base is skipped, so no valid restriction is rejected. XSTS: invalid schemas accepted 664 to 638, no other bucket raised (valid schemas rejected held at 72). - A complexType's shorthand content (without `simpleContent`/`complexContent`) is now checked for order, not just cardinality (#158). Its model is `(annotation?, (group | all | choice | sequence)?, (attribute | attributeGroup)*, anyAttribute?)`: an optional model group, then attributes, then an optional `anyAttribute`. The existing content-shape check caught the wrong number of model groups and the exclusivity of a content spec, but not the order, so a model group after an attribute, an attribute after `anyAttribute`, or a second `anyAttribute` (the ctB family) was accepted. The complexType's direct children, excluding `annotation` and the content specs, are matched against the same ordered slot model used for a `complexContent` derivation. XSTS: invalid schemas accepted 687 to 664, no other bucket raised (valid schemas rejected held at 72). diff --git a/Sources/Schema/ParticleRestriction.swift b/Sources/Schema/ParticleRestriction.swift index ba67f85..9ef0e8c 100644 --- a/Sources/Schema/ParticleRestriction.swift +++ b/Sources/Schema/ParticleRestriction.swift @@ -9,8 +9,10 @@ extension PureXML.Schema { /// by per-particle checks, a documented simplification. enum ParticleRestriction { /// The reason a restriction's content is not a valid subset of its - /// base's, or nil when it is. - static func violation(restricted: ContentType, base: ContentType) -> String? { + /// base's, or nil when it is. `types` resolves an element's + /// ``ElementType/typeReference`` so element-vs-element pairs can check that + /// the restricting type derives from the base's (NameAndTypeOK). + static func violation(restricted: ContentType, base: ContentType, types: [String: ElementType]) -> String? { switch (restricted, base) { case (.empty, .empty), (.simpleContent, _), (_, .simpleContent): nil @@ -23,18 +25,18 @@ extension PureXML.Schema { case let (.elementOnly(restrictedParticle), .elementOnly(baseParticle)), let (.elementOnly(restrictedParticle), .mixed(baseParticle)), let (.mixed(restrictedParticle), .mixed(baseParticle)): - valid(restrictedParticle, baseParticle) + valid(restrictedParticle, baseParticle, types) ? nil : "the restricted content model is not a subset of the base's" } } /// Whether `restricted` accepts a subset of `base` (the pairwise check). - static func valid(_ restricted: Particle, _ base: Particle) -> Bool { + static func valid(_ restricted: Particle, _ base: Particle, _ types: [String: ElementType]) -> Bool { guard rangeSubsumed(restricted, base) else { return false } switch (restricted.term, base.term) { - case let (.element(restrictedName, _), .element(baseName, _)): - return sameName(restrictedName, baseName) + case let (.element(restrictedName, restrictedType), .element(baseName, baseType)): + return sameName(restrictedName, baseName) && elementTypeRestrictionOK(restrictedType, baseType, types) case let (.element(name, _), .wildcard(wildcard)): return wildcard.admits(name) case let (.wildcard(restrictedWildcard), .wildcard(baseWildcard)): @@ -42,9 +44,9 @@ extension PureXML.Schema { case let (.element, .group(baseGroup)): // RecurseAsIfGroup: the lone element, as a one-particle sequence, // against the base group. - return groupValid(Group(compositor: .sequence, particles: [restricted.withUnitRange()]), baseGroup) + return groupValid(Group(compositor: .sequence, particles: [restricted.withUnitRange()]), baseGroup, types) case let (.group(restrictedGroup), .group(baseGroup)): - return groupValid(restrictedGroup, baseGroup) + return groupValid(restrictedGroup, baseGroup, types) case let (.group(restrictedGroup), .wildcard(wildcard)): // NSRecurseCheckCardinality, namespace part: every leaf element // the group can contain must be admitted by the wildcard. @@ -54,20 +56,20 @@ extension PureXML.Schema { } } - private static func groupValid(_ restricted: Group, _ base: Group) -> Bool { + private static func groupValid(_ restricted: Group, _ base: Group, _ types: [String: ElementType]) -> Bool { switch (restricted.compositor, base.compositor) { case (.sequence, .sequence), (.all, .all): - recurse(restricted.particles, base.particles, skippedMustBeEmptiable: true) + recurse(restricted.particles, base.particles, skippedMustBeEmptiable: true, types) case (.choice, .choice): - recurse(restricted.particles, base.particles, skippedMustBeEmptiable: false) + recurse(restricted.particles, base.particles, skippedMustBeEmptiable: false, types) case (.sequence, .choice): // MapAndSum (simplified): each restricted particle fits some branch. restricted.particles.allSatisfy { particle in - base.particles.contains { valid(particle, $0) } + base.particles.contains { valid(particle, $0, types) } } case (.sequence, .all): // RecurseUnordered: map onto distinct base particles in any order. - recurseUnordered(restricted.particles, base.particles) + recurseUnordered(restricted.particles, base.particles, types) default: false } @@ -76,14 +78,14 @@ extension PureXML.Schema { /// Order-preserving mapping of the restricted particles onto the base's. /// A skipped base particle must be emptiable for sequence/all (Recurse); /// for choice (RecurseLax) skipping is free. - private static func recurse(_ restricted: [Particle], _ base: [Particle], skippedMustBeEmptiable: Bool) -> Bool { + private static func recurse(_ restricted: [Particle], _ base: [Particle], skippedMustBeEmptiable: Bool, _ types: [String: ElementType]) -> Bool { var baseIndex = 0 for particle in restricted { var matched = false while baseIndex < base.count { let candidate = base[baseIndex] baseIndex += 1 - if valid(particle, candidate) { + if valid(particle, candidate, types) { matched = true break } @@ -102,10 +104,10 @@ extension PureXML.Schema { /// Any-order mapping onto distinct base particles; unmapped base /// particles must be emptiable. - private static func recurseUnordered(_ restricted: [Particle], _ base: [Particle]) -> Bool { + private static func recurseUnordered(_ restricted: [Particle], _ base: [Particle], _ types: [String: ElementType]) -> Bool { var used = [Bool](repeating: false, count: base.count) for particle in restricted { - guard let index = base.indices.first(where: { !used[$0] && valid(particle, base[$0]) }) else { + guard let index = base.indices.first(where: { !used[$0] && valid(particle, base[$0], types) }) else { return false } used[index] = true @@ -113,6 +115,40 @@ extension PureXML.Schema { return base.indices.allSatisfy { used[$0] || emptiable(base[$0]) } } + /// A conservative slice of NameAndTypeOK: when a restricting element and the + /// base element it maps onto both resolve to atomic simple types, the + /// restricting type's built-in base must derive from the base's, so renaming + /// an `xs:string` element to `xs:int` under restriction is rejected. Any other + /// pairing (complex content, a list/union variety, an unresolvable or ur-type + /// reference) is permitted here, so no valid restriction is rejected. + private static func elementTypeRestrictionOK(_ restricted: ElementType?, _ base: ElementType?, _ types: [String: ElementType]) -> Bool { + guard let restrictedSimple = resolvedAtomic(restricted, types), + let baseSimple = resolvedAtomic(base, types) + else { return true } + return restrictedSimple.base.derives(from: baseSimple.base) + } + + /// The atomic ``SimpleType`` an element type resolves to (following + /// ``ElementType/typeReference`` through `types`), or nil for a complex, + /// list/union, or unresolvable type. + private static func resolvedAtomic(_ type: ElementType?, _ types: [String: ElementType]) -> SimpleType? { + var current = type + var steps = 0 + while let resolved = current, steps <= types.count { + switch resolved { + case let .simple(simple): + guard case .atomic = simple.variety else { return nil } + return simple + case .complex: + return nil + case let .typeReference(key): + current = types[key] + steps += 1 + } + } + return nil + } + /// Occurrence-range subsumption: the restriction may not occur less often /// than the base requires, nor more often than the base allows. private static func rangeSubsumed(_ restricted: Particle, _ base: Particle) -> Bool { diff --git a/Sources/Schema/XSDSchemaValidations.swift b/Sources/Schema/XSDSchemaValidations.swift index d348d72..ee08802 100644 --- a/Sources/Schema/XSDSchemaValidations.swift +++ b/Sources/Schema/XSDSchemaValidations.swift @@ -49,7 +49,7 @@ public extension PureXML.Validation { guard let derivation = context.subject.derivation, derivation.method == .restriction, case let .complex(restricted)? = context.document.types[context.subject.name], case let .complex(base)? = context.document.types[derivation.base], - let reason = PureXML.Schema.ParticleRestriction.violation(restricted: restricted.content, base: base.content) + let reason = PureXML.Schema.ParticleRestriction.violation(restricted: restricted.content, base: base.content, types: context.document.types) else { return [] } let text = "type '\(context.subject.name)' is not a valid restriction of '\(derivation.base)': \(reason)" return [ValidationError(reason: text, at: context.codingPath)] diff --git a/Tests/XSDParticleRestrictionTests.swift b/Tests/XSDParticleRestrictionTests.swift index 66d0c79..d16ee89 100644 --- a/Tests/XSDParticleRestrictionTests.swift +++ b/Tests/XSDParticleRestrictionTests.swift @@ -125,4 +125,24 @@ struct XSDParticleRestrictionTests { let failures = PureXML.Validation.Conformance.failures(in: cases) #expect(failures.isEmpty, "\(failures.map(\.reason))") } + + @Test("NameAndTypeOK: a same-name element must keep a type that derives from the base's") + func test_elementTypeDerivation() { + // Renaming the element's type to an incompatible one (string -> int) is not a + // valid restriction: int does not derive from string. + #expect(!compiles( + base: "", + restriction: "", + )) + // Narrowing to a derived type (integer -> int) is valid. + #expect(compiles( + base: "", + restriction: "", + )) + // Keeping the same type is valid. + #expect(compiles( + base: "", + restriction: "", + )) + } } diff --git a/Tests/XSTSSuiteTests.swift b/Tests/XSTSSuiteTests.swift index 24622df..41e2eaf 100644 --- a/Tests/XSTSSuiteTests.swift +++ b/Tests/XSTSSuiteTests.swift @@ -29,7 +29,7 @@ struct XSTSSuiteTests { /// XSTS` (debug is far slower and the corpus is large). Per-case deviations /// are written to /tmp/xsts-failures.txt for the burn-down. private let knownSchemaValidRejected = 72 - private let knownSchemaInvalidAccepted = 621 + private let knownSchemaInvalidAccepted = 620 private let knownInstanceValidRejected = 214 private let knownInstanceInvalidAccepted = 165 diff --git a/docs/schema-validity-burndown.md b/docs/schema-validity-burndown.md index 285d8c7..09ed3a5 100644 --- a/docs/schema-validity-burndown.md +++ b/docs/schema-validity-burndown.md @@ -10,7 +10,7 @@ irreducible tail. Do not deviate to new features while this is open. | Bucket | Count | Reading | |---|---|---| | valid-schemas-rejected | 72 | we rarely reject a good schema | -| **invalid-schemas-accepted** | **2461 -> 621** | we rarely catch a bad one (facet definition + applicability, default/fixed value validity, id, structural content-model, UPA determinism, complexContent + complexType content order, identity-constraint XPath-subset, and wildcard-namespace validity landed) | +| **invalid-schemas-accepted** | **2461 -> 620** | we rarely catch a bad one (facet definition + applicability, default/fixed value validity, particle-restriction name+type, id, structural content-model, UPA determinism, complexContent + complexType content order, identity-constraint XPath-subset, and wildcard-namespace validity landed) | | valid-instances-rejected | 233 | (instance side, separately tracked) | | invalid-instances-accepted | 165 | |