Skip to content
Open
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
68 changes: 63 additions & 5 deletions __tests__/XsdTest.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import fs from "fs";
import beautify from 'json-beautify';
import { json2xml } from '../json2xml';
const { xsd2jsonSchema, json2xsd, jsonSchema2xsd, xml2xsd, xml2json } = require('../')
const { xsd2jsonSchema, xml2json } = require('../')

const readModuleFile = (
path,
Expand All @@ -16,7 +14,7 @@ const readModuleFile = (
};

it("Parse complex content xml2json", (done) => {
readModuleFile("./XsdData.xsd", (err, xsdText) => {
readModuleFile("./XsdData.xsd", (_err, xsdText) => {
const jsonO = xml2json(xsdText)
expect(JSON.parse(jsonO)).toMatchSnapshot();
done();
Expand Down Expand Up @@ -295,4 +293,64 @@ it("Parse complex content xsd2jsonSchema", (done) => {
expect(JSON.parse(jsonO)).toMatchSnapshot();
done();
});
});
});

it('Parse xsd has no complex types', () => {
const jsonO = xsd2jsonSchema(`
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="TestElemet" type="SomeIdVariation48_Type">
</xs:element>
<xs:simpleType name="SomeIdVariation48_Type">
<xs:restriction base="xs:string">
<xs:pattern value="[0-9]{4}"/>
</xs:restriction>
</xs:simpleType>
</xs:schema>
`)
expect(JSON.parse(jsonO)).toMatchSnapshot();
});

it('Parse xsd pattern extra escaping characters dot', () => {
const jsonO = xsd2jsonSchema(`
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="TestElemet" type="SomeIdVariation48_Type">
</xs:element>
<xs:complexType name="Ch_struct2">
<xs:sequence>
<xs:element name="Wdt" type="xs:string">
</xs:element>
</xs:sequence>
</xs:complexType>
<xs:simpleType name="SomeIdVariation48_Type">
<xs:restriction base="xs:string">
<xs:pattern value="(\\.[0-9]{3})?"/>
</xs:restriction>
</xs:simpleType>
</xs:schema>
`)
expect(JSON.parse(jsonO)).toMatchSnapshot();
});

it('Parse xsd pattern extra escaping characters plus', () => {
const jsonO = xsd2jsonSchema(`
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="TestElemet" type="SomeIdVariation48_Type">
</xs:element>
<xs:complexType name="Ch_struct2">
<xs:sequence>
<xs:element name="Wdt" type="xs:string">
</xs:element>
</xs:sequence>
</xs:complexType>
<xs:simpleType name="SomeIdVariation48_Type">
<xs:restriction base="xs:string">
<xs:pattern value="((-|\\+)[0-9]{2}:[0-9]{2})?)?)?"/>
</xs:restriction>
</xs:simpleType>
</xs:schema>
`)
expect(JSON.parse(jsonO)).toMatchSnapshot();
});
36 changes: 36 additions & 0 deletions __tests__/__snapshots__/XsdTest.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,42 @@ Object {
}
`;

exports[`Parse xsd has no complex types 1`] = `
Object {
"properties": Object {
"TestElemet": Object {
"pattern": "[0-9]{4}",
"type": "string",
},
},
"type": "object",
}
`;

exports[`Parse xsd pattern extra escaping characters dot 1`] = `
Object {
"properties": Object {
"TestElemet": Object {
"pattern": "(.[0-9]{3})?",
"type": "string",
},
},
"type": "object",
}
`;

exports[`Parse xsd pattern extra escaping characters plus 1`] = `
Object {
"properties": Object {
"TestElemet": Object {
"pattern": "((-|+)[0-9]{2}:[0-9]{2})?)?)?",
"type": "string",
},
},
"type": "object",
}
`;

exports[`Parse xsd restrictions 1`] = `
Object {
"properties": Object {
Expand Down
18 changes: 10 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ const generateSimpleContent = (d, coma = false, restrictions = []) => {
if (maxLength) attrJson += `,"maxLength":"${maxLength}"`
if (minInclusive) attrJson += `,"minimum":${minInclusive}`
if (maxInclusive) attrJson += `,"maximum":${maxInclusive}`
if (pattern) attrJson += `,"pattern":"${pattern}"`
if (pattern) attrJson += `,"pattern":"${pattern.split('\\.').join('.').split('\\+').join('+')}"`
}
}
jsonString += `"${d.attribute_name}":{"type":"${type.replace('xs:', '')}"${attrJson}}${coma ? ',' : ''}`
Expand Down Expand Up @@ -577,13 +577,15 @@ const simplifyJson = (jsonObj) => {
if (complexTypes && !complexTypes.length) complexTypes = [complexTypes]
if (simpleTypes && !simpleTypes.length) simpleTypes = [simpleTypes]

complexTypes.forEach((d) => {
if (d.attribute_name === nametype) {
// delete d.attribute_name
itemObj = d
itemKey = 'xs:complexType'
}
})
if (!itemObj && complexTypes && complexTypes.length) {
complexTypes.forEach((d) => {
if (d.attribute_name === nametype) {
// delete d.attribute_name
itemObj = d
itemKey = 'xs:complexType'
}
})
}

if (!itemObj && simpleTypes && simpleTypes.length) {
simpleTypes.forEach((d) => {
Expand Down