data-to-jsonschema-to-ts
Version:
Convert plain javascript objects to JSON Schema and TypeScript typings
57 lines • 1.98 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const vitest_1 = require("vitest");
const json_to_ts_js_1 = require("./json-to-ts.js");
(0, vitest_1.describe)("json-to-ts", () => {
(0, vitest_1.test)("should convert json schema to typescript", async () => {
// Given
//{
// $schema: "http://json-schema.org/draft-07/schema#",
// title: "RootObject",
// type: "object",
// properties: {
// foo: {
// type: "string",
// },
// bar: {
// type: "number",
// },
// emptyObject: {
// type: "object",
// },
// },
// }
const schema = {
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Product Catalog Schema",
"type": "object",
"$defs": {
"price": {
"type": "object",
"properties": {
"amount": {
"type": "number",
"description": "The numerical price value",
"minimum": 0,
"exclusiveMinimum": true
},
}
}
},
"properties": {
"price": {
"$ref": "#/$defs/price",
},
}
};
const result = await (0, json_to_ts_js_1.convertJsonSchemaToTs)(schema, {
additionalProperties: true,
});
console.log("result", result);
// replace all line breaks with a space and extra spaces to 1 space
const ts = result.replace(/\n/g, " ").replace(/\s+/g, " ");
// console.log('result', ts)
(0, vitest_1.expect)(ts).toBe(`export interface RootObject { foo?: string; bar?: number; } `);
});
});
//# sourceMappingURL=json-to-ts.test.js.map