diff --git a/CHANGELOG.md b/CHANGELOG.md index e172256..7600e4f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 nillable clause. A restricting element may not be `nillable` unless the base element it restricts is also nillable. Caught two invalid schemas: invalid-schemas-accepted 144 → 142, no other bucket moved. + - XSTS invalid-schemas-accepted (#145): Particle Valid (Restriction) now enforces the NameAndTypeOK fixed-value clause. When the base element has a `fixed` value constraint, the restricting element must also be `fixed` to the same value (compared in the base type's value space, so a value-equal pair in different lexical forms is not wrongly flagged). Caught four invalid schemas: invalid-schemas-accepted 148 → 144, no other bucket moved. - XSTS invalid-schemas-accepted (#145): Particle Valid (Restriction) now also covers anonymous complex types. The named-type subset check was keyed by type name, so an inline `complexType` deriving by `complexContent` restriction (the common `` shape) was never validated against its base. Such restrictions are now compiled and checked with the same algorithm. Caught fifteen invalid schemas: invalid-schemas-accepted 163 → 148, no other bucket moved. diff --git a/Sources/Schema/ComplexType.swift b/Sources/Schema/ComplexType.swift index 322c274..9ec8605 100644 --- a/Sources/Schema/ComplexType.swift +++ b/Sources/Schema/ComplexType.swift @@ -107,7 +107,14 @@ public extension PureXML.Schema { /// `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 = []) + case element( + name: PureXML.Model.QualifiedName, + type: ElementType?, + typeName: String?, + valueConstraint: ValueConstraint? = nil, + block: Set = [], + nillable: Bool = false, + ) case group(Group) case wildcard(Wildcard) } diff --git a/Sources/Schema/ComplexValidatorContentModel.swift b/Sources/Schema/ComplexValidatorContentModel.swift index 31188b6..61e6a3b 100644 --- a/Sources/Schema/ComplexValidatorContentModel.swift +++ b/Sources/Schema/ComplexValidatorContentModel.swift @@ -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 } diff --git a/Sources/Schema/ComplexValidatorHelpers.swift b/Sources/Schema/ComplexValidatorHelpers.swift index d157262..880fd9e 100644 --- a/Sources/Schema/ComplexValidatorHelpers.swift +++ b/Sources/Schema/ComplexValidatorHelpers.swift @@ -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 { diff --git a/Sources/Schema/ContentMatcher.swift b/Sources/Schema/ContentMatcher.swift index 0a1f180..09a6454 100644 --- a/Sources/Schema/ContentMatcher.swift +++ b/Sources/Schema/ContentMatcher.swift @@ -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 diff --git a/Sources/Schema/ParticleRestriction.swift b/Sources/Schema/ParticleRestriction.swift index 768853e..9228471 100644 --- a/Sources/Schema/ParticleRestriction.swift +++ b/Sources/Schema/ParticleRestriction.swift @@ -66,17 +66,19 @@ extension PureXML.Schema { return emptiable(base) } switch (restricted.term, base.term) { - case let (.element(restrictedName, _, restrictedTypeName, restrictedValue, restrictedBlock), .element(baseName, baseType, baseTypeName, baseValue, baseBlock)): + case let (.element(rName, _, rTypeName, rValue, rBlock, rNil), .element(bName, bType, bTypeName, bValue, bBlock, bNil)): // 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), and if - // the base is fixed the restriction must be fixed to the same value. + // restriction's {disallowed substitutions} must be a superset of the + // base's (it may block more, never fewer); if the base is fixed the + // restriction must be fixed to the same value; and it may not be + // nillable unless the base element is. return rangeSubsumed(restricted, base) - && sameName(restrictedName, baseName) - && restrictedBlock.isSuperset(of: baseBlock) - && fixedValueOK(restrictedValue, baseValue, baseType, types) - && elementTypeRestrictionOK(restrictedTypeName, baseTypeName, types, derivation) - case let (.element(name, _, _, _, _), .wildcard(wildcard)): + && sameName(rName, bName) + && rBlock.isSuperset(of: bBlock) + && (!rNil || bNil) + && fixedValueOK(rValue, bValue, bType, types) + && elementTypeRestrictionOK(rTypeName, bTypeName, types, derivation) + 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) @@ -91,7 +93,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 @@ -310,7 +312,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) } } @@ -328,7 +330,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 diff --git a/Sources/Schema/XSDCompletions.swift b/Sources/Schema/XSDCompletions.swift index 25d8a17..041ca27 100644 --- a/Sources/Schema/XSDCompletions.swift +++ b/Sources/Schema/XSDCompletions.swift @@ -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 { diff --git a/Sources/Schema/XSDParserParticles.swift b/Sources/Schema/XSDParserParticles.swift index cd0c33e..4e728f8 100644 --- a/Sources/Schema/XSDParserParticles.swift +++ b/Sources/Schema/XSDParserParticles.swift @@ -74,6 +74,7 @@ extension PureXML.Schema.XSDParser { typeName: elementTypeName(node), valueConstraint: valueConstraint(of: node), block: methodSet(XSDNode.attribute(node, "block") ?? context.blockDefault), + nillable: ["true", "1"].contains(XSDNode.attribute(node, "nillable")), ), ) } diff --git a/Tests/XSTSSuiteTests.swift b/Tests/XSTSSuiteTests.swift index 876aff1..e132669 100644 --- a/Tests/XSTSSuiteTests.swift +++ b/Tests/XSTSSuiteTests.swift @@ -99,8 +99,11 @@ struct XSTSSuiteTests { /// base element must be fixed to the same value, compared in the base type's /// value space) then caught four more invalid schemas: invalid-schemas accepted /// 148 → 144, no other bucket moved. + /// Enforcing the NameAndTypeOK nillable clause (a restriction may not be nillable + /// unless the base element is) then caught two more invalid schemas: + /// invalid-schemas accepted 144 → 142, no other bucket moved. private let knownSchemaValidRejected = 1 - private let knownSchemaInvalidAccepted = 144 + private let knownSchemaInvalidAccepted = 142 private let knownInstanceValidRejected = 3 private let knownInstanceInvalidAccepted = 132