json-schema-generator.js 3.1 KB

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