parseDef.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.parseDef = void 0;
  4. const Options_js_1 = require("./Options.js");
  5. const selectParser_js_1 = require("./selectParser.js");
  6. const getRelativePath_js_1 = require("./getRelativePath.js");
  7. const any_js_1 = require("./parsers/any.js");
  8. function parseDef(def, refs, forceResolution = false) {
  9. const seenItem = refs.seen.get(def);
  10. if (refs.override) {
  11. const overrideResult = refs.override?.(def, refs, seenItem, forceResolution);
  12. if (overrideResult !== Options_js_1.ignoreOverride) {
  13. return overrideResult;
  14. }
  15. }
  16. if (seenItem && !forceResolution) {
  17. const seenSchema = get$ref(seenItem, refs);
  18. if (seenSchema !== undefined) {
  19. return seenSchema;
  20. }
  21. }
  22. const newItem = { def, path: refs.currentPath, jsonSchema: undefined };
  23. refs.seen.set(def, newItem);
  24. const jsonSchemaOrGetter = (0, selectParser_js_1.selectParser)(def, def.typeName, refs);
  25. // If the return was a function, then the inner definition needs to be extracted before a call to parseDef (recursive)
  26. const jsonSchema = typeof jsonSchemaOrGetter === "function"
  27. ? parseDef(jsonSchemaOrGetter(), refs)
  28. : jsonSchemaOrGetter;
  29. if (jsonSchema) {
  30. addMeta(def, refs, jsonSchema);
  31. }
  32. if (refs.postProcess) {
  33. const postProcessResult = refs.postProcess(jsonSchema, def, refs);
  34. newItem.jsonSchema = jsonSchema;
  35. return postProcessResult;
  36. }
  37. newItem.jsonSchema = jsonSchema;
  38. return jsonSchema;
  39. }
  40. exports.parseDef = parseDef;
  41. const get$ref = (item, refs) => {
  42. switch (refs.$refStrategy) {
  43. case "root":
  44. return { $ref: item.path.join("/") };
  45. case "relative":
  46. return { $ref: (0, getRelativePath_js_1.getRelativePath)(refs.currentPath, item.path) };
  47. case "none":
  48. case "seen": {
  49. if (item.path.length < refs.currentPath.length &&
  50. item.path.every((value, index) => refs.currentPath[index] === value)) {
  51. console.warn(`Recursive reference detected at ${refs.currentPath.join("/")}! Defaulting to any`);
  52. return (0, any_js_1.parseAnyDef)(refs);
  53. }
  54. return refs.$refStrategy === "seen" ? (0, any_js_1.parseAnyDef)(refs) : undefined;
  55. }
  56. }
  57. };
  58. const addMeta = (def, refs, jsonSchema) => {
  59. if (def.description) {
  60. jsonSchema.description = def.description;
  61. if (refs.markdownDescription) {
  62. jsonSchema.markdownDescription = def.description;
  63. }
  64. }
  65. return jsonSchema;
  66. };