json-schema-generator.cjs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.JSONSchemaGenerator = void 0;
  4. const json_schema_processors_js_1 = require("./json-schema-processors.cjs");
  5. const to_json_schema_js_1 = require("./to-json-schema.cjs");
  6. /**
  7. * Legacy class-based interface for JSON Schema generation.
  8. * This class wraps the new functional implementation to provide backward compatibility.
  9. *
  10. * @deprecated Use the `toJSONSchema` function instead for new code.
  11. *
  12. * @example
  13. * ```typescript
  14. * // Legacy usage (still supported)
  15. * const gen = new JSONSchemaGenerator({ target: "draft-07" });
  16. * gen.process(schema);
  17. * const result = gen.emit(schema);
  18. *
  19. * // Preferred modern usage
  20. * const result = toJSONSchema(schema, { target: "draft-07" });
  21. * ```
  22. */
  23. class JSONSchemaGenerator {
  24. /** @deprecated Access via ctx instead */
  25. get metadataRegistry() {
  26. return this.ctx.metadataRegistry;
  27. }
  28. /** @deprecated Access via ctx instead */
  29. get target() {
  30. return this.ctx.target;
  31. }
  32. /** @deprecated Access via ctx instead */
  33. get unrepresentable() {
  34. return this.ctx.unrepresentable;
  35. }
  36. /** @deprecated Access via ctx instead */
  37. get override() {
  38. return this.ctx.override;
  39. }
  40. /** @deprecated Access via ctx instead */
  41. get io() {
  42. return this.ctx.io;
  43. }
  44. /** @deprecated Access via ctx instead */
  45. get counter() {
  46. return this.ctx.counter;
  47. }
  48. set counter(value) {
  49. this.ctx.counter = value;
  50. }
  51. /** @deprecated Access via ctx instead */
  52. get seen() {
  53. return this.ctx.seen;
  54. }
  55. constructor(params) {
  56. // Normalize target for internal context
  57. let normalizedTarget = params?.target ?? "draft-2020-12";
  58. if (normalizedTarget === "draft-4")
  59. normalizedTarget = "draft-04";
  60. if (normalizedTarget === "draft-7")
  61. normalizedTarget = "draft-07";
  62. this.ctx = (0, to_json_schema_js_1.initializeContext)({
  63. processors: json_schema_processors_js_1.allProcessors,
  64. target: normalizedTarget,
  65. ...(params?.metadata && { metadata: params.metadata }),
  66. ...(params?.unrepresentable && { unrepresentable: params.unrepresentable }),
  67. ...(params?.override && { override: params.override }),
  68. ...(params?.io && { io: params.io }),
  69. });
  70. }
  71. /**
  72. * Process a schema to prepare it for JSON Schema generation.
  73. * This must be called before emit().
  74. */
  75. process(schema, _params = { path: [], schemaPath: [] }) {
  76. return (0, to_json_schema_js_1.process)(schema, this.ctx, _params);
  77. }
  78. /**
  79. * Emit the final JSON Schema after processing.
  80. * Must call process() first.
  81. */
  82. emit(schema, _params) {
  83. // Apply emit params to the context
  84. if (_params) {
  85. if (_params.cycles)
  86. this.ctx.cycles = _params.cycles;
  87. if (_params.reused)
  88. this.ctx.reused = _params.reused;
  89. if (_params.external)
  90. this.ctx.external = _params.external;
  91. }
  92. (0, to_json_schema_js_1.extractDefs)(this.ctx, schema);
  93. const result = (0, to_json_schema_js_1.finalize)(this.ctx, schema);
  94. // Strip ~standard property to match old implementation's return type
  95. const { "~standard": _, ...plainResult } = result;
  96. return plainResult;
  97. }
  98. }
  99. exports.JSONSchemaGenerator = JSONSchemaGenerator;