feat(schema): enforce attribute fixed value constraint under restriction
This commit is contained in:
parent
b102503d29
commit
f86a7c9ade
@ -9,7 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||
|
||||
### Fixed
|
||||
|
||||
- A complexContent restriction may no longer relax a required base attribute (XSD 1.0 `cos-ct-derived-ok` / `derivation-ok-restriction.2`, #170). When a restriction redeclares an attribute that the base type marks `use="required"`, the redeclaration may not make it optional or prohibited; such schemas were accepted. Only attributes the restriction explicitly redeclares that also exist in the base are checked (an omitted attribute is inherited unchanged); the base reference must resolve to the schema's own target namespace, and the check stands down for a schema that composes other documents. XSTS: invalid schemas accepted 394 to 390, no other bucket raised. Disclosed under-rejection: the matching fixed-value clause (a base attribute fixed to a value must keep that value) is not enforced, because it requires comparing fixed values in the attribute type's value space rather than lexically.
|
||||
- A complexContent restriction must keep a base attribute's required use and its fixed value constraint (XSD 1.0 `cos-ct-derived-ok` / `derivation-ok-restriction.2` / `derivation-ok-restriction.4`). A restriction redeclaring a base `required` attribute may not make it optional or prohibited, and if a base attribute use has a `fixed` value constraint, the restriction must also make it `fixed` with the same value. The fixed values are compared in the attribute type's value space (supporting lists and unions) to avoid over-rejecting valid schemas. A prohibited attribute is removed, so the fixed clause does not apply to it. XSTS: invalid schemas accepted 390 to 387, no other bucket raised.
|
||||
|
||||
## [0.1.0] - 2026-06-15
|
||||
|
||||
|
||||
@ -30,7 +30,7 @@ extension PureXML.Schema.XSDParser {
|
||||
case let .complex(complex)? = types[AttrRestrictNode.stripPrefix(base)]
|
||||
else { continue }
|
||||
for derived in attributeUses(under: restriction, context) {
|
||||
if let message = attributeRestrictionViolation(derived, complex.attributes) {
|
||||
if let message = attributeRestrictionViolation(derived, complex.attributes, restriction, context) {
|
||||
errors.append(message)
|
||||
}
|
||||
}
|
||||
@ -42,11 +42,64 @@ extension PureXML.Schema.XSDParser {
|
||||
/// its base counterpart, or nil when it is a valid restriction of it (or has no
|
||||
/// base counterpart). A base `required` attribute may not be made optional or
|
||||
/// prohibited (a prohibited use has `required == false`).
|
||||
private static func attributeRestrictionViolation(_ derived: PureXML.Schema.AttributeUse, _ baseAttributes: [PureXML.Schema.AttributeUse]) -> String? {
|
||||
/// Also checks that fixed attributes keep their fixed values.
|
||||
private static func attributeRestrictionViolation(
|
||||
_ derived: PureXML.Schema.AttributeUse,
|
||||
_ baseAttributes: [PureXML.Schema.AttributeUse],
|
||||
_ restrictionNode: XSDTree,
|
||||
_ context: PureXML.Schema.XSDContext,
|
||||
) -> String? {
|
||||
guard let base = baseAttributes.first(where: { $0.name == derived.name }) else { return nil }
|
||||
if base.required, !derived.required {
|
||||
return "attribute '\(derived.name.localName)' is required in the base type and a restriction may not make it optional or prohibited"
|
||||
}
|
||||
if let baseFixed = base.valueConstraint?.fixedValue {
|
||||
if isProhibited(derived.name, under: restrictionNode, context) {
|
||||
return nil
|
||||
}
|
||||
guard let derivedConstraint = derived.valueConstraint,
|
||||
let derivedFixed = derivedConstraint.fixedValue
|
||||
else {
|
||||
return "attribute '\(derived.name.localName)' is fixed in the base type and a restriction must also make it fixed"
|
||||
}
|
||||
if !derived.type.valueMatches(derivedFixed, literal: baseFixed) {
|
||||
return "attribute '\(derived.name.localName)' has fixed value '\(derivedFixed)' which does not match base fixed value '\(baseFixed)' in the type's value space"
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
private static func isProhibited(_ name: PureXML.Model.QualifiedName, under node: XSDTree, _ context: PureXML.Schema.XSDContext, visited: Set<String> = []) -> Bool {
|
||||
for child in AttrRestrictNode.elementChildren(node) {
|
||||
switch AttrRestrictNode.localName(child) {
|
||||
case "attribute":
|
||||
if let attrName = attributeName(child, context), attrName == name {
|
||||
if AttrRestrictNode.attribute(child, "use") == "prohibited" {
|
||||
return true
|
||||
}
|
||||
}
|
||||
case "attributeGroup":
|
||||
guard let ref = AttrRestrictNode.attribute(child, "ref") else { break }
|
||||
let refName = AttrRestrictNode.stripPrefix(ref)
|
||||
guard !visited.contains(refName), let group = context.attributeGroups[refName] else { break }
|
||||
if isProhibited(name, under: group, context, visited: visited.union([refName])) {
|
||||
return true
|
||||
}
|
||||
default:
|
||||
break
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
private static func attributeName(_ node: XSDTree, _ context: PureXML.Schema.XSDContext) -> PureXML.Model.QualifiedName? {
|
||||
if let ref = AttrRestrictNode.attribute(node, "ref") {
|
||||
let refName = AttrRestrictNode.stripPrefix(ref)
|
||||
return PureXML.Model.QualifiedName(localName: refName, namespaceURI: context.targetNamespace)
|
||||
}
|
||||
guard let name = AttrRestrictNode.attribute(node, "name") else { return nil }
|
||||
let qualified = AttrRestrictNode.attribute(node, "form") == "qualified"
|
||||
|| (AttrRestrictNode.attribute(node, "form") == nil && context.attributeFormQualified)
|
||||
return PureXML.Model.QualifiedName(localName: name, namespaceURI: qualified ? context.targetNamespace : nil)
|
||||
}
|
||||
}
|
||||
|
||||
@ -129,6 +129,27 @@ public extension PureXML.Schema {
|
||||
/// whitespace-normalized lexical form. (Durations and binary types fall
|
||||
/// under the lexical rule, so only their normalized forms compare equal.)
|
||||
public func valueMatches(_ instance: String, literal: String) -> Bool {
|
||||
switch variety {
|
||||
case .atomic:
|
||||
return atomicValueMatches(instance, literal)
|
||||
case let .list(item):
|
||||
let whitespaceProcessing = facets.whiteSpace ?? .collapse
|
||||
let leftNorm = Self.process(instance, whiteSpace: whitespaceProcessing)
|
||||
let rightNorm = Self.process(literal, whiteSpace: whitespaceProcessing)
|
||||
let leftTokens = leftNorm.isEmpty ? [] : leftNorm.split(separator: " ").map(String.init)
|
||||
let rightTokens = rightNorm.isEmpty ? [] : rightNorm.split(separator: " ").map(String.init)
|
||||
guard leftTokens.count == rightTokens.count else { return false }
|
||||
return zip(leftTokens, rightTokens).allSatisfy { item.valueMatches($0, literal: $1) }
|
||||
case let .union(members):
|
||||
guard let leftMember = members.first(where: { $0.isValid(instance) }),
|
||||
let rightMember = members.first(where: { $0.isValid(literal) }),
|
||||
leftMember.base == rightMember.base
|
||||
else { return false }
|
||||
return leftMember.valueMatches(instance, literal: literal)
|
||||
}
|
||||
}
|
||||
|
||||
private func atomicValueMatches(_ instance: String, _ literal: String) -> Bool {
|
||||
let whiteSpace = facets.whiteSpace ?? base.whiteSpace
|
||||
let left = Self.process(instance, whiteSpace: whiteSpace)
|
||||
let right = Self.process(literal, whiteSpace: whiteSpace)
|
||||
|
||||
@ -48,13 +48,40 @@ struct SchemaAttributeRestrictionTests {
|
||||
/// not lexical, comparison). A list-typed fixed value that differs only in
|
||||
/// whitespace is the same value and must compile; and, for now, even a genuinely
|
||||
/// changed fixed value is accepted rather than risk a lexical false positive.
|
||||
@Test("Fixed-value differences on a restricted attribute are not (yet) rejected")
|
||||
func test_fixedClauseDeferred() {
|
||||
@Test("Fixed-value differences on a restricted attribute: matching values compile, mismatched values are rejected")
|
||||
func test_fixedClauseRestrictions() {
|
||||
let listBase = "<xs:simpleType name=\"L\"><xs:list itemType=\"xs:int\"/></xs:simpleType>"
|
||||
+ "<xs:complexType name=\"Base\"><xs:attribute name=\"a\" type=\"L\" fixed=\"1 2 3\"/></xs:complexType>"
|
||||
let derived = "<xs:complexType name=\"D\"><xs:complexContent><xs:restriction base=\"Base\">"
|
||||
+ "<xs:attribute name=\"a\" type=\"L\" fixed=\"1 2 3\"/></xs:restriction></xs:complexContent></xs:complexType>"
|
||||
|
||||
// Same list value, different whitespace: valid, must compile.
|
||||
#expect(compiles(listBase + derived))
|
||||
let derivedValidWS = "<xs:complexType name=\"D\"><xs:complexContent><xs:restriction base=\"Base\">"
|
||||
+ "<xs:attribute name=\"a\" type=\"L\" fixed=\"1 2 3\"/></xs:restriction></xs:complexContent></xs:complexType>"
|
||||
#expect(compiles(listBase + derivedValidWS))
|
||||
|
||||
// Same integer values, different padding: valid, must compile.
|
||||
let intBase = "<xs:complexType name=\"Base\"><xs:attribute name=\"a\" type=\"xs:integer\" fixed=\"01\"/></xs:complexType>"
|
||||
let derivedValidInt = "<xs:complexType name=\"D\"><xs:complexContent><xs:restriction base=\"Base\">"
|
||||
+ "<xs:attribute name=\"a\" type=\"xs:integer\" fixed=\"1\"/></xs:restriction></xs:complexContent></xs:complexType>"
|
||||
#expect(compiles(intBase + derivedValidInt))
|
||||
|
||||
// Mismatched list values: invalid, must be rejected.
|
||||
let derivedInvalidList = "<xs:complexType name=\"D\"><xs:complexContent><xs:restriction base=\"Base\">"
|
||||
+ "<xs:attribute name=\"a\" type=\"L\" fixed=\"1 2 4\"/></xs:restriction></xs:complexContent></xs:complexType>"
|
||||
#expect(!compiles(listBase + derivedInvalidList))
|
||||
|
||||
// Mismatched integer values: invalid, must be rejected.
|
||||
let derivedInvalidInt = "<xs:complexType name=\"D\"><xs:complexContent><xs:restriction base=\"Base\">"
|
||||
+ "<xs:attribute name=\"a\" type=\"xs:integer\" fixed=\"2\"/></xs:restriction></xs:complexContent></xs:complexType>"
|
||||
#expect(!compiles(intBase + derivedInvalidInt))
|
||||
|
||||
// Missing value constraint when base is fixed: invalid, must be rejected.
|
||||
let derivedMissingConstraint = "<xs:complexType name=\"D\"><xs:complexContent><xs:restriction base=\"Base\">"
|
||||
+ "<xs:attribute name=\"a\" type=\"xs:integer\"/></xs:restriction></xs:complexContent></xs:complexType>"
|
||||
#expect(!compiles(intBase + derivedMissingConstraint))
|
||||
|
||||
// Default value constraint when base is fixed: invalid, must be rejected.
|
||||
let derivedDefaultConstraint = "<xs:complexType name=\"D\"><xs:complexContent><xs:restriction base=\"Base\">"
|
||||
+ "<xs:attribute name=\"a\" type=\"xs:integer\" default=\"1\"/></xs:restriction></xs:complexContent></xs:complexType>"
|
||||
#expect(!compiles(intBase + derivedDefaultConstraint))
|
||||
}
|
||||
}
|
||||
|
||||
@ -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 = 1
|
||||
private let knownSchemaInvalidAccepted = 390
|
||||
private let knownSchemaInvalidAccepted = 387
|
||||
private let knownInstanceValidRejected = 180
|
||||
private let knownInstanceInvalidAccepted = 158
|
||||
|
||||
|
||||
@ -10,7 +10,7 @@ irreducible tail. Do not deviate to new features while this is open.
|
||||
| Bucket | Count | Reading |
|
||||
|---|---|---|
|
||||
| valid-schemas-rejected | **72 -> 4** | production target is 0: rejecting a correct schema is the most user-hostile defect (#148). Particle-restriction over-rejection fixed: pointless `maxOccurs=0` particles removed, content-free restriction of an EMPTY base, empty-derived emptiable-base, and (#163) the pairwise check over the normalized content model with occurrence subsumption applied only to same-kind pairings (cross-kind element/group, group/element, group/wildcard match occurrence inside, by effective total range / member). #164 resolved element-ref namespaces by prefix binding (the `particlesJj/Jk/Jm/Jn/Js`, `particlesQ/R` cluster, -28). Content-free (`maxOccurs=0`) derived groups and substitution-group declaration order closed `particlesW006`/`elemZ027_a`; the W3C MapAndSum rule (Sequence:Choice, #165) closed `particlesV003`. Remaining 1 (`particlesZ001`): a documented spec ambiguity (RecurseAsIfGroup; the reference rejects it), not a defect. |
|
||||
| **invalid-schemas-accepted** | **2461 -> 390** | we rarely catch a bad one (facet, default/fixed value, particle-restriction name+type, id, structural content-model, UPA determinism, content order, identity-constraint XPath-subset, wildcard-namespace, final/block, simpleType content, group/wildcard and group/element cardinality, element-ref namespace, sequence/choice MapAndSum, circular type derivation, circular group/attributeGroup/substitution references, attribute-use uniqueness and single-ID, type-excludes-inline-type, ID-typed value-constraint exclusion, all-group reference placement, substitution-member type derivation, default/fixed mutual exclusion landed). |
|
||||
| **invalid-schemas-accepted** | **2461 -> 387** | we rarely catch a bad one (facet, default/fixed value, particle-restriction name+type, id, structural content-model, UPA determinism, content order, identity-constraint XPath-subset, wildcard-namespace, final/block, simpleType content, group/wildcard and group/element cardinality, element-ref namespace, sequence/choice MapAndSum, circular type derivation, circular group/attributeGroup/substitution references, attribute-use uniqueness and single-ID, type-excludes-inline-type, ID-typed value-constraint exclusion, all-group reference placement, substitution-member type derivation, default/fixed mutual exclusion landed). |
|
||||
| valid-instances-rejected | **233 -> 180** | (instance side; #164's element-ref namespace fix is shared with instance validation, -34) |
|
||||
| invalid-instances-accepted | **160 -> 158** | (instance side; resolving an inline `<simpleType>` restriction base preserves list/union variety, so a `length` facet counts items and two more invalid instances are caught) |
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user