diff --git a/CHANGELOG.md b/CHANGELOG.md index 16292c9..b06f182 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed +- XSTS invalid-schemas-accepted (#145): an element's `default`/`fixed` value constraint requires a content type that admits character data (`cos-valid-default`). An element whose type is, or whose inline complex type has, element-only or empty content may not carry a value constraint; a simple type, mixed content, the ur-type `xs:anyType`, and an untyped element (also `anyType`) stay valid. The check flags only a content type that is certainly element-only or empty, so it cannot over-reject. invalid-schemas-accepted 99 → 96, no false positive (valid-schemas-rejected held at 0; differential PureXML-stricter unchanged at 87 over 9618 schemas), no other bucket moved. + - XSTS invalid-schemas-accepted (#145): the base-type kind of a content-model derivation is enforced. A `complexContent` restriction or extension base must be a complex type (`src-ct.1`), so a `simpleType` or a built-in simple type such as `xs:string` is rejected (the ur-type `xs:anyType` is complex and stays valid). A `simpleContent` extension base must be a simple type or a complex type with simple content (`src-ct.2`), so a complex type with element-only, mixed, or empty content, including `xs:anyType`, is rejected. A locally-declared type takes precedence over the built-in reading, so a schema that targets the XSD namespace and extends its own complex types (the schema-for-schemas) is not rejected. invalid-schemas-accepted 104 → 99, no false positive (valid-schemas-rejected held at 0, corroborated by the differential: PureXML-stricter unchanged at 87 over 9618 schemas), no other bucket moved. - XSTS invalid-schemas-accepted (#145): an identity constraint's content model is enforced. `unique`, `key`, and `keyref` must contain exactly one `selector` followed by one or more `field`s (after an optional `annotation`); a `field` before the `selector`, a second `selector`, or no `field` is now rejected (previously only child-element membership was checked, not their cardinality or order). invalid-schemas-accepted 110 → 104, no false positive (valid-schemas-rejected held at 0), no other bucket moved. diff --git a/Sources/Schema/SchemaCompileValidation.swift b/Sources/Schema/SchemaCompileValidation.swift index 473430e..7d554ed 100644 --- a/Sources/Schema/SchemaCompileValidation.swift +++ b/Sources/Schema/SchemaCompileValidation.swift @@ -367,7 +367,8 @@ extension PureXML.Validation.SchemaCompile { + PureXML.Schema.XSDParser.extensionMixedAgreementErrors(document.schema, document.context, namedTypes) + PureXML.Schema.XSDParser.simpleContentRestrictionBaseErrors(document.schema, document.context, namedTypes) + PureXML.Schema.XSDParser.complexContentBaseKindErrors(document.schema, document.context, namedTypes) - + PureXML.Schema.XSDParser.simpleContentExtensionBaseKindErrors(document.schema, document.context, namedTypes), + + PureXML.Schema.XSDParser.simpleContentExtensionBaseKindErrors(document.schema, document.context, namedTypes) + + PureXML.Schema.XSDParser.elementValueConstraintContentErrors(document.schema, document.context, namedTypes), ) } } diff --git a/Sources/Schema/SchemaExtensionAllGroup.swift b/Sources/Schema/SchemaExtensionAllGroup.swift index c38aeac..fad60bf 100644 --- a/Sources/Schema/SchemaExtensionAllGroup.swift +++ b/Sources/Schema/SchemaExtensionAllGroup.swift @@ -201,6 +201,54 @@ extension PureXML.Schema.XSDParser { return errors } + /// XSD 1.0 `cos-valid-default` / `e-props-correct`: an element with a `default` + /// or `fixed` value constraint must have a content type that admits character + /// data, i.e. a simple type or mixed content. An element whose type is (or whose + /// inline complex type has) element-only or empty content may not carry a value + /// constraint. Conservative by construction: it flags only a content type that is + /// CERTAINLY element-only or empty. A simple type, mixed content, the ur-type + /// `xs:anyType` (mixed), an untyped element (also `anyType`), a complexContent + /// derivation (mixedness may be inherited), and a foreign or unresolved type are + /// all left alone, so this cannot raise a false positive. Self-contained schema. + static func elementValueConstraintContentErrors(_ schema: XSDTree, _ context: PureXML.Schema.XSDContext, _ types: [String: PureXML.Schema.ElementType]) -> [String] { + guard !skipsCrossDocumentRules(schema, compositionLoaded: context.compositionLoaded) else { return [] } + let bindings = context.namespaceBindings + var errors: [String] = [] + for element in descendants(schema, named: "element") where element.name?.namespaceURI == xsdNamespace { + guard ExtAllNode.attribute(element, "fixed") != nil || ExtAllNode.attribute(element, "default") != nil, + valueConstraintForbiddenByContent(element, bindings, context, types) + else { continue } + errors.append("an element with a 'default' or 'fixed' value must have simple or mixed content, not element-only or empty content") + } + return errors + } + + /// Whether `element`'s content type is certainly element-only or empty, so a + /// value constraint on it is invalid. Returns false (no error) for anything that + /// admits character data or that cannot be resolved with certainty. + private static func valueConstraintForbiddenByContent( + _ element: XSDTree, + _ bindings: [String: String], + _ context: PureXML.Schema.XSDContext, + _ types: [String: PureXML.Schema.ElementType], + ) -> Bool { + if let inline = ExtAllNode.firstChild(element, named: "complexType") { + if ExtAllNode.firstChild(inline, named: "simpleContent") != nil { return false } + if ["true", "1"].contains(ExtAllNode.attribute(inline, "mixed")) { return false } + if ExtAllNode.firstChild(inline, named: "complexContent") != nil { return false } + return ["all", "sequence", "choice", "group"].contains { ExtAllNode.firstChild(inline, named: $0) != nil } + } + guard let typeName = ExtAllNode.attribute(element, "type") else { return false } + let namespace = ExtAllNode.referenceNamespace(typeName, bindings) + let local = ExtAllNode.stripPrefix(typeName) + if namespace == xsdNamespace { return false } + guard namespace == context.targetNamespace, case let .complex(complex)? = types[local] else { return false } + switch complex.content { + case .elementOnly, .empty: return true + case .simpleContent, .mixed: return false + } + } + /// XSD 1.0 `cos-ct-extends.1.4.3.2.2.1`: a `complexContent` extension's mixedness /// must match its base type's. The effective content joins the base content and /// the extension's, so a mixed extension may extend only a mixed base and an diff --git a/Tests/SchemaExtensionAllGroupTests.swift b/Tests/SchemaExtensionAllGroupTests.swift index be48dbb..5b3a3b1 100644 --- a/Tests/SchemaExtensionAllGroupTests.swift +++ b/Tests/SchemaExtensionAllGroupTests.swift @@ -111,4 +111,24 @@ struct SchemaExtensionAllGroupTests { """ #expect((try? PureXML.Schema.Document(document)) != nil) } + + /// cos-valid-default: an element with a `default`/`fixed` value constraint needs + /// simple or mixed content. An element-only type (inline or named) is rejected; + /// a simple type, `xs:anyType` (mixed), an untyped element (anyType), a mixed or + /// simple-content complex type, all stay valid. + @Test("an element value constraint requires simple or mixed content (cos-valid-default)") + func test_valueConstraintContentType() { + // Inline element-only complex type with a fixed value (elemG003/elemG004). + #expect(!compiles("\(allContent)")) + // Named element-only complex type with a default value (elemD004). + #expect(!compiles(seqBase + "")) + // Valid: a simple type, the ur-type, an untyped (anyType) element. + #expect(compiles("")) + #expect(compiles("")) + #expect(compiles("")) + // Valid: a complex type with simple content, and a mixed complex type. + #expect(compiles(simpleBase + "")) + let mixedType = "" + #expect(compiles(mixedType + "")) + } } diff --git a/Tests/XSTSSuiteTests.swift b/Tests/XSTSSuiteTests.swift index c55f6c9..549463b 100644 --- a/Tests/XSTSSuiteTests.swift +++ b/Tests/XSTSSuiteTests.swift @@ -149,9 +149,11 @@ struct XSTSSuiteTests { /// base-type kind (src-ct.1 a complexContent base is complex; src-ct.2 a /// simpleContent extension base is a simple type or a simple-content complex type; /// a local type takes precedence over the built-in reading so the schema-for- - /// schemas, which targets the XSD namespace, is not rejected) 104 -> 99. + /// schemas, which targets the XSD namespace, is not rejected) 104 -> 99; and the + /// element value-constraint content type (cos-valid-default: a default/fixed needs + /// simple or mixed content, not element-only) 99 -> 96. private let knownSchemaValidRejected = 0 - private let knownSchemaInvalidAccepted = 99 + private let knownSchemaInvalidAccepted = 96 private let knownInstanceValidRejected = 0 private let knownInstanceInvalidAccepted = 133