fix(schema): enforce the NameAndTypeOK block-superset rule (#145)

Particle Valid (Restriction) now requires a restricting element's {disallowed
substitutions} to be a superset of the base element's: a restriction may block
more substitutions, never fewer. The element particle (Term.element) carries a
block set, populated from the element's block attribute or the schema's
blockDefault.

XSTS invalid-schemas-accepted 179 to 163, no other bucket moved.
This commit is contained in:
Mihaela Mihaljevic 2026-06-17 16:45:35 +02:00
parent c3876a261f
commit ee5d180ba3
11 changed files with 32 additions and 14 deletions

View File

@ -22,6 +22,8 @@ Second pre-release. Continues the W3C XSTS schema-validity campaign: **invalid-s
### Fixed
- XSTS invalid-schemas-accepted (#145): Particle Valid (Restriction) now enforces the NameAndTypeOK block-superset clause. A restricting element's `{disallowed substitutions}` (its `block`, or the schema's `blockDefault`) must be a superset of the base element's, so a restriction may block more substitutions but never fewer. The element particle now carries this `block` set. Caught sixteen invalid schemas: invalid-schemas-accepted 179 → 163, no other bucket moved.
- XSTS invalid-schemas-accepted (#145): the RecurseAsIfGroup case of Particle Valid (Restriction) now checks occurrence. An element restricting a base model group is treated as a synthetic group with `minOccurs=maxOccurs=1` holding the element (XSD 1.0 §3.9.6), so a base group that must occur two or more times cannot be restricted to a single element. The element's own occurrence is still matched against the base branch it maps to, so an optional element restricting a once-or-more base group stays valid (no false rejection). Caught two invalid schemas: invalid-schemas-accepted 181 → 179, no other bucket moved.
- XSTS invalid-schemas-accepted (#145): a `redefine` is now checked against the schema it redefines once that schema is loaded: every component the `redefine` names (a `complexType`, `simpleType`, `group`, or `attributeGroup`) must have a same-kind component of the same name in the redefined schema (`src-redefine.6`/`7.2.1`), and the same component may not be redefined twice in one `redefine` (`src-redefine.7.2.2`). A redefinition naming a component that does not exist there, or a duplicate redefinition, is rejected. The check runs only when the redefined document was loaded through a `schemaLoader`, so a stand-alone schema is unaffected. Caught two invalid schemas: invalid-schemas-accepted 183 → 181, no other bucket moved.

View File

@ -104,7 +104,10 @@ public extension PureXML.Schema {
/// (a built-in, a user type, or `anyType` for an absent type), or nil when
/// the type is an inline anonymous definition. It preserves the derivation
/// identity the flattened `type` (`ElementType`) loses, for NameAndTypeOK.
case element(name: PureXML.Model.QualifiedName, type: ElementType?, typeName: String?, valueConstraint: ValueConstraint? = nil)
/// `block` is the element's `{disallowed substitutions}` (its own `block`, or
/// the schema's `blockDefault`), needed for the NameAndTypeOK block-superset
/// rule in Particle Valid (Restriction); empty when nothing is blocked.
case element(name: PureXML.Model.QualifiedName, type: ElementType?, typeName: String?, valueConstraint: ValueConstraint? = nil, block: Set<DerivationMethod> = [])
case group(Group)
case wildcard(Wildcard)
}

View File

@ -40,7 +40,7 @@ extension PureXML.Schema.ComplexValidator {
counts[position] += 1
}
for (index, member) in group.particles.enumerated() where counts[index] < member.minOccurs {
if case let .element(name, _, _, _) = member.term {
if case let .element(name, _, _, _, _) = member.term {
errors.append(XSDFailure(reason: "element '\(name.localName)' is required but missing", at: path))
}
}
@ -48,7 +48,7 @@ extension PureXML.Schema.ComplexValidator {
static func memberMatches(_ term: PureXML.Schema.Term, _ name: PureXML.Model.QualifiedName) -> Bool {
switch term {
case let .element(declared, _, _, _): declared.localName == name.localName && declared.namespaceURI == name.namespaceURI
case let .element(declared, _, _, _, _): declared.localName == name.localName && declared.namespaceURI == name.namespaceURI
case let .wildcard(wildcard): wildcard.admits(name)
case .group: false
}
@ -77,7 +77,7 @@ extension PureXML.Schema.ComplexValidator {
static func matchedParticle(of term: PureXML.Schema.Term) -> PureXML.Schema.MatchedParticle? {
switch term {
case let .element(_, type, _, valueConstraint): .element(type: type, valueConstraint: valueConstraint)
case let .element(_, type, _, valueConstraint, _): .element(type: type, valueConstraint: valueConstraint)
case let .wildcard(wildcard): .wildcard(wildcard)
case .group: nil
}

View File

@ -34,7 +34,7 @@ extension PureXML.Schema.ComplexValidator {
fileprivate static func label(of term: XSDTerm) -> XSDTermLabel {
switch term {
case let .element(name, _, _, _): .name(name)
case let .element(name, _, _, _, _): .name(name)
case let .wildcard(wildcard): .wildcard(wildcard)
case .group: .wildcard(XSDWildcard())
}
@ -64,7 +64,7 @@ extension PureXML.Schema.ComplexValidator {
fileprivate static func collectTypes(_ term: XSDTerm, into result: inout [String: XSDElementType]) {
switch term {
case let .element(name, type, _, _):
case let .element(name, type, _, _, _):
if let type { result[key(name)] = type }
case let .group(group):
for member in group.particles {

View File

@ -247,7 +247,7 @@ extension PureXML.Schema {
private mutating func term(_ term: PureXML.Schema.Term) -> (start: Int, accept: Int) {
switch term {
case let .element(name, type, _, constraint):
case let .element(name, type, _, constraint, _):
let fragment = labeled(.name(name))
states[fragment.start].elementType = type
states[fragment.start].valueConstraint = constraint

View File

@ -66,11 +66,15 @@ extension PureXML.Schema {
return emptiable(base)
}
switch (restricted.term, base.term) {
case let (.element(restrictedName, _, restrictedTypeName, _), .element(baseName, _, baseTypeName, _)):
case let (.element(restrictedName, _, restrictedTypeName, _, restrictedBlock), .element(baseName, _, baseTypeName, _, baseBlock)):
// NameAndTypeOK: beyond name, occurrence, and type derivation, the
// restricting element's {disallowed substitutions} must be a superset
// of the base's (a restriction may block more, never fewer).
return rangeSubsumed(restricted, base)
&& sameName(restrictedName, baseName)
&& restrictedBlock.isSuperset(of: baseBlock)
&& elementTypeRestrictionOK(restrictedTypeName, baseTypeName, types, derivation)
case let (.element(name, _, _, _), .wildcard(wildcard)):
case let (.element(name, _, _, _, _), .wildcard(wildcard)):
return rangeSubsumed(restricted, base) && wildcard.admits(name)
case let (.wildcard(restrictedWildcard), .wildcard(baseWildcard)):
return rangeSubsumed(restricted, base) && narrows(restrictedWildcard, baseWildcard)
@ -85,7 +89,7 @@ extension PureXML.Schema {
// element restricting a once-or-more base group.
return rangeWithinWildcard(derivedMin: 1, derivedMax: 1, wildcardMin: base.minOccurs, wildcardMax: base.maxOccurs)
&& groupValid(Group(compositor: .sequence, particles: [restricted]), baseGroup, types, derivation)
case let (.group, .element(baseName, _, baseTypeName, _)):
case let (.group, .element(baseName, _, baseTypeName, _, _)):
// The mirror of RecurseAsIfGroup, by the same cardinality argument as
// group/wildcard: a base element accepts only its own name, its own
// occurrence count of times. The derived group is a valid restriction
@ -282,7 +286,7 @@ extension PureXML.Schema {
/// element by name, a nested wildcard by narrowing.
private static func leavesAdmitted(_ particle: Particle, by wildcard: Wildcard) -> Bool {
switch particle.term {
case let .element(name, _, _, _): wildcard.admits(name)
case let .element(name, _, _, _, _): wildcard.admits(name)
case let .wildcard(inner): narrows(inner, wildcard)
case let .group(group): group.particles.allSatisfy { leavesAdmitted($0, by: wildcard) }
}
@ -300,7 +304,7 @@ extension PureXML.Schema {
_ derivation: [String: TypeDerivation],
) -> Bool {
switch particle.term {
case let .element(name, _, typeName, _):
case let .element(name, _, typeName, _, _):
sameName(name, baseName) && elementTypeRestrictionOK(typeName, baseTypeName, types, derivation)
case .wildcard:
false

View File

@ -111,7 +111,7 @@ public extension PureXML.Schema {
private static func collect(_ term: Term, into result: inout [String: ElementType]) {
switch term {
case let .element(name, type, _, _):
case let .element(name, type, _, _, _):
if let type { result[name.description] = type }
case let .group(group):
for member in group.particles {

View File

@ -296,6 +296,9 @@ extension PureXML.Schema {
/// `schemaLoader`, so cross-document rules may run over the merged component
/// set instead of standing down.
var compositionLoaded: Bool = false
/// The schema's `blockDefault`, the `{disallowed substitutions}` a local or
/// global element inherits when it states no `block` of its own.
var blockDefault: String?
/// The location of each container schema document.
var containerLocations: [ObjectIdentifier: String?] = [:]
/// Target namespace imposed on an included schema with no `targetNamespace`

View File

@ -85,6 +85,7 @@ extension PureXML.Schema.XSDParser {
compositionLoaded: compositionLoaded,
containerLocations: containerLocations,
)
context.blockDefault = XSDNode.attribute(schema, "blockDefault")
context.elementFormQualified = XSDNode.attribute(schema, "elementFormDefault") == "qualified"
context.attributeFormQualified = XSDNode.attribute(schema, "attributeFormDefault") == "qualified"
context.namespaceBindings = XSDNode.namespaceBindings(of: schema)

View File

@ -73,6 +73,7 @@ extension PureXML.Schema.XSDParser {
type: elementType(node, context),
typeName: elementTypeName(node),
valueConstraint: valueConstraint(of: node),
block: methodSet(XSDNode.attribute(node, "block") ?? context.blockDefault),
),
)
}

View File

@ -87,8 +87,12 @@ struct XSTSSuiteTests {
/// that must occur 2+ times cannot be restricted to one element; §3.9.6) then
/// caught two more invalid schemas: invalid-schemas accepted 181 → 179, no other
/// bucket moved.
/// Enforcing the NameAndTypeOK block-superset clause (a restricting element's
/// `{disallowed substitutions}` must be a superset of the base element's, with
/// `block`/`blockDefault` carried on the particle) then caught sixteen more
/// invalid schemas: invalid-schemas accepted 179 → 163, no other bucket moved.
private let knownSchemaValidRejected = 1
private let knownSchemaInvalidAccepted = 179
private let knownSchemaInvalidAccepted = 163
private let knownInstanceValidRejected = 3
private let knownInstanceInvalidAccepted = 132