fix(schema): honor empty-element default/fixed values and replace enumeration on restriction

An element with a default or fixed value takes that value when it appears empty,
so the empty content is valid even where the type would reject an empty string.
The constraint value, not the empty string, is validated against the type,
including any xsi:type override, so a fixed value invalid against the overriding
type is still rejected.

Validating the constraint value under xsi:type exposed a second defect: a
restriction's enumeration was unioned with the inherited enumeration, so a
derived type wrongly accepted the base's values. Enumeration now replaces on
restriction (the new set is the value space); pattern still accumulates and is
ANDed across derivation steps.

XSTS: valid instances rejected 395 to 369, invalid instances accepted 240 to
238. Baseline updated; regression tests cover empty default/fixed, present
content still validated, and the constraint value checked against an xsi:type.

Refs #147, #129.
This commit is contained in:
Mihaela Mihaljevic 2026-06-13 17:50:07 +02:00
parent 43456029ac
commit c9f41c387e
6 changed files with 80 additions and 3 deletions

View File

@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed
- Element `default` and `fixed` values are honored on empty elements (#147). An empty element with a value constraint takes that value, so the empty content is valid even when the type would reject the empty string; the constraint value (not the empty string) is what is validated against the type, including any `xsi:type` override. A restriction's `enumeration` now also replaces the inherited enumeration rather than unioning with it (the new set is the value space; `pattern` still accumulates and is ANDed), which a value-constraint case under `xsi:type` exposed. XSTS: valid instances rejected 395 to 369, invalid instances accepted 240 to 238.
- Element-level `block` is now enforced against `xsi:type` substitution (#147). The `block` on an element declaration (distinct from `block` on its type) was parsed but never threaded to the instance validator, so a document substituting a blocked-derivation type via `xsi:type` was wrongly accepted. The element block is now carried through to `blockedSubstitutionError`, which also resolves the element-ref type indirection (`element:foo` to its concrete type) so the check fires for referenced children, not only the root. XSTS: invalid instances accepted 261 to 240.
- The regex engine now recognizes the full XSD `\p{Is<Block>}` Unicode block set (#147). Only about fifteen blocks were tabulated, so a pattern over any other block (`\p{IsArmenian}`, `\p{IsTamil}`, `\p{IsHangulJamo}`, and so on) failed to compile and rejected valid instances. The table now carries the Unicode 3.1 blocks the XSD regular-expression appendix fixes (surrogate blocks omitted, since surrogates are not scalar values); each range is verified bidirectionally by the XSTS corpus (valid in-block instances pass, invalid out-of-block instances still fail). XSTS: valid instances rejected 502 to 395.
- Length facets on `xs:QName` no longer reject valid values (#147). XSD 1.0 Datatypes 4.3.1 does not define a unit of length for QName (only string, anyURI, binary, and list), so `length`/`minLength`/`maxLength` on QName are implementation-defined; like Xerces and the XSTS NIST set we treat them as non-constraining rather than counting characters. Net XSTS: valid instances rejected 547 to 502 (36+ NIST false rejections fixed); 4 Microsoft cases that read QName length as a character count remain in invalid instances accepted (257 to 261) and are documented as a deliberate spec-ambiguity interpretation in docs/xsts-deviations.md.

View File

@ -115,6 +115,14 @@ public extension PureXML.Schema {
if case let .fixed(value) = self { return value }
return nil
}
/// The constraint's value, whether `default` or `fixed`: the value an
/// empty element or absent attribute takes.
public var value: String {
switch self {
case let .default(value), let .fixed(value): value
}
}
}
/// An attribute use on a complex type.

View File

@ -87,6 +87,8 @@ public extension PureXML.Schema {
func elementFixedErrors(_ element: PureXML.Model.Element, at path: XSDPath) -> [XSDFailure] {
guard let fixed = elementConstraints[element.name.localName]?.fixedValue else { return [] }
let text = Self.textContent(element)
// An empty element takes the fixed value; only present content must equal it.
if text.isEmpty { return [] }
return text == fixed ? [] : [XSDFailure(reason: "element '\(element.name.localName)' is fixed and must be '\(fixed)'", at: path)]
}
@ -261,7 +263,12 @@ public extension PureXML.Schema {
if !child.children.compactMap(\.element).isEmpty {
errors.append(XSDFailure(reason: "'\(child.name.localName)' must not have children", at: path))
}
if let error = simple.validate(Self.textContent(child)) {
// An empty element takes its `default`/`fixed` value; that value
// (not the empty string) is what must be valid against the type,
// including any xsi:type override already resolved into `simple`.
let text = Self.textContent(child)
let effective = text.isEmpty ? (elementConstraints[child.name.localName]?.value ?? text) : text
if let error = simple.validate(effective) {
errors.append(XSDFailure(reason: "'\(child.name.localName)': \(error)", at: path))
}
errors += elementFixedErrors(child, at: path)

View File

@ -19,6 +19,13 @@ extension PureXML.Schema {
// Restricting a user type inherits its base, facets, and variety.
let baseType = simpleTypeReference(XSDNode.attribute(restriction, "base") ?? "string", context)
var facets = baseType.facets
// A restriction's own `enumeration` replaces the inherited set (the new
// enumeration is the value space, not a union with the base's), whereas
// `pattern` accumulates across steps and is ANDed. So drop the inherited
// enumeration when this step declares its own.
if XSDNode.elementChildren(restriction).contains(where: { XSDNode.localName($0) == "enumeration" }) {
facets.enumeration = nil
}
applyFacets(restriction, into: &facets)
return SimpleType(base: baseType.base, facets: facets, variety: baseType.variety)
}

View File

@ -0,0 +1,54 @@
@testable import PureXML
import Testing
/// An element with a `default` or `fixed` value takes that value when it appears
/// empty, so the empty content is valid even when the type would reject an empty
/// string (#147, XSTS valueConstraint set). Present content is still validated.
@Suite("Element default and fixed value constraints")
struct SchemaValueConstraintTests {
private func element(_ constraint: String) throws -> PureXML.Schema.Document {
try PureXML.Schema.Document("""
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="v" type="xs:decimal" \(constraint)/>
</xs:schema>
""")
}
@Test("An empty element with a default takes the default and is valid")
func test_defaultEmptyValid() throws {
let doc = try element(#"default="12""#)
#expect(try doc.validate("<v/>").isEmpty) // empty: default applies
#expect(try doc.validate("<v>7</v>").isEmpty) // present valid decimal
#expect(try !doc.validate("<v>abc</v>").isEmpty) // present content still validated
}
@Test("An empty element with a fixed value is valid; present content must equal it")
func test_fixed() throws {
let doc = try element(#"fixed="5""#)
#expect(try doc.validate("<v/>").isEmpty) // empty: fixed applies
#expect(try doc.validate("<v>5</v>").isEmpty) // equals fixed
#expect(try !doc.validate("<v>6</v>").isEmpty) // differs from fixed
#expect(try !doc.validate("<v>x</v>").isEmpty) // not even a decimal
}
@Test("An empty element's value constraint must still be valid against an xsi:type override")
func test_constraintValueValidatedAgainstXsiType() throws {
let doc = try PureXML.Schema.Document("""
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="v" type="B" fixed="a"/>
<xs:simpleType name="B">
<xs:restriction base="xs:string">
<xs:enumeration value="a"/><xs:enumeration value="b"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="D">
<xs:restriction base="B"><xs:enumeration value="b"/></xs:restriction>
</xs:simpleType>
</xs:schema>
""")
// Empty: fixed 'a' is valid against the declared type B.
#expect(try doc.validate("<v/>").isEmpty)
// Empty under xsi:type=D: the fixed value 'a' is not in D's enumeration, so invalid.
#expect(try !doc.validate(#"<v xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="D"/>"#).isEmpty)
}
}

View File

@ -30,8 +30,8 @@ struct XSTSSuiteTests {
/// are written to /tmp/xsts-failures.txt for the burn-down.
private let knownSchemaValidRejected = 75
private let knownSchemaInvalidAccepted = 2467
private let knownInstanceValidRejected = 395
private let knownInstanceInvalidAccepted = 240
private let knownInstanceValidRejected = 369
private let knownInstanceInvalidAccepted = 238
@Test("Every XSTS case behaves: compile, reject, validate, invalidate")
func test_suite() throws {