Skip to content

Commit 021b5a1

Browse files
committed
Disable XSD validation when xsi:schemaLocation doesn't declare the
namespace for the document element root. See eclipse-lemminx/lemminx#951 Signed-off-by: azerr <azerr@redhat.com>
1 parent 86e2f0b commit 021b5a1

File tree

3 files changed

+84
-11
lines changed

3 files changed

+84
-11
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,8 @@ The following settings are supported:
8484
* [`xml.codeLens.enabled`](https://github.com/redhat-developer/vscode-xml/blob/master/docs/CodeLens.md): Enable/disable XML CodeLens. Default is `false`.
8585
* [`xml.preferences.showSchemaDocumentationType`](https://github.com/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`.
8686
* [`xml.validation.enabled`](https://github.com/redhat-developer/vscode-xml/blob/master/docs/Validation.md): Enable/disable all validation. Default is `true`.
87-
* `xml.validation.schema`: Enable/disable schema based validation. Default is `true`. Ignored if `xml.validation.enabled` is set to `false`
88-
* [`xml.validation.disallowDocTypeDecl`](https://github.com/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`.
87+
* [`xml.validation.schema.enabled`](https://github.com/redhat-developer/vscode-xml/blob/master/docs/Validation.md#xmlvalidationschemaenabled): Enable/disable schema based validation. Default is `always`. Ignored if [`xml.validation.enabled`](https://github.com/redhat-developer/vscode-xml/blob/master/docs/Validation.md) is set to `false`.
88+
* [`xml.validation.disallowDocTypeDecl`](https://github.com/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`.
8989
* [`xml.validation.resolveExternalEntities`](https://github.com/redhat-developer/vscode-xml/blob/master/docs/Validation.md#resolve-external-entities): Enable/disable resolve of external entities. Default is `false`.
9090
* [`xml.validation.noGrammar`](https://github.com/redhat-developer/vscode-xml/blob/master/docs/Preferences.md#grammar): The message severity when a document has no associated grammar. Defaults to `hint`.
9191
* [`xml.symbols.enabled`](https://github.com/redhat-developer/vscode-xml/blob/master/docs/Symbols.md#xmlsymbolsenabled): Enable/disable document symbols (Outline). Default is `true`.

docs/Validation.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,3 +417,66 @@ If `xml.validation.resolveExternalEntities` is set to `true` (default is `false`
417417
Demonstration of the different behaviour:
418418

419419
![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)
420+
421+
## xml.validation.schema.enabled
422+
423+
The `xml.validation.schema.enabled` gives the capability to enable / disable the validation based on XSD. It can be configured with 3 values:
424+
425+
* `always`: enable schema based validation.
426+
* `never`: disable schema based validation.
427+
* `onValidSchema`: enable schema based validation only when the declared xsi:schemaLocation hint or xsi:noNamespaceSchemaLocation is valid for the root element.
428+
429+
To understand the `onValidSchema` settings value, lets go through an example:
430+
431+
Create the XML `foo.xml` file:
432+
433+
```xml
434+
<foo xmlns="http://foo"
435+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
436+
xsi:schemaLocation="
437+
http://foo foo.xsd">
438+
<bar />
439+
<BAD_ELEMENT />
440+
</foo>
441+
```
442+
443+
Create the XSD `foo.xsd` file (in the same folder as foo.xml) :
444+
445+
```xml
446+
<?xml version="1.0" encoding="UTF-8"?>
447+
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
448+
<xs:element name="foo">
449+
<xs:complexType>
450+
<xs:sequence>
451+
<xs:element name="bar" />
452+
</xs:sequence>
453+
</xs:complexType>
454+
</xs:element>
455+
</xs:schema>
456+
```
457+
458+
In the XML file, the `BAD_ELEMENT` is higlighted as an error. Update the xsi:schemaLocation with bad namespace hint
459+
460+
```xml
461+
<foo>
462+
...
463+
xsi:schemaLocation="
464+
http://bad-foo foo.xsd">
465+
...
466+
</foo>
467+
```
468+
469+
In `always` you will have error, in `onValidSchema` you will have none error.
470+
471+
Now, update the xsi:schemaLocation with bad location hint
472+
473+
```xml
474+
<foo>
475+
...
476+
xsi:schemaLocation="
477+
http://foo bad-foo.xsd">
478+
...
479+
</foo>
480+
```
481+
482+
In `always` you will have error, in `onValidSchema` you will have none error.

package.json

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@
248248
"Ignore `xsi:schemaLocation` content formatting."
249249
],
250250
"default": "none",
251-
"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",
251+
"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",
252252
"scope": "window"
253253
},
254254
"xml.format.splitAttributes": {
@@ -298,7 +298,7 @@
298298
"Documentation comes from the `xs:documentation` and `xs:appinfo` elements.",
299299
"Displays no documentation."
300300
],
301-
"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.",
301+
"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.",
302302
"scope": "window"
303303
},
304304
"xml.validation.enabled": {
@@ -307,10 +307,20 @@
307307
"markdownDescription": "Enable/disable all validation. Default is `true`.",
308308
"scope": "window"
309309
},
310-
"xml.validation.schema": {
311-
"type": "boolean",
312-
"default": true,
313-
"markdownDescription": "Enable/disable schema based validation. Default is `true`. Ignored if `#xml.validation.enabled#` is set to `false`",
310+
"xml.validation.schema.enabled": {
311+
"type": "string",
312+
"default": "always",
313+
"enum": [
314+
"always",
315+
"never",
316+
"onValidSchema"
317+
],
318+
"markdownEnumDescriptions": [
319+
"Enable schema based validation.",
320+
"Disable schema based validation.",
321+
"Enable schema based validation only when the declared xsi:schemaLocation hint or xsi:noNamespaceSchemaLocation is valid for the root element."
322+
],
323+
"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.",
314324
"scope": "window"
315325
},
316326
"xml.validation.disallowDocTypeDecl": {
@@ -356,12 +366,12 @@
356366
"xml.symbols.maxItemsComputed": {
357367
"type": "integer",
358368
"default": 5000,
359-
"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."
369+
"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."
360370
},
361371
"xml.symbols.showReferencedGrammars": {
362372
"type": "boolean",
363373
"default": true,
364-
"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.",
374+
"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.",
365375
"scope": "window"
366376
},
367377
"xml.symbols.filters": {
@@ -405,7 +415,7 @@
405415
"xml.extension.jars": {
406416
"type": "array",
407417
"default": [],
408-
"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",
418+
"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",
409419
"scope": "window"
410420
}
411421
}

0 commit comments

Comments
 (0)