UNPKG

cumulocity-cypress

Version:
39 lines (38 loc) 1.43 kB
import { InputData, jsonInputForTargetLanguage, quicktype, } from "quicktype-core"; /** * C8ySchemaGenerator implementation using quicktype library with target language * json-schema. From the generated schema, all non-standard keywords are removed * to ensure compatibility with any json-schema validators. * * Quicktype has been reported to cause issues in browser runtimes. If you encounter * issues with quicktype, consider implementing a custom schema generator. */ export class C8yQicktypeSchemaGenerator { async generate(obj, options = {}) { const { name } = options; const inputData = new InputData(); const jsonInput = jsonInputForTargetLanguage("json-schema"); await jsonInput.addSource({ name: name || "root", samples: [JSON.stringify(obj)], }); inputData.addInput(jsonInput); const result = await quicktype({ inputData, lang: "json-schema", }); const schema = JSON.parse(result.lines.join("\n")); this.removeNonStandardKeywords(schema); return schema; } removeNonStandardKeywords(schema) { for (const key in schema) { if (key.startsWith("qt-")) { delete schema[key]; } else if (typeof schema[key] === "object") { this.removeNonStandardKeywords(schema[key]); } } } }