diff --git a/CHANGELOG.md b/CHANGELOG.md index a7eddda..34da6ba 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 +- 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}` 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. diff --git a/Sources/Schema/ComplexType.swift b/Sources/Schema/ComplexType.swift index 254ff66..b00507c 100644 --- a/Sources/Schema/ComplexType.swift +++ b/Sources/Schema/ComplexType.swift @@ -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. diff --git a/Sources/Schema/ComplexValidator.swift b/Sources/Schema/ComplexValidator.swift index 08ed526..a35a1dd 100644 --- a/Sources/Schema/ComplexValidator.swift +++ b/Sources/Schema/ComplexValidator.swift @@ -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) diff --git a/Sources/Schema/XSDSimpleParser.swift b/Sources/Schema/XSDSimpleParser.swift index d49e696..5463f83 100644 --- a/Sources/Schema/XSDSimpleParser.swift +++ b/Sources/Schema/XSDSimpleParser.swift @@ -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) } diff --git a/Tests/SchemaValueConstraintTests.swift b/Tests/SchemaValueConstraintTests.swift new file mode 100644 index 0000000..376d0f0 --- /dev/null +++ b/Tests/SchemaValueConstraintTests.swift @@ -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(""" + + + + """) + } + + @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("").isEmpty) // empty: default applies + #expect(try doc.validate("7").isEmpty) // present valid decimal + #expect(try !doc.validate("abc").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("").isEmpty) // empty: fixed applies + #expect(try doc.validate("5").isEmpty) // equals fixed + #expect(try !doc.validate("6").isEmpty) // differs from fixed + #expect(try !doc.validate("x").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(""" + + + + + + + + + + + + """) + // Empty: fixed 'a' is valid against the declared type B. + #expect(try doc.validate("").isEmpty) + // Empty under xsi:type=D: the fixed value 'a' is not in D's enumeration, so invalid. + #expect(try !doc.validate(#""#).isEmpty) + } +} diff --git a/Tests/XSTSSuiteTests.swift b/Tests/XSTSSuiteTests.swift index 8cbb4ac..4dd60b9 100644 --- a/Tests/XSTSSuiteTests.swift +++ b/Tests/XSTSSuiteTests.swift @@ -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 {