Skip to content

Commit

Permalink
Collect validation messages (#87)
Browse files Browse the repository at this point in the history
* Unexpected root
* XML structure check already in preParser
* Fix message for misplaced text node
* Schema child name collisions
* Throw in non-strict mode if no Edmx root element
* Warnings in CLI
  • Loading branch information
ralfhandl authored Jan 29, 2024
1 parent baa12b5 commit 354758e
Show file tree
Hide file tree
Showing 5 changed files with 1,470 additions and 724 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## 0.9.0 2024-01-24

### Added

- `xml2json`: option `messages` to collect all error messages in non-strict mode

## 0.8.3 2023-08-16

### Fixed
Expand Down
23 changes: 15 additions & 8 deletions examples/miscellaneous2.xml
Original file line number Diff line number Diff line change
@@ -1,26 +1,32 @@
<?xml version="1.0" encoding="utf-8"?>
<edmx:Edmx xmlns:edmx="http://docs.oasis-open.org/odata/ns/edmx" xmlns="http://docs.oasis-open.org/odata/ns/edm" Version="4.0">
<edmx:Edmx xmlns:edmx="http://docs.oasis-open.org/odata/ns/edmx"
xmlns="http://docs.oasis-open.org/odata/ns/edm" Version="4.0">
<edmx:Reference Uri="SomeOther.xml">
<edmx:Include Namespace="Some.Other.Schema" />
</edmx:Reference>
<edmx:Reference Uri="https://oasis-tcs.github.io/odata-vocabularies/vocabularies/Org.OData.Core.V1.xml">
<edmx:Reference
Uri="https://oasis-tcs.github.io/odata-vocabularies/vocabularies/Org.OData.Core.V1.xml">
<edmx:Include Alias="Core" Namespace="Org.OData.Core.V1" />
</edmx:Reference>

<edmx:DataServices>
<Schema Namespace="org.example2">
<!--
Example 34: the entity container Extending will contain all child elements that it defines itself, plus all child
Example 34: the entity container Extending will contain all child elements that it defines itself,
plus all child
elements of the Base entity container located in SomeOtherSchema
-->
<EntityContainer Name="Extending" Extends="Some.Other.Schema.Base">
<FunctionImport Name="Bar" Function="One.Foo" IncludeInServiceDocument="true" EntitySet="Model.Extending/Freds" />
<FunctionImport Name="CreatedEntities" Function="Model.CreatedEntities" EntitySet="org.example2.Extending/Waldos" IncludeInServiceDocument="true" />
<FunctionImport Name="Bar" Function="One.Foo" IncludeInServiceDocument="true"
EntitySet="Model.Extending/Freds" />
<FunctionImport Name="CreatedEntities" Function="Model.CreatedEntities"
EntitySet="org.example2.Extending/Waldos" IncludeInServiceDocument="true" />
<EntitySet Name="Waldos" EntityType="Two.Waldo" />
<EntitySet Name="Freds" EntityType="Two.Fred">
<NavigationPropertyBinding Target="Waldos" Path="Waldos" />
<NavigationPropertyBinding Target="org.example.Container/Freds" Path="MoreWaldos" />
<NavigationPropertyBinding Target="org.example.Container/Orders/Items/Model.E/Nav" Path="EvenMoreWaldos" />
<NavigationPropertyBinding Target="org.example.Container/Orders/Items/Model.E/Nav"
Path="EvenMoreWaldos" />
</EntitySet>
</EntityContainer>
</Schema>
Expand Down Expand Up @@ -61,7 +67,8 @@
</Annotations>

<Annotations Target="One.OddWaldos(Collection(One.Waldo),One.Waldo)">
<Annotation Term="Core.Description" String="Function bound to a collection of Waldos with an additional parameter" />
<Annotation Term="Core.Description"
String="Function bound to a collection of Waldos with an additional parameter" />
</Annotations>
<Annotations Target="One.OddWaldos(Collection(One.Waldo))">
<Annotation Term="Core.Description" String="Function bound to a collection of Waldos" />
Expand All @@ -87,7 +94,7 @@
<PropertyRef Name="ID" />
</Key>
<Property Name="ID" Type="Edm.String" Nullable="false" />
<NavigationProperty Name="Waldos" Type="Collection(Two.Waldo)" Nullable="true" />
<NavigationProperty Name="Waldos" Type="Collection(Two.Waldo)" />
</EntityType>
</Schema>

Expand Down
14 changes: 10 additions & 4 deletions lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,24 @@ function convert(source) {
const xml = fs.readFileSync(source);

try {
const json = csdl.xml2json(xml);
const messages = [];
const json = csdl.xml2json(xml, { messages });
for (const m of messages) {
console.warn(
`${source}:${m.parser.line}:${m.parser.column}: ${m.message}`
);
}

const target =
argv.t || source.substring(0, source.lastIndexOf(".")) + ".json";
console.log(target);

fs.writeFileSync(target, JSON.stringify(json, null, argv.pretty ? 4 : 0));
} catch (e) {
console.error(
colors.red(`${source}:${e.parser.line}:${e.parser.column}: ${e.message}`)
colors.red(
`${source}:${e.parser.line}:${e.parser.column}: ${e.message}\nAt: ${e.parser.construct}`
)
);
console.log(e.stack.split("\n")[1]);
process.exitCode = 1;
return;
}
Expand Down
Loading

0 comments on commit 354758e

Please sign in to comment.