fix(schema): validate value constraints against inline types (#145)

An element or attribute default/fixed value is now validated against an inline
type (an inline simpleType, or an inline complex type with simpleContent) the
same way it already was against a named type reference. Validated against the
simpleContent base's value space, so only a clear value-space mismatch (a
non-numeric default on a decimal-based type) is flagged.

XSTS invalid-schemas-accepted 185 to 183, no other bucket moved.
This commit is contained in:
Mihaela Mihaljevic 2026-06-17 15:45:06 +02:00
parent 4460508924
commit 379a9ccd8c
4 changed files with 47 additions and 2 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): an element or attribute `default`/`fixed` value constraint is now validated against an inline type (an inline `simpleType`, or an inline complex type with `simpleContent`) just as it already was against a named `type` reference, so a non-numeric `default` on a decimal-based inline type is rejected. Validated against the simpleContent base's value space, so only a clear value-space mismatch is flagged. Caught two invalid schemas: invalid-schemas-accepted 185 → 183, no other bucket moved.
- XSTS invalid-schemas-accepted (#145): a complex type without its own `final` attribute now inherits the schema's `finalDefault` as its `{final}` property, so `finalDefault="#all"` or `"restriction"`/`"extension"` blocks a `complexContent` restriction or extension derived from it just as an explicit `final` would (an explicit `final=""` overrides the default back to no restriction). Complex types previously honoured only an explicit `final`. Caught four invalid schemas: invalid-schemas-accepted 189 → 185, no other bucket moved.
- XSTS invalid-schemas-accepted (#145): the global-only element properties `abstract`, `final`, and `substitutionGroup` are now rejected on a local element declaration (an element particle, not a direct child of `schema`). They belong to the `topLevelElement` form only, so their presence on a local element is invalid at any value (`abstract="false"` is rejected as well as `"true"`). Caught five invalid schemas: invalid-schemas-accepted 194 → 189, no other bucket moved.

View File

@ -191,7 +191,8 @@ extension PureXML.Validation {
compileRule("Element value constraints are valid against their declared types") { document in
guard let namedTypes = document.namedTypes else { return [] }
return PureXML.Schema.SchemaLocatedFinding.unlocated(
PureXML.Schema.XSDParser.userTypeValueConstraintErrors(document.schema, document.context, namedTypes),
PureXML.Schema.XSDParser.userTypeValueConstraintErrors(document.schema, document.context, namedTypes)
+ PureXML.Schema.XSDParser.inlineTypeValueConstraintErrors(document.schema, document.context),
)
}
}

View File

@ -98,6 +98,44 @@ extension PureXML.Schema.XSDParser {
return errors
}
/// Like ``userTypeValueConstraintErrors`` but for an element or attribute whose
/// type is INLINE (no `type` attribute): an inline `simpleType`, or an inline
/// complex type with `simpleContent`. The `default`/`fixed` value must be valid
/// against that type's value space. Validated against the simpleContent base's
/// value space (not the local restriction facets), so only a clear value-space
/// mismatch (a non-numeric `default` on a decimal-based type) is flagged.
static func inlineTypeValueConstraintErrors(_ schema: XSDTree, _ context: PureXML.Schema.XSDContext) -> [String] {
var errors: [String] = []
forEachValueConstrained(schema) { node in
guard node.name?.namespaceURI == xsdNamespace,
unprefixedValue(node, "type") == nil
else { return }
let fixed = unprefixedValue(node, "fixed")
guard let value = fixed ?? unprefixedValue(node, "default"),
let simple = inlineSimpleType(of: node, context), !simple.isValid(value)
else { return }
errors.append("the \(fixed != nil ? "fixed" : "default") value '\(value)' is not valid for the declared inline type")
}
return errors
}
/// The simple value space of an element/attribute's INLINE type: an inline
/// `simpleType` child, or an inline complex type whose `simpleContent` derives
/// from a base simple type. Anything else (element-only/mixed/empty content)
/// carries no simple value space here and is left unchecked.
private static func inlineSimpleType(of node: XSDTree, _ context: PureXML.Schema.XSDContext) -> PureXML.Schema.SimpleType? {
if let inline = PureXML.Schema.XSDNode.firstChild(node, named: "simpleType") {
return PureXML.Schema.XSDSimpleParser.simpleType(inline, context)
}
guard let complexType = PureXML.Schema.XSDNode.firstChild(node, named: "complexType"),
let simpleContent = PureXML.Schema.XSDNode.firstChild(complexType, named: "simpleContent"),
let derivation = PureXML.Schema.XSDNode.firstChild(simpleContent, named: "extension")
?? PureXML.Schema.XSDNode.firstChild(simpleContent, named: "restriction"),
let base = PureXML.Schema.XSDNode.attribute(derivation, "base")
else { return nil }
return PureXML.Schema.XSDSimpleParser.simpleTypeReference(base, context)
}
/// The simple type a value constraint is validated against: a simple type
/// directly, or the simple content of a complex type. A complex type with
/// element-only, mixed, or empty content carries no simple value space here, so

View File

@ -144,8 +144,12 @@ struct XSTSSuiteTests {
/// blocks a restriction or extension derivation from it; explicit `final=""`
/// overrides) then caught four more invalid schemas: invalid-schemas accepted
/// 189 → 185, no other bucket moved.
/// Validating an element/attribute `default`/`fixed` against an INLINE type (an
/// inline simpleType, or an inline complex type with simpleContent) the same way
/// a named `type` reference already is, then caught two more invalid schemas:
/// invalid-schemas accepted 185 → 183, no other bucket moved.
private let knownSchemaValidRejected = 1
private let knownSchemaInvalidAccepted = 185
private let knownSchemaInvalidAccepted = 183
private let knownInstanceValidRejected = 3
private let knownInstanceInvalidAccepted = 132