diff --git a/CHANGELOG.md b/CHANGELOG.md index 5866074..e172256 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 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. - 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. diff --git a/Sources/Schema/ParticleRestriction.swift b/Sources/Schema/ParticleRestriction.swift index 625ba86..768853e 100644 --- a/Sources/Schema/ParticleRestriction.swift +++ b/Sources/Schema/ParticleRestriction.swift @@ -66,13 +66,15 @@ extension PureXML.Schema { return emptiable(base) } switch (restricted.term, base.term) { - case let (.element(restrictedName, _, restrictedTypeName, _, restrictedBlock), .element(baseName, _, baseTypeName, _, baseBlock)): + case let (.element(restrictedName, _, restrictedTypeName, restrictedValue, restrictedBlock), .element(baseName, baseType, baseTypeName, baseValue, 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). + // 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. 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)): return rangeSubsumed(restricted, base) && wildcard.admits(name) @@ -282,6 +284,28 @@ extension PureXML.Schema { lhs.localName == rhs.localName && (lhs.namespaceURI ?? "") == (rhs.namespaceURI ?? "") } + /// NameAndTypeOK value-constraint clause: when the base element is `fixed`, + /// the restricting element must also be `fixed` to the same value (compared in + /// the base type's value space, so `1` and `01` match). A base with no fixed + /// value, or a `default`, imposes no constraint on the restriction. + private static func fixedValueOK(_ restrictedValue: ValueConstraint?, _ baseValue: ValueConstraint?, _ baseType: ElementType?, _ types: [String: ElementType]) -> Bool { + guard let baseFixed = baseValue?.fixedValue else { return true } + guard let restrictedFixed = restrictedValue?.fixedValue else { return false } + return atomicType(baseType, types)?.valueMatches(restrictedFixed, literal: baseFixed) ?? (restrictedFixed == baseFixed) + } + + /// The atomic simple type an element's type resolves to (following a + /// `typeReference` through the types map, bounded against cycles), or nil for + /// a complex, list, or union type. + private static func atomicType(_ type: ElementType?, _ types: [String: ElementType], _ depth: Int = 0) -> PureXML.Schema.SimpleType? { + guard depth < 32 else { return nil } + switch type { + case let .simple(simple): return simple + case let .typeReference(key): return atomicType(types[key], types, depth + 1) + default: return nil + } + } + /// Whether every leaf a particle can contain is admitted by `wildcard`: an /// element by name, a nested wildcard by narrowing. private static func leavesAdmitted(_ particle: Particle, by wildcard: Wildcard) -> Bool { diff --git a/Tests/XSTSSuiteTests.swift b/Tests/XSTSSuiteTests.swift index 356953c..876aff1 100644 --- a/Tests/XSTSSuiteTests.swift +++ b/Tests/XSTSSuiteTests.swift @@ -95,8 +95,12 @@ struct XSTSSuiteTests { /// declarations, which the named-type rule never saw) with the same /// Particle-Valid-Restriction algorithm then caught fifteen more invalid schemas: /// invalid-schemas accepted 163 → 148, no other bucket moved. + /// Enforcing the NameAndTypeOK fixed-value clause (a restriction of a `fixed` + /// 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. private let knownSchemaValidRejected = 1 - private let knownSchemaInvalidAccepted = 148 + private let knownSchemaInvalidAccepted = 144 private let knownInstanceValidRejected = 3 private let knownInstanceInvalidAccepted = 132