UNPKG

data-to-jsonschema-to-ts

Version:

Convert plain javascript objects to JSON Schema and TypeScript typings

55 lines 1.86 kB
import { describe, expect, test } from "vitest"; import { convertJsonSchemaToTs } from "./json-to-ts.js"; describe("json-to-ts", () => { 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 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) expect(ts).toBe(`export interface RootObject { foo?: string; bar?: number; } `); }); }); //# sourceMappingURL=json-to-ts.test.js.map