Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disable XSD validation when xsi:schemaLocation doesn't declare the namespace for the document element root. #390

Merged
merged 1 commit into from
Jan 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ The following settings are supported:
* [`xml.codeLens.enabled`](https:/redhat-developer/vscode-xml/blob/master/docs/CodeLens.md): Enable/disable XML CodeLens. Default is `false`.
* [`xml.preferences.showSchemaDocumentationType`](https:/redhat-developer/vscode-xml/blob/master/docs/Preferences.md#documentation-type): Specifies the source of the XML schema documentation displayed on hover and completion. Default is `all`.
* [`xml.validation.enabled`](https:/redhat-developer/vscode-xml/blob/master/docs/Validation.md): Enable/disable all validation. Default is `true`.
* `xml.validation.schema`: Enable/disable schema based validation. Default is `true`. Ignored if `xml.validation.enabled` is set to `false`
* [`xml.validation.disallowDocTypeDecl`](https:/redhat-developer/vscode-xml/blob/master/docs/Validation.md#disallow-doc-type-declarations): Enable/disable if a fatal error is thrown if the incoming document contains a DOCTYPE declaration. Default is `false`.
* [`xml.validation.schema.enabled`](https:/redhat-developer/vscode-xml/blob/master/docs/Validation.md#xmlvalidationschemaenabled): Enable/disable schema based validation. Default is `always`. Ignored if [`xml.validation.enabled`](https:/redhat-developer/vscode-xml/blob/master/docs/Validation.md) is set to `false`.
* [`xml.validation.disallowDocTypeDecl`](https:/redhat-developer/vscode-xml/blob/master/docs/Validation.md#disallow-doc-type-declarations): Enable/disable if a fatal error is thrown if the incoming document contains a DOCTYPE declaration. Default is `false`.
* [`xml.validation.resolveExternalEntities`](https:/redhat-developer/vscode-xml/blob/master/docs/Validation.md#resolve-external-entities): Enable/disable resolve of external entities. Default is `false`.
* [`xml.validation.noGrammar`](https:/redhat-developer/vscode-xml/blob/master/docs/Preferences.md#grammar): The message severity when a document has no associated grammar. Defaults to `hint`.
* [`xml.symbols.enabled`](https:/redhat-developer/vscode-xml/blob/master/docs/Symbols.md#xmlsymbolsenabled): Enable/disable document symbols (Outline). Default is `true`.
Expand Down
63 changes: 63 additions & 0 deletions docs/Validation.md
Original file line number Diff line number Diff line change
Expand Up @@ -417,3 +417,66 @@ If `xml.validation.resolveExternalEntities` is set to `true` (default is `false`
Demonstration of the different behaviour:

![When using the default settings, an external entity that has an XML element as its content will not produce a validation error when nested in an element that expects character content. If `xml.validation.resolveExternalEntities` is enabled, then an error will be produced](./images/Validation/ExternalEntityResolvingDemonstration.gif)

## xml.validation.schema.enabled

The `xml.validation.schema.enabled` gives the capability to enable / disable the validation based on XSD. It can be configured with 3 values:

* `always`: enable schema based validation.
* `never`: disable schema based validation.
* `onValidSchema`: enable schema based validation only when the declared xsi:schemaLocation hint or xsi:noNamespaceSchemaLocation is valid for the root element.

To understand the `onValidSchema` settings value, lets go through an example:

Create the XML `foo.xml` file:

```xml
<foo xmlns="http://foo"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://foo foo.xsd">
<bar />
<BAD_ELEMENT />
</foo>
```

Create the XSD `foo.xsd` file (in the same folder as foo.xml) :

```xml
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="foo">
<xs:complexType>
<xs:sequence>
<xs:element name="bar" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
```

In the XML file, the `BAD_ELEMENT` is higlighted as an error. Update the xsi:schemaLocation with bad namespace hint

```xml
<foo>
...
xsi:schemaLocation="
http://bad-foo foo.xsd">
...
</foo>
```

In `always` you will have error, in `onValidSchema` you will have none error.

Now, update the xsi:schemaLocation with bad location hint

```xml
<foo>
...
xsi:schemaLocation="
http://foo bad-foo.xsd">
...
</foo>
```

In `always` you will have error, in `onValidSchema` you will have none error.
28 changes: 19 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@
"Ignore `xsi:schemaLocation` content formatting."
],
"default": "none",
"markdownDescription": "Split `xsi:schemaLocation` content. Default is `none`. Please see [here](command:xml.open.docs?%5B%7B%22page%22%3A%22Formatting%22%2C%22section%22%3A%22xmlformatxsischemalocationsplit%22%7D%5D) for more information",
"markdownDescription": "Split `xsi:schemaLocation` content. Default is `none`. See [here](command:xml.open.docs?%5B%7B%22page%22%3A%22Formatting%22%2C%22section%22%3A%22xmlformatxsischemalocationsplit%22%7D%5D) for more information",
"scope": "window"
},
"xml.format.splitAttributes": {
Expand Down Expand Up @@ -298,7 +298,7 @@
"Documentation comes from the `xs:documentation` and `xs:appinfo` elements.",
"Displays no documentation."
],
"markdownDescription": "Specifies the source of the XML schema documentation displayed on hover and completion. Default is `all`. Please see [here](command:xml.open.docs?%5B%7B%22page%22%3A%22Preferences%22%2C%22section%22%3A%22documentation-type%22%7D%5D) for more information.",
"markdownDescription": "Specifies the source of the XML schema documentation displayed on hover and completion. Default is `all`. See [here](command:xml.open.docs?%5B%7B%22page%22%3A%22Preferences%22%2C%22section%22%3A%22documentation-type%22%7D%5D) for more information.",
"scope": "window"
},
"xml.validation.enabled": {
Expand All @@ -307,10 +307,20 @@
"markdownDescription": "Enable/disable all validation. Default is `true`.",
"scope": "window"
},
"xml.validation.schema": {
"type": "boolean",
"default": true,
"markdownDescription": "Enable/disable schema based validation. Default is `true`. Ignored if `#xml.validation.enabled#` is set to `false`",
"xml.validation.schema.enabled": {
"type": "string",
"default": "always",
"enum": [
"always",
"never",
"onValidSchema"
],
"markdownEnumDescriptions": [
"Enable schema based validation.",
"Disable schema based validation.",
"Enable schema based validation only when the declared xsi:schemaLocation hint or xsi:noNamespaceSchemaLocation is valid for the root element."
],
"markdownDescription": "Enable/disable schema based validation. Default is `always`. Ignored if `#xml.validation.enabled#` is set to `false`. See [here](command:xml.open.docs?%5B%7B%22page%22%3A%22Validation%22%2C%22section%22%3A%22xmlvalidationschemaenabled%22%7D%5D) for more information.",
"scope": "window"
},
"xml.validation.disallowDocTypeDecl": {
Expand Down Expand Up @@ -356,12 +366,12 @@
"xml.symbols.maxItemsComputed": {
"type": "integer",
"default": 5000,
"markdownDescription": "The maximum number of outline symbols and folding regions computed (limited for performance reasons). Default is `5000`. Please see [here](command:xml.open.docs?%5B%7B%22page%22%3A%22Symbols%22%2C%22section%22%3A%22xmlsymbolsmaxitemscomputed%22%7D%5D) for more information."
"markdownDescription": "The maximum number of outline symbols and folding regions computed (limited for performance reasons). Default is `5000`. See [here](command:xml.open.docs?%5B%7B%22page%22%3A%22Symbols%22%2C%22section%22%3A%22xmlsymbolsmaxitemscomputed%22%7D%5D) for more information."
},
"xml.symbols.showReferencedGrammars": {
"type": "boolean",
"default": true,
"markdownDescription": "Show referenced grammars in the Outline. Default is `true`. Please see [here](command:xml.open.docs?%5B%7B%22page%22%3A%22Symbols%22%2C%22section%22%3A%22xmlsymbolsshowreferencedgrammars%22%7D%5D) for more information.",
"markdownDescription": "Show referenced grammars in the Outline. Default is `true`. See [here](command:xml.open.docs?%5B%7B%22page%22%3A%22Symbols%22%2C%22section%22%3A%22xmlsymbolsshowreferencedgrammars%22%7D%5D) for more information.",
"scope": "window"
},
"xml.symbols.filters": {
Expand Down Expand Up @@ -405,7 +415,7 @@
"xml.extension.jars": {
"type": "array",
"default": [],
"markdownDescription": "An array of paths to JARs that should be contributed to the LemMinX classpath. The paths can include glob patterns. This is intended to be used as a tool for developing extensions to vscode-xml. Please see [here](command:xml.open.docs?%5B%7B%22page%22%3A%22Preferences%22%2C%22section%22%3A%22extension-jars%22%7D%5D) for more information",
"markdownDescription": "An array of paths to JARs that should be contributed to the LemMinX classpath. The paths can include glob patterns. This is intended to be used as a tool for developing extensions to vscode-xml. See [here](command:xml.open.docs?%5B%7B%22page%22%3A%22Preferences%22%2C%22section%22%3A%22extension-jars%22%7D%5D) for more information",
"scope": "window"
}
}
Expand Down