@terreno/api 0.31.0 → 56.0.0-beta.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (232) hide show
  1. package/dist/api.d.ts +10 -0
  2. package/dist/api.js +110 -361
  3. package/dist/auth.d.ts +26 -0
  4. package/dist/auth.js +121 -26
  5. package/dist/betterAuth.d.ts +5 -0
  6. package/dist/betterAuthSetup.js +23 -4
  7. package/dist/docLoader.js +12 -9
  8. package/dist/errors.d.ts +13 -0
  9. package/dist/errors.js +29 -1
  10. package/dist/index.d.ts +17 -1
  11. package/dist/index.js +29 -1
  12. package/dist/middleware.d.ts +7 -0
  13. package/dist/middleware.js +16 -1
  14. package/dist/openApiCompat.d.ts +10 -1
  15. package/dist/openApiCompat.js +115 -47
  16. package/dist/permissions.d.ts +27 -0
  17. package/dist/permissions.js +42 -1
  18. package/dist/plugins.d.ts +13 -0
  19. package/dist/plugins.js +47 -1
  20. package/dist/realtime/changeStreamWatcher.d.ts +100 -3
  21. package/dist/realtime/changeStreamWatcher.js +542 -137
  22. package/dist/realtime/realtimeApp.d.ts +2 -0
  23. package/dist/realtime/realtimeApp.js +83 -27
  24. package/dist/realtime/sessionRevalidation.d.ts +90 -0
  25. package/dist/realtime/sessionRevalidation.js +438 -0
  26. package/dist/realtime/socketAuth.d.ts +97 -0
  27. package/dist/realtime/socketAuth.js +272 -0
  28. package/dist/realtime/socketUser.d.ts +54 -0
  29. package/dist/realtime/socketUser.js +75 -5
  30. package/dist/realtime/types.d.ts +54 -0
  31. package/dist/secretProviders.d.ts +23 -0
  32. package/dist/secretProviders.js +9 -3
  33. package/dist/sync/executors.d.ts +198 -0
  34. package/dist/sync/executors.js +670 -0
  35. package/dist/sync/loadHarness.js +807 -0
  36. package/dist/sync/models.d.ts +216 -0
  37. package/dist/sync/models.js +644 -0
  38. package/dist/sync/mutationHandler.d.ts +97 -0
  39. package/dist/sync/mutationHandler.js +886 -0
  40. package/dist/sync/registry.d.ts +73 -0
  41. package/dist/sync/registry.js +223 -0
  42. package/dist/sync/routes.d.ts +62 -0
  43. package/dist/sync/routes.js +936 -0
  44. package/dist/sync/scripts/compactTombstones.d.ts +47 -0
  45. package/dist/sync/scripts/compactTombstones.js +270 -0
  46. package/dist/sync/serialize.d.ts +20 -0
  47. package/dist/{tests/bunSetup.js → sync/serialize.js} +35 -68
  48. package/dist/sync/socketHandlers.d.ts +54 -0
  49. package/dist/sync/socketHandlers.js +470 -0
  50. package/dist/sync/streams.d.ts +85 -0
  51. package/dist/sync/streams.js +221 -0
  52. package/dist/sync/syncApp.d.ts +29 -0
  53. package/dist/sync/syncApp.js +44 -0
  54. package/dist/sync/syncBatch.d.ts +71 -0
  55. package/dist/sync/syncBatch.js +252 -0
  56. package/dist/sync/syncSeqPlugin.d.ts +8 -0
  57. package/dist/sync/syncSeqPlugin.js +899 -0
  58. package/dist/sync/types.d.ts +254 -0
  59. package/dist/sync/types.js +9 -0
  60. package/dist/terrenoApp.js +90 -27
  61. package/dist/vendor/wesleytodd-openapi/index.d.ts +1 -1
  62. package/dist/vendor/wesleytodd-openapi/lib/ui.d.ts +2 -1
  63. package/package.json +7 -6
  64. package/src/__tests__/openApiCompat.test.ts +20 -0
  65. package/src/api.errors.test.ts +32 -23
  66. package/src/api.test.ts +12 -0
  67. package/src/api.ts +87 -296
  68. package/src/auth.test.ts +202 -0
  69. package/src/auth.ts +134 -23
  70. package/src/betterAuth.ts +6 -0
  71. package/src/betterAuthSetup.test.ts +34 -0
  72. package/src/betterAuthSetup.ts +20 -1
  73. package/src/configuration.test.ts +59 -38
  74. package/src/configurationPlugin.test.ts +72 -42
  75. package/src/docLoader.ts +3 -1
  76. package/src/errors.ts +33 -0
  77. package/src/expressServer.test.ts +7 -1
  78. package/src/index.ts +42 -0
  79. package/src/middleware.test.ts +56 -1
  80. package/src/middleware.ts +15 -0
  81. package/src/openApiCompat.ts +99 -45
  82. package/src/permissions.test.ts +101 -17
  83. package/src/permissions.ts +49 -0
  84. package/src/plugins.test.ts +64 -0
  85. package/src/plugins.ts +56 -0
  86. package/src/realtime/changeStreamWatcher.ts +552 -114
  87. package/src/realtime/realtime.test.ts +157 -0
  88. package/src/realtime/realtimeApp.ts +82 -14
  89. package/src/realtime/realtimeAppSetup.test.ts +25 -27
  90. package/src/realtime/sessionRevalidation.test.ts +423 -0
  91. package/src/realtime/sessionRevalidation.ts +273 -0
  92. package/src/realtime/socketAuth.test.ts +355 -0
  93. package/src/realtime/socketAuth.ts +228 -0
  94. package/src/realtime/socketUser.ts +69 -0
  95. package/src/realtime/types.ts +54 -0
  96. package/src/secretProviders.test.ts +7 -1
  97. package/src/secretProviders.ts +15 -3
  98. package/src/secretProvidersGcpClient.test.ts +66 -0
  99. package/src/sync/ensureSyncIndexes.test.ts +144 -0
  100. package/src/sync/executors.test.ts +882 -0
  101. package/src/sync/executors.ts +705 -0
  102. package/src/sync/integration.test.ts +1160 -0
  103. package/src/sync/loadHarness.ts +618 -0
  104. package/src/sync/models.ts +641 -0
  105. package/src/sync/mutationHandler.test.ts +1252 -0
  106. package/src/sync/mutationHandler.ts +850 -0
  107. package/src/sync/registry.ts +232 -0
  108. package/src/sync/routes.ts +812 -0
  109. package/src/sync/scripts/compactTombstones.test.ts +249 -0
  110. package/src/sync/scripts/compactTombstones.ts +145 -0
  111. package/src/sync/serialize.ts +48 -0
  112. package/src/sync/sessionRevalidationIntegration.test.ts +281 -0
  113. package/src/sync/socketHandlers.ts +320 -0
  114. package/src/sync/streams.ts +188 -0
  115. package/src/sync/sync.test.ts +582 -0
  116. package/src/sync/syncApp.ts +44 -0
  117. package/src/sync/syncBatch.ts +219 -0
  118. package/src/sync/syncFrontier.test.ts +494 -0
  119. package/src/sync/syncPhaseC.test.ts +718 -0
  120. package/src/sync/syncRoutes.test.ts +865 -0
  121. package/src/sync/syncSeqPlugin.ts +730 -0
  122. package/src/sync/syncSocket.test.ts +1440 -0
  123. package/src/sync/types.ts +270 -0
  124. package/src/terrenoApp.ts +37 -17
  125. package/src/tests/bunSetup.ts +8 -0
  126. package/tsconfig.json +4 -4
  127. package/tsconfig.typedoc.json +7 -1
  128. package/dist/__tests__/openApiCompat.test.js +0 -317
  129. package/dist/__tests__/versionCheckPlugin.test.d.ts +0 -1
  130. package/dist/__tests__/versionCheckPlugin.test.js +0 -597
  131. package/dist/actions.openApi.test.d.ts +0 -1
  132. package/dist/actions.openApi.test.js +0 -253
  133. package/dist/actions.test.d.ts +0 -1
  134. package/dist/actions.test.js +0 -1087
  135. package/dist/api.arrayOperations.test.d.ts +0 -1
  136. package/dist/api.arrayOperations.test.js +0 -878
  137. package/dist/api.asyncHandler.test.d.ts +0 -1
  138. package/dist/api.asyncHandler.test.js +0 -236
  139. package/dist/api.coverage.test.d.ts +0 -1
  140. package/dist/api.coverage.test.js +0 -192
  141. package/dist/api.errorPaths.test.d.ts +0 -1
  142. package/dist/api.errorPaths.test.js +0 -887
  143. package/dist/api.errors.test.d.ts +0 -1
  144. package/dist/api.errors.test.js +0 -185
  145. package/dist/api.hooks.test.d.ts +0 -1
  146. package/dist/api.hooks.test.js +0 -950
  147. package/dist/api.query.test.d.ts +0 -1
  148. package/dist/api.query.test.js +0 -948
  149. package/dist/api.test.d.ts +0 -1
  150. package/dist/api.test.js +0 -2820
  151. package/dist/auth.compile.test.d.ts +0 -1
  152. package/dist/auth.compile.test.js +0 -201
  153. package/dist/auth.coverage.test.d.ts +0 -1
  154. package/dist/auth.coverage.test.js +0 -202
  155. package/dist/auth.test.d.ts +0 -1
  156. package/dist/auth.test.js +0 -2286
  157. package/dist/betterAuth.test.d.ts +0 -1
  158. package/dist/betterAuth.test.js +0 -181
  159. package/dist/betterAuthApp.test.d.ts +0 -1
  160. package/dist/betterAuthApp.test.js +0 -242
  161. package/dist/betterAuthSetup.test.d.ts +0 -1
  162. package/dist/betterAuthSetup.test.js +0 -762
  163. package/dist/config.test.d.ts +0 -1
  164. package/dist/config.test.js +0 -353
  165. package/dist/configuration.test.d.ts +0 -1
  166. package/dist/configuration.test.js +0 -980
  167. package/dist/configurationPlugin.test.d.ts +0 -1
  168. package/dist/configurationPlugin.test.js +0 -827
  169. package/dist/consentApp.test.d.ts +0 -1
  170. package/dist/consentApp.test.js +0 -1411
  171. package/dist/docLoader.test.d.ts +0 -1
  172. package/dist/docLoader.test.js +0 -137
  173. package/dist/envConfigurationPlugin.test.d.ts +0 -1
  174. package/dist/envConfigurationPlugin.test.js +0 -369
  175. package/dist/errors.test.d.ts +0 -1
  176. package/dist/errors.test.js +0 -607
  177. package/dist/expressServer.test.d.ts +0 -1
  178. package/dist/expressServer.test.js +0 -1249
  179. package/dist/githubAuth.test.d.ts +0 -1
  180. package/dist/githubAuth.test.js +0 -1142
  181. package/dist/httpClient.test.d.ts +0 -1
  182. package/dist/httpClient.test.js +0 -1471
  183. package/dist/logger.test.d.ts +0 -1
  184. package/dist/logger.test.js +0 -685
  185. package/dist/middleware.test.d.ts +0 -1
  186. package/dist/middleware.test.js +0 -241
  187. package/dist/notifiers/googleChatNotifier.test.d.ts +0 -1
  188. package/dist/notifiers/googleChatNotifier.test.js +0 -298
  189. package/dist/notifiers/slackNotifier.test.d.ts +0 -1
  190. package/dist/notifiers/slackNotifier.test.js +0 -297
  191. package/dist/notifiers/zoomNotifier.test.d.ts +0 -1
  192. package/dist/notifiers/zoomNotifier.test.js +0 -373
  193. package/dist/openApi.test.d.ts +0 -1
  194. package/dist/openApi.test.js +0 -620
  195. package/dist/openApiBuilder.test.d.ts +0 -1
  196. package/dist/openApiBuilder.test.js +0 -599
  197. package/dist/openApiEtag.test.d.ts +0 -1
  198. package/dist/openApiEtag.test.js +0 -86
  199. package/dist/openApiValidator.test.d.ts +0 -1
  200. package/dist/openApiValidator.test.js +0 -795
  201. package/dist/permissions.middleware.test.d.ts +0 -1
  202. package/dist/permissions.middleware.test.js +0 -308
  203. package/dist/permissions.test.d.ts +0 -1
  204. package/dist/permissions.test.js +0 -439
  205. package/dist/plugins.test.d.ts +0 -1
  206. package/dist/plugins.test.js +0 -902
  207. package/dist/populate.test.d.ts +0 -1
  208. package/dist/populate.test.js +0 -370
  209. package/dist/realtime/realtime.test.d.ts +0 -10
  210. package/dist/realtime/realtime.test.js +0 -4213
  211. package/dist/realtime/realtimeAppSetup.test.d.ts +0 -1
  212. package/dist/realtime/realtimeAppSetup.test.js +0 -213
  213. package/dist/requestContext.test.d.ts +0 -1
  214. package/dist/requestContext.test.js +0 -412
  215. package/dist/scriptRunner.test.d.ts +0 -1
  216. package/dist/scriptRunner.test.js +0 -386
  217. package/dist/secretProviders.test.d.ts +0 -1
  218. package/dist/secretProviders.test.js +0 -726
  219. package/dist/syncConsents.test.d.ts +0 -1
  220. package/dist/syncConsents.test.js +0 -522
  221. package/dist/terrenoApp.test.d.ts +0 -1
  222. package/dist/terrenoApp.test.js +0 -489
  223. package/dist/tests/bunSetup.d.ts +0 -1
  224. package/dist/tests/fixtures/compileAuthEntry.d.ts +0 -1
  225. package/dist/tests/fixtures/compileAuthEntry.js +0 -37
  226. package/dist/tests/testEnv.d.ts +0 -0
  227. package/dist/tests/testEnv.js +0 -6
  228. package/dist/transformers.test.d.ts +0 -1
  229. package/dist/transformers.test.js +0 -423
  230. package/dist/utils.test.d.ts +0 -1
  231. package/dist/utils.test.js +0 -242
  232. /package/dist/{__tests__/openApiCompat.test.d.ts → sync/loadHarness.d.ts} +0 -0
@@ -1,795 +0,0 @@
1
- "use strict";
2
- var __assign = (this && this.__assign) || function () {
3
- __assign = Object.assign || function(t) {
4
- for (var s, i = 1, n = arguments.length; i < n; i++) {
5
- s = arguments[i];
6
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
- t[p] = s[p];
8
- }
9
- return t;
10
- };
11
- return __assign.apply(this, arguments);
12
- };
13
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
14
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
15
- return new (P || (P = Promise))(function (resolve, reject) {
16
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
17
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
18
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
19
- step((generator = generator.apply(thisArg, _arguments || [])).next());
20
- });
21
- };
22
- var __generator = (this && this.__generator) || function (thisArg, body) {
23
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
24
- return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
25
- function verb(n) { return function (v) { return step([n, v]); }; }
26
- function step(op) {
27
- if (f) throw new TypeError("Generator is already executing.");
28
- while (g && (g = 0, op[0] && (_ = 0)), _) try {
29
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
30
- if (y = 0, t) op = [op[0] & 2, t.value];
31
- switch (op[0]) {
32
- case 0: case 1: t = op; break;
33
- case 4: _.label++; return { value: op[1], done: false };
34
- case 5: _.label++; y = op[1]; op = [0]; continue;
35
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
36
- default:
37
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
38
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
39
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
40
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
41
- if (t[2]) _.ops.pop();
42
- _.trys.pop(); continue;
43
- }
44
- op = body.call(thisArg, _);
45
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
46
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
47
- }
48
- };
49
- var __read = (this && this.__read) || function (o, n) {
50
- var m = typeof Symbol === "function" && o[Symbol.iterator];
51
- if (!m) return o;
52
- var i = m.call(o), r, ar = [], e;
53
- try {
54
- while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
55
- }
56
- catch (error) { e = { error: error }; }
57
- finally {
58
- try {
59
- if (r && !r.done && (m = i["return"])) m.call(i);
60
- }
61
- finally { if (e) throw e.error; }
62
- }
63
- return ar;
64
- };
65
- var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
66
- if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
67
- if (ar || !(i in from)) {
68
- if (!ar) ar = Array.prototype.slice.call(from, 0, i);
69
- ar[i] = from[i];
70
- }
71
- }
72
- return to.concat(ar || Array.prototype.slice.call(from));
73
- };
74
- Object.defineProperty(exports, "__esModule", { value: true });
75
- // noExplicitAny: test mock typing
76
- // biome-ignore-all lint/suspicious/noExplicitAny: test mock typing
77
- var bun_test_1 = require("bun:test");
78
- var api_1 = require("./api");
79
- var auth_1 = require("./auth");
80
- var openApiValidator_1 = require("./openApiValidator");
81
- var permissions_1 = require("./permissions");
82
- var tests_1 = require("./tests");
83
- // RequiredModel has a clean schema that AJV can compile (no non-standard types).
84
- // It has: name (String, required), about (String, optional)
85
- var requiredRouterOptions = {
86
- permissions: {
87
- create: [permissions_1.Permissions.IsAuthenticated],
88
- delete: [permissions_1.Permissions.IsAdmin],
89
- list: [permissions_1.Permissions.IsAuthenticated],
90
- read: [permissions_1.Permissions.IsAuthenticated],
91
- update: [permissions_1.Permissions.IsAuthenticated],
92
- },
93
- queryFields: ["name"],
94
- sort: "-name",
95
- };
96
- var setupFreshApp = function () { return __awaiter(void 0, void 0, void 0, function () {
97
- var freshApp;
98
- return __generator(this, function (_a) {
99
- freshApp = (0, tests_1.getBaseServer)();
100
- (0, auth_1.setupAuth)(freshApp, tests_1.UserModel);
101
- (0, auth_1.addAuthRoutes)(freshApp, tests_1.UserModel);
102
- return [2 /*return*/, freshApp];
103
- });
104
- }); };
105
- (0, bun_test_1.describe)("openApiValidator", function () {
106
- (0, bun_test_1.beforeEach)(function () { return __awaiter(void 0, void 0, void 0, function () {
107
- return __generator(this, function (_a) {
108
- switch (_a.label) {
109
- case 0:
110
- (0, openApiValidator_1.resetOpenApiValidatorConfig)();
111
- return [4 /*yield*/, (0, tests_1.setupDb)()];
112
- case 1:
113
- _a.sent();
114
- return [4 /*yield*/, tests_1.RequiredModel.deleteMany({})];
115
- case 2:
116
- _a.sent();
117
- return [2 /*return*/];
118
- }
119
- });
120
- }); });
121
- (0, bun_test_1.afterEach)(function () {
122
- (0, openApiValidator_1.resetOpenApiValidatorConfig)();
123
- });
124
- (0, bun_test_1.describe)("isConfigured flag", function () {
125
- (0, bun_test_1.it)("is false by default", function () {
126
- (0, bun_test_1.expect)((0, openApiValidator_1.isOpenApiValidatorConfigured)()).toBe(false);
127
- });
128
- (0, bun_test_1.it)("becomes true after configureOpenApiValidator()", function () {
129
- (0, openApiValidator_1.configureOpenApiValidator)();
130
- (0, bun_test_1.expect)((0, openApiValidator_1.isOpenApiValidatorConfigured)()).toBe(true);
131
- });
132
- (0, bun_test_1.it)("resets to false after resetOpenApiValidatorConfig()", function () {
133
- (0, openApiValidator_1.configureOpenApiValidator)();
134
- (0, bun_test_1.expect)((0, openApiValidator_1.isOpenApiValidatorConfigured)()).toBe(true);
135
- (0, openApiValidator_1.resetOpenApiValidatorConfig)();
136
- (0, bun_test_1.expect)((0, openApiValidator_1.isOpenApiValidatorConfigured)()).toBe(false);
137
- });
138
- });
139
- (0, bun_test_1.describe)("no-op when not configured", function () {
140
- (0, bun_test_1.it)("does not strip or validate when not configured", function () { return __awaiter(void 0, void 0, void 0, function () {
141
- var freshApp, admin, res;
142
- return __generator(this, function (_a) {
143
- switch (_a.label) {
144
- case 0: return [4 /*yield*/, setupFreshApp()];
145
- case 1:
146
- freshApp = _a.sent();
147
- freshApp.use("/required", (0, api_1.modelRouter)(tests_1.RequiredModel, requiredRouterOptions));
148
- return [4 /*yield*/, (0, tests_1.authAsUser)(freshApp, "admin")];
149
- case 2:
150
- admin = _a.sent();
151
- return [4 /*yield*/, admin.post("/required").send({ name: "Apple" }).expect(201)];
152
- case 3:
153
- res = _a.sent();
154
- (0, bun_test_1.expect)(res.body.data.name).toBe("Apple");
155
- return [2 /*return*/];
156
- }
157
- });
158
- }); });
159
- });
160
- (0, bun_test_1.describe)("active after configuration", function () {
161
- (0, bun_test_1.it)("strips extra properties when removeAdditional is true", function () { return __awaiter(void 0, void 0, void 0, function () {
162
- var freshApp, admin, res;
163
- return __generator(this, function (_a) {
164
- switch (_a.label) {
165
- case 0:
166
- (0, openApiValidator_1.configureOpenApiValidator)({ removeAdditional: true });
167
- return [4 /*yield*/, setupFreshApp()];
168
- case 1:
169
- freshApp = _a.sent();
170
- freshApp.use("/required", (0, api_1.modelRouter)(tests_1.RequiredModel, requiredRouterOptions));
171
- return [4 /*yield*/, (0, tests_1.authAsUser)(freshApp, "admin")];
172
- case 2:
173
- admin = _a.sent();
174
- return [4 /*yield*/, admin
175
- .post("/required")
176
- .send({ fakeField: "this should be stripped", name: "Apple" })
177
- .expect(201)];
178
- case 3:
179
- res = _a.sent();
180
- (0, bun_test_1.expect)(res.body.data.name).toBe("Apple");
181
- (0, bun_test_1.expect)(res.body.data.fakeField).toBeUndefined();
182
- return [2 /*return*/];
183
- }
184
- });
185
- }); });
186
- (0, bun_test_1.it)("rejects missing required fields", function () { return __awaiter(void 0, void 0, void 0, function () {
187
- var freshApp, admin, res;
188
- return __generator(this, function (_a) {
189
- switch (_a.label) {
190
- case 0:
191
- (0, openApiValidator_1.configureOpenApiValidator)();
192
- return [4 /*yield*/, setupFreshApp()];
193
- case 1:
194
- freshApp = _a.sent();
195
- freshApp.use("/required", (0, api_1.modelRouter)(tests_1.RequiredModel, requiredRouterOptions));
196
- return [4 /*yield*/, (0, tests_1.authAsUser)(freshApp, "admin")];
197
- case 2:
198
- admin = _a.sent();
199
- return [4 /*yield*/, admin.post("/required").send({ about: "no name" }).expect(400)];
200
- case 3:
201
- res = _a.sent();
202
- (0, bun_test_1.expect)(res.body.title).toBe("Request validation failed");
203
- return [2 /*return*/];
204
- }
205
- });
206
- }); });
207
- });
208
- (0, bun_test_1.describe)("onAdditionalPropertiesRemoved hook", function () {
209
- (0, bun_test_1.it)("fires callback with removed property names", function () { return __awaiter(void 0, void 0, void 0, function () {
210
- var removedProps, freshApp, admin;
211
- return __generator(this, function (_a) {
212
- switch (_a.label) {
213
- case 0:
214
- removedProps = [];
215
- (0, openApiValidator_1.configureOpenApiValidator)({
216
- onAdditionalPropertiesRemoved: function (props) {
217
- removedProps.push.apply(removedProps, __spreadArray([], __read(props), false));
218
- },
219
- removeAdditional: true,
220
- });
221
- return [4 /*yield*/, setupFreshApp()];
222
- case 1:
223
- freshApp = _a.sent();
224
- freshApp.use("/required", (0, api_1.modelRouter)(tests_1.RequiredModel, requiredRouterOptions));
225
- return [4 /*yield*/, (0, tests_1.authAsUser)(freshApp, "admin")];
226
- case 2:
227
- admin = _a.sent();
228
- return [4 /*yield*/, admin
229
- .post("/required")
230
- .send({ extraA: "stripped", extraB: "also stripped", name: "Apple" })
231
- .expect(201)];
232
- case 3:
233
- _a.sent();
234
- (0, bun_test_1.expect)(removedProps).toContain("extraA");
235
- (0, bun_test_1.expect)(removedProps).toContain("extraB");
236
- return [2 /*return*/];
237
- }
238
- });
239
- }); });
240
- });
241
- (0, bun_test_1.describe)("per-route validation: false override", function () {
242
- (0, bun_test_1.it)("skips validation when validation is false", function () { return __awaiter(void 0, void 0, void 0, function () {
243
- var freshApp, admin, res;
244
- return __generator(this, function (_a) {
245
- switch (_a.label) {
246
- case 0:
247
- (0, openApiValidator_1.configureOpenApiValidator)({ removeAdditional: true });
248
- return [4 /*yield*/, setupFreshApp()];
249
- case 1:
250
- freshApp = _a.sent();
251
- freshApp.use("/required", (0, api_1.modelRouter)(tests_1.RequiredModel, __assign(__assign({}, requiredRouterOptions), { validation: false })));
252
- return [4 /*yield*/, (0, tests_1.authAsUser)(freshApp, "admin")];
253
- case 2:
254
- admin = _a.sent();
255
- return [4 /*yield*/, admin
256
- .post("/required")
257
- .send({ fakeField: "not stripped", name: "Apple" })
258
- .expect(201)];
259
- case 3:
260
- res = _a.sent();
261
- (0, bun_test_1.expect)(res.body.data.name).toBe("Apple");
262
- return [2 /*return*/];
263
- }
264
- });
265
- }); });
266
- });
267
- (0, bun_test_1.describe)("per-route validation: object-form options", function () {
268
- (0, bun_test_1.it)("applies object validation options with per-operation control", function () { return __awaiter(void 0, void 0, void 0, function () {
269
- var removedProps, errorsCaught, freshApp, admin, res;
270
- return __generator(this, function (_a) {
271
- switch (_a.label) {
272
- case 0:
273
- (0, openApiValidator_1.configureOpenApiValidator)({ removeAdditional: true });
274
- removedProps = [];
275
- errorsCaught = [];
276
- return [4 /*yield*/, setupFreshApp()];
277
- case 1:
278
- freshApp = _a.sent();
279
- freshApp.use("/required", (0, api_1.modelRouter)(tests_1.RequiredModel, __assign(__assign({}, requiredRouterOptions), { validation: {
280
- excludeFromCreate: ["about"],
281
- onAdditionalPropertiesRemoved: function (props) {
282
- removedProps.push.apply(removedProps, __spreadArray([], __read(props), false));
283
- },
284
- onError: function (errors) {
285
- errorsCaught.push(errors);
286
- },
287
- validateCreate: true,
288
- validateQuery: true,
289
- validateUpdate: true,
290
- } })));
291
- return [4 /*yield*/, (0, tests_1.authAsUser)(freshApp, "admin")];
292
- case 2:
293
- admin = _a.sent();
294
- return [4 /*yield*/, admin.post("/required").send({ name: "Validated" }).expect(201)];
295
- case 3:
296
- res = _a.sent();
297
- (0, bun_test_1.expect)(res.body.data.name).toBe("Validated");
298
- return [2 /*return*/];
299
- }
300
- });
301
- }); });
302
- (0, bun_test_1.it)("disables create validation when validateCreate is false", function () { return __awaiter(void 0, void 0, void 0, function () {
303
- var freshApp, admin, res;
304
- return __generator(this, function (_a) {
305
- switch (_a.label) {
306
- case 0:
307
- (0, openApiValidator_1.configureOpenApiValidator)({ removeAdditional: true });
308
- return [4 /*yield*/, setupFreshApp()];
309
- case 1:
310
- freshApp = _a.sent();
311
- freshApp.use("/required", (0, api_1.modelRouter)(tests_1.RequiredModel, __assign(__assign({}, requiredRouterOptions), { validation: {
312
- excludeFromUpdate: ["about"],
313
- validateCreate: false,
314
- validateUpdate: true,
315
- } })));
316
- return [4 /*yield*/, (0, tests_1.authAsUser)(freshApp, "admin")];
317
- case 2:
318
- admin = _a.sent();
319
- return [4 /*yield*/, admin
320
- .post("/required")
321
- .send({ extraField: "ignored", name: "NoCreateValidation" })
322
- .expect(201)];
323
- case 3:
324
- res = _a.sent();
325
- (0, bun_test_1.expect)(res.body.data.name).toBe("NoCreateValidation");
326
- return [2 /*return*/];
327
- }
328
- });
329
- }); });
330
- });
331
- (0, bun_test_1.describe)("sanitization of non-standard mongoose-to-swagger types", function () {
332
- (0, bun_test_1.it)("validates models with ObjectId and DateOnly fields after sanitization", function () { return __awaiter(void 0, void 0, void 0, function () {
333
- var freshApp, admin, res;
334
- return __generator(this, function (_a) {
335
- switch (_a.label) {
336
- case 0:
337
- (0, openApiValidator_1.configureOpenApiValidator)({ removeAdditional: true });
338
- return [4 /*yield*/, setupFreshApp()];
339
- case 1:
340
- freshApp = _a.sent();
341
- freshApp.use("/food", (0, api_1.modelRouter)(tests_1.FoodModel, {
342
- permissions: {
343
- create: [permissions_1.Permissions.IsAuthenticated],
344
- delete: [permissions_1.Permissions.IsAdmin],
345
- list: [permissions_1.Permissions.IsAuthenticated],
346
- read: [permissions_1.Permissions.IsAuthenticated],
347
- update: [permissions_1.Permissions.IsAuthenticated],
348
- },
349
- queryFields: ["name", "calories", "hidden"],
350
- sort: "-created",
351
- }));
352
- return [4 /*yield*/, (0, tests_1.authAsUser)(freshApp, "admin")];
353
- case 2:
354
- admin = _a.sent();
355
- return [4 /*yield*/, admin
356
- .post("/food")
357
- .send({ calories: 100, likesIds: [], name: "Apple", source: { name: "Test" } })
358
- .expect(201)];
359
- case 3:
360
- res = _a.sent();
361
- (0, bun_test_1.expect)(res.body.data.name).toBe("Apple");
362
- return [2 /*return*/];
363
- }
364
- });
365
- }); });
366
- });
367
- (0, bun_test_1.describe)("buildQuerySchemaFromFields", function () {
368
- (0, bun_test_1.it)("always includes limit, page, and sort", function () {
369
- var schema = (0, openApiValidator_1.buildQuerySchemaFromFields)(tests_1.FoodModel, []);
370
- (0, bun_test_1.expect)(schema.limit).toBeDefined();
371
- (0, bun_test_1.expect)(schema.page).toBeDefined();
372
- (0, bun_test_1.expect)(schema.sort).toBeDefined();
373
- });
374
- (0, bun_test_1.it)("includes queryFields from model schema", function () {
375
- var schema = (0, openApiValidator_1.buildQuerySchemaFromFields)(tests_1.FoodModel, ["name", "calories"]);
376
- (0, bun_test_1.expect)(schema.name).toBeDefined();
377
- (0, bun_test_1.expect)(schema.calories).toBeDefined();
378
- (0, bun_test_1.expect)(schema.hidden).toBeUndefined();
379
- });
380
- (0, bun_test_1.it)("marks query fields as not required", function () {
381
- var schema = (0, openApiValidator_1.buildQuerySchemaFromFields)(tests_1.FoodModel, ["name"]);
382
- (0, bun_test_1.expect)(schema.name.required).toBe(false);
383
- });
384
- });
385
- (0, bun_test_1.describe)("validateRequestBody middleware", function () {
386
- (0, bun_test_1.it)("is a no-op when not configured", function () {
387
- (0, openApiValidator_1.resetOpenApiValidatorConfig)();
388
- var middleware = (0, openApiValidator_1.validateRequestBody)({
389
- name: { required: true, type: "string" },
390
- });
391
- var nextCalled = false;
392
- var req = { body: {} };
393
- var res = {};
394
- var next = function () {
395
- nextCalled = true;
396
- };
397
- middleware(req, res, next);
398
- (0, bun_test_1.expect)(nextCalled).toBe(true);
399
- });
400
- (0, bun_test_1.it)("validates when configured", function () {
401
- (0, openApiValidator_1.configureOpenApiValidator)();
402
- var middleware = (0, openApiValidator_1.validateRequestBody)({
403
- name: { required: true, type: "string" },
404
- });
405
- var req = { body: {}, method: "POST", path: "/test" };
406
- var res = {};
407
- (0, bun_test_1.expect)(function () {
408
- middleware(req, res, function () { });
409
- }).toThrow();
410
- });
411
- (0, bun_test_1.it)("calls onError callback instead of throwing when provided at option level", function () {
412
- (0, openApiValidator_1.configureOpenApiValidator)();
413
- var capturedErrors = [];
414
- var middleware = (0, openApiValidator_1.validateRequestBody)({ name: { required: true, type: "string" } }, {
415
- onError: function (errors) {
416
- capturedErrors = errors;
417
- },
418
- });
419
- var nextCalled = false;
420
- var req = { body: {}, method: "POST", path: "/test" };
421
- var res = {};
422
- middleware(req, res, function () {
423
- nextCalled = true;
424
- });
425
- (0, bun_test_1.expect)(nextCalled).toBe(true);
426
- (0, bun_test_1.expect)(capturedErrors.length).toBeGreaterThan(0);
427
- });
428
- (0, bun_test_1.it)("calls global onValidationError when no per-route handler", function () {
429
- var capturedErrors = [];
430
- (0, openApiValidator_1.configureOpenApiValidator)({
431
- onValidationError: function (errors) {
432
- capturedErrors = errors;
433
- },
434
- });
435
- var middleware = (0, openApiValidator_1.validateRequestBody)({ name: { required: true, type: "string" } });
436
- var nextCalled = false;
437
- var req = { body: {}, method: "POST", path: "/test" };
438
- var res = {};
439
- middleware(req, res, function () {
440
- nextCalled = true;
441
- });
442
- (0, bun_test_1.expect)(nextCalled).toBe(true);
443
- (0, bun_test_1.expect)(capturedErrors.length).toBeGreaterThan(0);
444
- });
445
- (0, bun_test_1.it)("skips validation when enabled=false on options", function () {
446
- (0, openApiValidator_1.configureOpenApiValidator)();
447
- var middleware = (0, openApiValidator_1.validateRequestBody)({ name: { required: true, type: "string" } }, { enabled: false });
448
- var nextCalled = false;
449
- var req = { body: {}, method: "POST", path: "/test" };
450
- var res = {};
451
- middleware(req, res, function () {
452
- nextCalled = true;
453
- });
454
- (0, bun_test_1.expect)(nextCalled).toBe(true);
455
- });
456
- (0, bun_test_1.it)("respects validateRequests=false in global config", function () {
457
- (0, openApiValidator_1.configureOpenApiValidator)({ validateRequests: false });
458
- var middleware = (0, openApiValidator_1.validateRequestBody)({ name: { required: true, type: "string" } });
459
- var nextCalled = false;
460
- var req = { body: {}, method: "POST", path: "/test" };
461
- var res = {};
462
- middleware(req, res, function () {
463
- nextCalled = true;
464
- });
465
- (0, bun_test_1.expect)(nextCalled).toBe(true);
466
- });
467
- });
468
- (0, bun_test_1.describe)("validateQueryParams middleware", function () {
469
- (0, bun_test_1.it)("is a no-op when not configured", function () {
470
- (0, openApiValidator_1.resetOpenApiValidatorConfig)();
471
- var middleware = (0, openApiValidator_1.validateQueryParams)({
472
- page: { type: "number" },
473
- });
474
- var nextCalled = false;
475
- var req = { method: "GET", path: "/test", query: {} };
476
- var res = {};
477
- middleware(req, res, function () {
478
- nextCalled = true;
479
- });
480
- (0, bun_test_1.expect)(nextCalled).toBe(true);
481
- });
482
- (0, bun_test_1.it)("coerces types when configured", function () {
483
- (0, openApiValidator_1.configureOpenApiValidator)({ coerceTypes: true });
484
- var middleware = (0, openApiValidator_1.validateQueryParams)({
485
- page: { type: "number" },
486
- });
487
- var req = { method: "GET", path: "/test", query: { page: "3" } };
488
- var res = {};
489
- middleware(req, res, function () { });
490
- (0, bun_test_1.expect)(req.query.page).toBe(3);
491
- });
492
- (0, bun_test_1.it)("throws validation error when query does not match", function () {
493
- (0, openApiValidator_1.configureOpenApiValidator)({ coerceTypes: false });
494
- var middleware = (0, openApiValidator_1.validateQueryParams)({
495
- page: { required: true, type: "number" },
496
- });
497
- var req = { method: "GET", path: "/test", query: {} };
498
- var res = {};
499
- // Required top-level property missing triggers an error
500
- // We need required: [] at schema level via required: true on property. Confirm via calling.
501
- (0, bun_test_1.expect)(function () {
502
- middleware(req, res, function () { });
503
- }).toThrow();
504
- });
505
- (0, bun_test_1.it)("uses onError callback for query validation", function () {
506
- var captured = [];
507
- (0, openApiValidator_1.configureOpenApiValidator)({ coerceTypes: false });
508
- var middleware = (0, openApiValidator_1.validateQueryParams)({ page: { required: true, type: "number" } }, {
509
- onError: function (errors) {
510
- captured = errors;
511
- },
512
- });
513
- var nextCalled = false;
514
- var req = { method: "GET", path: "/test", query: {} };
515
- var res = {};
516
- middleware(req, res, function () {
517
- nextCalled = true;
518
- });
519
- (0, bun_test_1.expect)(nextCalled).toBe(true);
520
- (0, bun_test_1.expect)(captured.length).toBeGreaterThan(0);
521
- });
522
- (0, bun_test_1.it)("skips query validation when enabled=false", function () {
523
- (0, openApiValidator_1.configureOpenApiValidator)();
524
- var middleware = (0, openApiValidator_1.validateQueryParams)({ page: { required: true, type: "number" } }, { enabled: false });
525
- var nextCalled = false;
526
- var req = { method: "GET", path: "/test", query: {} };
527
- var res = {};
528
- middleware(req, res, function () {
529
- nextCalled = true;
530
- });
531
- (0, bun_test_1.expect)(nextCalled).toBe(true);
532
- });
533
- });
534
- (0, bun_test_1.describe)("validateResponseData", function () {
535
- (0, bun_test_1.it)("returns valid when validateResponses is disabled", function () {
536
- (0, openApiValidator_1.configureOpenApiValidator)({ validateResponses: false });
537
- var result = (0, openApiValidator_1.validateResponseData)({ foo: "bar" }, { name: { type: "string" } });
538
- (0, bun_test_1.expect)(result.valid).toBe(true);
539
- });
540
- (0, bun_test_1.it)("validates response shape when validateResponses is enabled", function () {
541
- (0, openApiValidator_1.configureOpenApiValidator)({ validateResponses: true });
542
- var result = (0, openApiValidator_1.validateResponseData)({ name: "Apple" }, { name: { required: true, type: "string" } });
543
- (0, bun_test_1.expect)(result.valid).toBe(true);
544
- });
545
- (0, bun_test_1.it)("returns errors for invalid response shape", function () {
546
- (0, openApiValidator_1.configureOpenApiValidator)({ coerceTypes: false, validateResponses: true });
547
- var result = (0, openApiValidator_1.validateResponseData)({ name: 42 }, { name: { required: true, type: "string" } });
548
- (0, bun_test_1.expect)(result.valid).toBe(false);
549
- (0, bun_test_1.expect)(result.errors).toBeDefined();
550
- });
551
- });
552
- (0, bun_test_1.describe)("createValidator", function () {
553
- (0, bun_test_1.it)("runs body then query validation and calls next once both pass", function () {
554
- (0, openApiValidator_1.configureOpenApiValidator)({ coerceTypes: true });
555
- var middleware = (0, openApiValidator_1.createValidator)({
556
- body: { name: { required: true, type: "string" } },
557
- query: { page: { type: "number" } },
558
- });
559
- var nextCalled = false;
560
- var req = {
561
- body: { name: "ok" },
562
- method: "POST",
563
- path: "/test",
564
- query: { page: "2" },
565
- };
566
- var res = {};
567
- middleware(req, res, function () {
568
- nextCalled = true;
569
- });
570
- (0, bun_test_1.expect)(nextCalled).toBe(true);
571
- (0, bun_test_1.expect)(req.query.page).toBe(2);
572
- });
573
- (0, bun_test_1.it)("skips when neither body nor query schemas are provided", function () {
574
- (0, openApiValidator_1.configureOpenApiValidator)();
575
- var middleware = (0, openApiValidator_1.createValidator)({});
576
- var nextCalled = false;
577
- var req = { body: {}, method: "POST", path: "/test", query: {} };
578
- var res = {};
579
- middleware(req, res, function () {
580
- nextCalled = true;
581
- });
582
- (0, bun_test_1.expect)(nextCalled).toBe(true);
583
- });
584
- (0, bun_test_1.it)("runs only query validation when only query provided", function () {
585
- (0, openApiValidator_1.configureOpenApiValidator)({ coerceTypes: true });
586
- var middleware = (0, openApiValidator_1.createValidator)({
587
- query: { page: { type: "number" } },
588
- });
589
- var nextCalled = false;
590
- var req = { method: "GET", path: "/test", query: { page: "5" } };
591
- var res = {};
592
- middleware(req, res, function () {
593
- nextCalled = true;
594
- });
595
- (0, bun_test_1.expect)(nextCalled).toBe(true);
596
- (0, bun_test_1.expect)(req.query.page).toBe(5);
597
- });
598
- (0, bun_test_1.it)("runs only body validation when no query schema is provided", function () {
599
- (0, openApiValidator_1.configureOpenApiValidator)({ coerceTypes: true });
600
- var middleware = (0, openApiValidator_1.createValidator)({
601
- body: { count: { type: "number" }, name: { required: true, type: "string" } },
602
- });
603
- var req = {
604
- body: { count: "3", name: "ok" },
605
- method: "POST",
606
- path: "/test",
607
- query: { unchecked: "kept" },
608
- };
609
- var res = {};
610
- var nextCalls = [];
611
- middleware(req, res, (function () {
612
- var args = [];
613
- for (var _i = 0; _i < arguments.length; _i++) {
614
- args[_i] = arguments[_i];
615
- }
616
- nextCalls.push(args);
617
- }));
618
- (0, bun_test_1.expect)(nextCalls).toEqual([[]]);
619
- (0, bun_test_1.expect)(req.body.count).toBe(3);
620
- // Query params are untouched when no query schema is configured.
621
- (0, bun_test_1.expect)(req.query.unchecked).toBe("kept");
622
- });
623
- (0, bun_test_1.it)("throws body validation errors when only a body schema is provided", function () {
624
- (0, openApiValidator_1.configureOpenApiValidator)({});
625
- var middleware = (0, openApiValidator_1.createValidator)({
626
- body: { name: { required: true, type: "string" } },
627
- });
628
- var req = { body: {}, method: "POST", path: "/test", query: {} };
629
- var res = {};
630
- (0, bun_test_1.expect)(function () { return middleware(req, res, (function () { })); }).toThrow("Request validation failed");
631
- });
632
- (0, bun_test_1.it)("propagates body validation error via next", function () {
633
- var capturedErrors = [];
634
- (0, openApiValidator_1.configureOpenApiValidator)({
635
- onValidationError: function (errors) {
636
- capturedErrors = errors;
637
- },
638
- });
639
- var middleware = (0, openApiValidator_1.createValidator)({
640
- body: { name: { required: true, type: "string" } },
641
- query: { page: { type: "number" } },
642
- });
643
- var req = { body: {}, method: "POST", path: "/test", query: {} };
644
- var res = {};
645
- var nextCalled = false;
646
- middleware(req, res, function () {
647
- nextCalled = true;
648
- });
649
- (0, bun_test_1.expect)(capturedErrors.length).toBeGreaterThan(0);
650
- (0, bun_test_1.expect)(nextCalled).toBe(true);
651
- });
652
- });
653
- (0, bun_test_1.describe)("createModelValidators", function () {
654
- (0, bun_test_1.beforeEach)(function () {
655
- (0, openApiValidator_1.configureOpenApiValidator)();
656
- });
657
- (0, bun_test_1.it)("returns create and update middleware", function () {
658
- var validators = (0, openApiValidator_1.createModelValidators)(tests_1.RequiredModel);
659
- (0, bun_test_1.expect)(typeof validators.create).toBe("function");
660
- (0, bun_test_1.expect)(typeof validators.update).toBe("function");
661
- });
662
- (0, bun_test_1.it)("create validator rejects body with wrong type", function () {
663
- (0, openApiValidator_1.configureOpenApiValidator)({ coerceTypes: false });
664
- var create = (0, openApiValidator_1.createModelValidators)(tests_1.RequiredModel).create;
665
- var req = { body: { name: 123 }, method: "POST", path: "/required" };
666
- var res = {};
667
- (0, bun_test_1.expect)(function () {
668
- create(req, res, function () { });
669
- }).toThrow();
670
- });
671
- (0, bun_test_1.it)("update validator allows a partial body", function () {
672
- var update = (0, openApiValidator_1.createModelValidators)(tests_1.RequiredModel).update;
673
- var nextCalled = false;
674
- var req = { body: { about: "info" }, method: "PATCH", path: "/required" };
675
- var res = {};
676
- update(req, res, function () {
677
- nextCalled = true;
678
- });
679
- (0, bun_test_1.expect)(nextCalled).toBe(true);
680
- });
681
- (0, bun_test_1.it)("supports onAdditionalPropertiesRemoved option", function () {
682
- (0, openApiValidator_1.configureOpenApiValidator)({ removeAdditional: true });
683
- var removedProps = [];
684
- var create = (0, openApiValidator_1.createModelValidators)(tests_1.RequiredModel, {
685
- onAdditionalPropertiesRemoved: function (props) {
686
- removedProps.push.apply(removedProps, __spreadArray([], __read(props), false));
687
- },
688
- }).create;
689
- // Extra property gets stripped and hook is called
690
- var req = {
691
- body: { extra: "strip me", name: "Apple" },
692
- method: "POST",
693
- path: "/required",
694
- };
695
- create(req, {}, function () { });
696
- (0, bun_test_1.expect)(removedProps).toContain("extra");
697
- });
698
- (0, bun_test_1.it)("supports onError option", function () {
699
- (0, openApiValidator_1.configureOpenApiValidator)({ coerceTypes: false });
700
- var errorHandled = [];
701
- var create = (0, openApiValidator_1.createModelValidators)(tests_1.RequiredModel, {
702
- onError: function (errors) {
703
- errorHandled = errors;
704
- },
705
- }).create;
706
- // Wrong type triggers error handler
707
- var req = { body: { name: 42 }, method: "POST", path: "/required" };
708
- create(req, {}, function () { });
709
- (0, bun_test_1.expect)(errorHandled.length).toBeGreaterThan(0);
710
- });
711
- });
712
- (0, bun_test_1.describe)("validateModelRequestBody", function () {
713
- (0, bun_test_1.beforeEach)(function () {
714
- (0, openApiValidator_1.configureOpenApiValidator)();
715
- });
716
- (0, bun_test_1.it)("creates a validator from a Mongoose model", function () {
717
- var middleware = (0, openApiValidator_1.validateModelRequestBody)(tests_1.RequiredModel);
718
- var req = { body: {}, method: "POST", path: "/required" };
719
- var res = {};
720
- (0, bun_test_1.expect)(function () {
721
- middleware(req, res, function () { });
722
- }).toThrow();
723
- });
724
- (0, bun_test_1.it)("respects excludeFields option", function () {
725
- var middleware = (0, openApiValidator_1.validateModelRequestBody)(tests_1.RequiredModel, {
726
- excludeFields: ["name"],
727
- });
728
- // Without 'name' required, empty body passes
729
- var nextCalled = false;
730
- var req = { body: {}, method: "POST", path: "/required" };
731
- var res = {};
732
- middleware(req, res, function () {
733
- nextCalled = true;
734
- });
735
- (0, bun_test_1.expect)(nextCalled).toBe(true);
736
- });
737
- });
738
- (0, bun_test_1.describe)("getSchemaFromModel", function () {
739
- (0, bun_test_1.it)("returns properties for a Mongoose model", function () {
740
- var schema = (0, openApiValidator_1.getSchemaFromModel)(tests_1.RequiredModel);
741
- (0, bun_test_1.expect)(schema.name).toBeDefined();
742
- (0, bun_test_1.expect)(schema.about).toBeDefined();
743
- });
744
- });
745
- (0, bun_test_1.describe)("getOpenApiValidatorConfig", function () {
746
- (0, bun_test_1.it)("returns a shallow copy of the config", function () {
747
- (0, openApiValidator_1.configureOpenApiValidator)({ removeAdditional: false });
748
- var config = (0, openApiValidator_1.getOpenApiValidatorConfig)();
749
- (0, bun_test_1.expect)(config.removeAdditional).toBe(false);
750
- // Mutating the returned copy should not affect internal state
751
- config.removeAdditional = true;
752
- (0, bun_test_1.expect)((0, openApiValidator_1.getOpenApiValidatorConfig)().removeAdditional).toBe(false);
753
- });
754
- });
755
- (0, bun_test_1.describe)("schema sanitization and validator caching", function () {
756
- var runMiddleware = function (middleware, req) {
757
- var nextCalled = false;
758
- middleware(req, {}, function () {
759
- nextCalled = true;
760
- });
761
- return nextCalled;
762
- };
763
- // An unresolvable $ref makes ajv.compile throw, so getValidator catches the error
764
- // and returns null. Built via `unknown` to keep the non-standard keyword out of the
765
- // typed surface (and out of the explicit-any baseline).
766
- var uncompilableSchema = {
767
- broken: { $ref: "#/components/schemas/DoesNotExist" },
768
- };
769
- (0, bun_test_1.it)("maps an unrecognized mongoose type to string so the schema still compiles", function () {
770
- (0, openApiValidator_1.configureOpenApiValidator)();
771
- // "buffer" is neither a valid JSON Schema type nor present in MONGOOSE_TYPE_MAP,
772
- // so sanitizeSchemaForAjv falls through to the `type = "string"` branch.
773
- var middleware = (0, openApiValidator_1.validateRequestBody)({ raw: { type: "buffer" } });
774
- (0, bun_test_1.expect)(runMiddleware(middleware, { body: { raw: "some-string" }, method: "POST", path: "/t" })).toBe(true);
775
- });
776
- (0, bun_test_1.it)("reuses the cached validator on subsequent requests with the same schema", function () {
777
- (0, openApiValidator_1.configureOpenApiValidator)();
778
- var middleware = (0, openApiValidator_1.validateRequestBody)({ name: { type: "string" } });
779
- var req = { body: { name: "ok" }, method: "POST", path: "/t" };
780
- // First request compiles and caches the validator; the second must hit the cache.
781
- (0, bun_test_1.expect)(runMiddleware(middleware, req)).toBe(true);
782
- (0, bun_test_1.expect)(runMiddleware(middleware, req)).toBe(true);
783
- });
784
- (0, bun_test_1.it)("skips query validation when the schema cannot be compiled", function () {
785
- (0, openApiValidator_1.configureOpenApiValidator)();
786
- var middleware = (0, openApiValidator_1.validateQueryParams)(uncompilableSchema);
787
- (0, bun_test_1.expect)(runMiddleware(middleware, { method: "GET", path: "/t", query: { broken: "x" } })).toBe(true);
788
- });
789
- (0, bun_test_1.it)("skips response validation when the schema cannot be compiled", function () {
790
- (0, openApiValidator_1.configureOpenApiValidator)({ validateResponses: true });
791
- var result = (0, openApiValidator_1.validateResponseData)({ broken: "x" }, uncompilableSchema);
792
- (0, bun_test_1.expect)(result.valid).toBe(true);
793
- });
794
- });
795
- });