andrewbober / xsd2jsonschema

A pure JavaScript library for translating complex XML Schemas into JSON Schemas.
https://www.xsd2jsonschema.org
Apache License 2.0
47 stars 25 forks source link

Browser-compatible conversion #30

Closed azaslonov closed 3 years ago

azaslonov commented 3 years ago

Is it possible to make this wonderful library browser compatible, that would do the conversion without referring to the file system?

andrewbober commented 3 years ago

Hi @azaslonov - all file IO was removed from the library in v0.3.1.

https://github.com/andrewbober/xsd2jsonschema/releases/tag/0.3.1

azaslonov commented 3 years ago

Hi @andrewbober, thanks for the quick reply. Looks like there are still dependencies, this is what my browser says:

image

I'm using package version 0.3.7, and using the example from Quickstart with no changes:

const XML_SCHEMA = `
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema  xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:simpleType name="C">
        <xs:restriction base="xs:string">
            <xs:minLength value="1"/>
        </xs:restriction>
    </xs:simpleType>
    <xs:simpleType name="Char_20">
        <xs:restriction base="C">
            <xs:minLength value="1"/>
            <xs:maxLength value="20"/>
        </xs:restriction>
    </xs:simpleType>
</xs:schema>
`;

const Xsd2JsonSchema = require('xsd2jsonschema').Xsd2JsonSchema;
const xs2js = new Xsd2JsonSchema();

const convertedSchemas = xs2js.processAllSchemas({
    schemas: {'hello_world.xsd': XML_SCHEMA}
});
const jsonSchema = convertedSchemas['hello_world.xsd'].getJsonSchema();
console.log(JSON.stringify(jsonSchema, null, 2));
andrewbober commented 3 years ago

Hi @azaslonov - The line you have highlighted simply parses a string to provide a name for the resulting JSON Schema. There is no actual file io. Are you getting a specific error that I can help with?

azaslonov commented 3 years ago

Hi @andrewbober, thanks for your response. The error on the screenshot above is from the browser, so it fails on that line, because of the "path" object which is not defined in the browser environment. I could probably do some stub like window.path = { parse: () => {...} }, but I'm sure other people (with web apps) will hit this error as well, so it would be great to have it working universally in both browser and node.