From ca8fd278774e5d1db1d9486eb48a999247b18778 Mon Sep 17 00:00:00 2001 From: Mihaela Mihaljevic Date: Sat, 13 Jun 2026 23:16:47 +0200 Subject: [PATCH] feat(schema): check name and reference attribute lexical validity Extends the compile-time attribute-model check. A component's name must be a valid NCName, the reference attributes type/base/ref/itemType/refer/ substitutionGroup must be lexical QNames, and memberTypes a list of QNames. These were unchecked, so name="", name="123", name="a:b", or type=":_" left the schema wrongly accepted. Only the lexical form is checked (a QName's prefix is resolved elsewhere), so a valid reference is never rejected; each of these attribute names has one type throughout XSD, so checking by local name is safe. XSTS: invalid schemas accepted 1190 to 1129 (-61); valid schemas rejected held at 72 (no over-rejection); instance buckets unchanged. --- CHANGELOG.md | 1 + Sources/Schema/SchemaStructure.swift | 44 ++++++++++++++++++++-------- Tests/SchemaStructureTests.swift | 17 +++++++++++ Tests/XSTSSuiteTests.swift | 2 +- docs/schema-validity-burndown.md | 4 +-- docs/xsts-deviations.md | 4 +-- 6 files changed, 55 insertions(+), 17 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 57c0d49..9a8fc4e 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 +- Schema-vocabulary name and reference attribute values are now checked for lexical validity at compile time (#147). A component's `name` must be a valid NCName (`name=""`, `name="123"`, `name="a:b"` are rejected), and the reference attributes `type`/`base`/`ref`/`itemType`/`refer`/`substitutionGroup` must be lexical QNames (`type=":_"`, `type="a:b:c"` rejected), with `memberTypes` a list of QNames; these were unchecked and the schema accepted. Only the lexical form is checked (a QName's prefix is resolved elsewhere), so no valid schema is rejected. XSTS: invalid schemas accepted 1190 to 1129, no other bucket raised. - Schema-vocabulary attribute values are now validated at compile time (#147), the attribute-model sibling of the structural content-model check. Enumerated attributes (`form`/`elementFormDefault`/`attributeFormDefault` to `qualified`/`unqualified`, `use` to `optional`/`prohibited`/`required`, `processContents` to `skip`/`lax`/`strict`, `mixed`/`abstract`/`nillable` to a boolean) and `minOccurs`/`maxOccurs` (a `nonNegativeInteger`, `maxOccurs` also `unbounded`) are checked against their fixed value space; an out-of-space value (`use="foo"`, `mixed="yes"`, `minOccurs="-1"`) was previously ignored and the schema accepted. Only unprefixed attributes on XSD-namespace elements are checked, so a foreign attribute is never flagged. XSTS: invalid schemas accepted 1313 to 1190, no other bucket raised. - Schema documents are now structurally validated against the schema-for-schemas content model (#147), the general mechanism for the structural tail of the schema-validity gap. A data-driven table of the XSD 1.0 child content model rejects, at compile time, any component child the model does not admit, more than one `annotation` (or an `annotation` that is not first, except on `schema`/`redefine`), and an identity constraint missing its selector or field. The check walks only XSD-namespace elements and skips `xs:appinfo`/`xs:documentation` foreign content, so no valid schema is rejected; `restriction`/`extension` use the permissive union of their contexts (never over-rejecting). One table covers every component rather than a rule hand-coded per element. XSTS: invalid schemas accepted 1688 to 1313 (-375), no other bucket raised. - The `id` attribute on XSD components is now validated at compile time (#147). It is of type `xs:ID`, so each value must be a valid NCName and unique within the schema document; the values were never checked, so a malformed (`id=""`, `id="123"`) or duplicated `id` left the schema wrongly accepted. The schema-document tree is walked, skipping `xs:appinfo`/`xs:documentation` foreign content (whose `id` attributes are not `xs:ID`) and treating only the unprefixed `id` (not a foreign `xml:id`/`pre:id`) as the component identifier, so no valid schema is rejected. XSTS: invalid schemas accepted 1789 to 1688, no other bucket raised. diff --git a/Sources/Schema/SchemaStructure.swift b/Sources/Schema/SchemaStructure.swift index 8aa5628..db1593d 100644 --- a/Sources/Schema/SchemaStructure.swift +++ b/Sources/Schema/SchemaStructure.swift @@ -95,19 +95,39 @@ extension PureXML.Schema.XSDParser { /// (`maxOccurs` also admits `unbounded`). A prefixed (foreign) attribute is the /// schema author's own and is not checked. private static func attributeValueErrors(_ node: XSDTree) -> [String] { - var errors: [String] = [] - for attribute in node.attributes where attribute.name.prefix == nil { - let name = attribute.name.localName - let value = attribute.value.trimmingXMLWhitespace() - if let allowed = attributeEnumerations[name], !allowed.contains(value) { - errors.append("attribute '\(name)' has invalid value '\(attribute.value)'") - } else if name == "minOccurs", !isNonNegativeInteger(value) { - errors.append("attribute 'minOccurs' must be a nonNegativeInteger, not '\(attribute.value)'") - } else if name == "maxOccurs", value != "unbounded", !isNonNegativeInteger(value) { - errors.append("attribute 'maxOccurs' must be a nonNegativeInteger or 'unbounded', not '\(attribute.value)'") - } + node.attributes.filter { $0.name.prefix == nil }.compactMap { attribute in + attributeValueError(attribute.name.localName, attribute.value) + } + } + + /// The schema-vocabulary attributes whose value is a single QName reference; + /// each has one meaning throughout XSD, so they are recognised by local name. + private static let qnameAttributes: Set = ["type", "base", "ref", "itemType", "refer", "substitutionGroup"] + + /// The finding, if any, for one unprefixed schema-vocabulary attribute whose + /// value falls outside its fixed value space: an enumerated token, an + /// occurrence count, a `name` (NCName), or a QName reference. Only the lexical + /// form is checked here; a QName's prefix is resolved elsewhere. + private static func attributeValueError(_ name: String, _ raw: String) -> String? { + let value = raw.trimmingXMLWhitespace() + if let allowed = attributeEnumerations[name] { + return allowed.contains(value) ? nil : "attribute '\(name)' has invalid value '\(raw)'" + } + switch name { + case "minOccurs": + return isNonNegativeInteger(value) ? nil : "attribute 'minOccurs' must be a nonNegativeInteger, not '\(raw)'" + case "maxOccurs": + return value == "unbounded" || isNonNegativeInteger(value) ? nil : "attribute 'maxOccurs' must be a nonNegativeInteger or 'unbounded', not '\(raw)'" + case "name": + return PureXML.Schema.Lexical.isNCName(value) ? nil : "attribute 'name' value '\(raw)' is not a valid NCName" + case "memberTypes": + let tokens = value.split(whereSeparator: \.isWhitespace).map(String.init) + return tokens.allSatisfy(PureXML.Schema.Lexical.isQName) ? nil : "attribute 'memberTypes' value '\(raw)' is not a list of QNames" + case _ where qnameAttributes.contains(name): + return PureXML.Schema.Lexical.isQName(value) ? nil : "attribute '\(name)' value '\(raw)' is not a valid QName" + default: + return nil } - return errors } /// Whether `value` is a lexical `nonNegativeInteger` (optional `+`, ASCII diff --git a/Tests/SchemaStructureTests.swift b/Tests/SchemaStructureTests.swift index e561b55..93fe359 100644 --- a/Tests/SchemaStructureTests.swift +++ b/Tests/SchemaStructureTests.swift @@ -98,6 +98,23 @@ struct SchemaStructureTests { try compile(#""#) } + @Test("a component name must be a valid NCName") + func test_nameNCName() { + #expect(rejects(#""#)) + #expect(rejects(#""#)) + #expect(rejects(#""#)) + #expect(rejects(#""#)) + try? compile(#""#) + } + + @Test("a QName-valued reference attribute must be a lexical QName") + func test_qnameReferences() throws { + #expect(rejects(#""#)) + #expect(rejects(#""#)) + try compile(#""#) + try compile(#""#) + } + @Test("an identity constraint requires a selector and field") func test_identityConstraintRequiresParts() { #expect(rejects(#""" diff --git a/Tests/XSTSSuiteTests.swift b/Tests/XSTSSuiteTests.swift index 1d9d398..317a9c9 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 = 1190 + private let knownSchemaInvalidAccepted = 1129 private let knownInstanceValidRejected = 214 private let knownInstanceInvalidAccepted = 165 diff --git a/docs/schema-validity-burndown.md b/docs/schema-validity-burndown.md index d4c0019..1e788a8 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 -> 1190** | we rarely catch a bad one (facet, id, and structural content-model validity landed) | +| **invalid-schemas-accepted** | **2461 -> 1129** | we rarely catch a bad one (facet, id, and structural content-model validity landed) | | valid-instances-rejected | 233 | (instance side, separately tracked) | | invalid-instances-accepted | 165 | | @@ -73,7 +73,7 @@ Each iteration targets one self-contained rule family with a clean root cause. (1688 -> 1313, -375)._ A data-driven `allowedChildren` table (the schema-for- schemas child content model) rejects disallowed children, multi/misplaced `annotation`, and identity constraints missing selector/field, via one general - mechanism (`SchemaStructure.swift`). Attribute **value** validity also landed (enumerated attributes, `minOccurs`/`maxOccurs`; 1313 -> 1190). **Still open:** attribute **applicability** (which attributes each component admits, e.g. `form` is prohibited on a global declaration, `ref` excludes `name`); child *order* beyond the leading annotation; and particle / model-group semantic rules (Unique Particle Attribution, Particle-Valid-Restriction completeness). + mechanism (`SchemaStructure.swift`). Attribute **value** validity also landed (enumerated attributes, `minOccurs`/`maxOccurs`; 1313 -> 1190), plus **name/reference lexical validity** (`name` an NCName; `type`/`base`/`ref`/`itemType`/`refer`/`substitutionGroup` QNames; 1190 -> 1129). **Still open:** attribute **applicability** (which attributes each component admits, e.g. `form` is prohibited on a global declaration, `ref` excludes `name`); child *order* beyond the leading annotation; and particle / model-group semantic rules (Unique Particle Attribution, Particle-Valid-Restriction completeness). ## Per-iteration protocol (the PR-critic-loop) diff --git a/docs/xsts-deviations.md b/docs/xsts-deviations.md index bd96d05..3965400 100644 --- a/docs/xsts-deviations.md +++ b/docs/xsts-deviations.md @@ -47,7 +47,7 @@ and to document the remainder as spec-justified, deliberate exclusions. ## Category A: genuine correctness bugs -### Fixed (nineteen root causes, ~1945 deviations cleared) +### Fixed (twenty root causes, ~2006 deviations cleared) list-datatype length facets (#146) · union pattern/enumeration facets · duration partial-order facets · QName length non-constraining · the full XSD `\p{Is...}` @@ -65,7 +65,7 @@ validity (`min`/`maxInclusive`, `min`/`maxExclusive`, `enumeration` values valid in the base space; inclusive/exclusive exclusions; bound ordering) · `gMonth` `--MM--` lexical form · schema `id` attribute validity (`xs:ID`: NCName and unique within the document). The schema-validity work brought invalid schemas -accepted 2461 to 1190 (also: schema-document structural validity, content model and attribute values, against the schema-for-schemas); see `schema-validity-burndown.md`. +accepted 2461 to 1129 (also: schema-document structural validity, content model + attribute values + name/reference lexical validity, against the schema-for-schemas); see `schema-validity-burndown.md`. Plus the measurement fix (exclude W3C-disputed `status="queried"` entries). ### Remaining, root-caused (the hard tail)