@terreno/api 57.4.0 → 57.6.0
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.
- package/README.md +5 -5
- package/bunfig.toml +1 -1
- package/dist/actions.js +197 -368
- package/dist/adminModelRouter.js +88 -173
- package/dist/api.js +598 -852
- package/dist/audit/auditApp.js +48 -89
- package/dist/audit/auditEventModel.js +15 -16
- package/dist/audit/cloudTasksEnqueue.js +56 -149
- package/dist/audit/diff.js +70 -160
- package/dist/audit/processQueue.js +25 -70
- package/dist/audit/rbacSink.js +25 -66
- package/dist/audit/record.js +129 -251
- package/dist/auth.js +432 -681
- package/dist/authRecovery.js +155 -269
- package/dist/authTokens.js +43 -118
- package/dist/betterAuthApp.js +25 -26
- package/dist/betterAuthSetup.js +258 -413
- package/dist/browserLogsRoute.d.ts +6 -0
- package/dist/browserLogsRoute.js +97 -0
- package/dist/collectionRegistry.js +32 -47
- package/dist/config.js +53 -122
- package/dist/configurationApp.js +180 -361
- package/dist/configurationPlugin.js +130 -302
- package/dist/consentApp.js +276 -421
- package/dist/docLoader.js +78 -126
- package/dist/envConfigurationPlugin.js +25 -147
- package/dist/errors.js +151 -247
- package/dist/example.js +28 -34
- package/dist/expressServer.d.ts +10 -0
- package/dist/expressServer.js +109 -255
- package/dist/frameworkModelNames.d.ts +24 -0
- package/dist/frameworkModelNames.js +31 -0
- package/dist/githubAuth.js +152 -216
- package/dist/httpClient.js +176 -264
- package/dist/index.d.ts +2 -0
- package/dist/index.js +7 -2
- package/dist/logger.js +145 -278
- package/dist/mcp/auth.js +76 -128
- package/dist/mcp/createMCPRequest.js +4 -5
- package/dist/mcp/handlers.js +268 -480
- package/dist/mcp/query.js +56 -150
- package/dist/mcp/registry.js +17 -30
- package/dist/mcp/schemaGenerator.js +95 -181
- package/dist/mcp/server.js +28 -100
- package/dist/mcp/serviceTokens.js +102 -191
- package/dist/mcp/toolGenerator.js +78 -191
- package/dist/middleware.js +15 -27
- package/dist/migrations/fixtures/all-down/20260910120000-one.js +2 -42
- package/dist/migrations/fixtures/all-down/20260910120001-two.js +2 -42
- package/dist/migrations/fixtures/bad-id/20260910120000-mismatch.js +1 -39
- package/dist/migrations/fixtures/bad-name/not-a-migration.js +1 -39
- package/dist/migrations/fixtures/valid/20260910120000-alpha.js +1 -39
- package/dist/migrations/fixtures/valid/20260910120001-beta.js +2 -42
- package/dist/migrations/generate.js +107 -229
- package/dist/migrations/load.js +75 -201
- package/dist/migrations/lock.js +54 -126
- package/dist/migrations/runner.js +192 -446
- package/dist/migrations/schemaCatalog.js +122 -263
- package/dist/models/consentForm.js +6 -6
- package/dist/models/consentResponse.js +5 -5
- package/dist/models/mcpServiceToken.js +66 -156
- package/dist/models/notification.js +6 -6
- package/dist/models/notificationPreference.js +6 -6
- package/dist/models/versionConfig.js +5 -5
- package/dist/notifications/notificationService.js +133 -273
- package/dist/notifications/notificationsApp.js +85 -179
- package/dist/notifications/notificationsBeforeSend.js +19 -64
- package/dist/notifiers/googleChatNotifier.js +36 -117
- package/dist/notifiers/slackNotifier.d.ts +31 -2
- package/dist/notifiers/slackNotifier.js +170 -119
- package/dist/notifiers/zoomNotifier.js +60 -111
- package/dist/openApi.js +107 -122
- package/dist/openApiBuilder.js +117 -159
- package/dist/openApiCompat.js +81 -148
- package/dist/openApiEtag.js +7 -7
- package/dist/openApiValidator.js +177 -286
- package/dist/orgs/orgContext.js +87 -157
- package/dist/orgs/orgPermissions.js +29 -68
- package/dist/orgs/orgPlugin.js +9 -27
- package/dist/orgs/organizationModel.js +65 -166
- package/dist/orgs/organizationSettings.js +29 -36
- package/dist/orgs/orgsApp.js +346 -608
- package/dist/permissions.js +88 -181
- package/dist/plugins.js +125 -218
- package/dist/populate.js +130 -240
- package/dist/rateLimit/applyTrustProxy.js +1 -1
- package/dist/rateLimit/createStore.js +7 -8
- package/dist/rateLimit/memoryStore.js +22 -100
- package/dist/rateLimit/middleware.js +48 -99
- package/dist/rateLimit/mongoStore.js +54 -112
- package/dist/rateLimit/policies.js +41 -75
- package/dist/rateLimit/redisStore.js +39 -133
- package/dist/rbac/access.js +119 -267
- package/dist/rbac/assertAllowed.js +95 -171
- package/dist/rbac/auditModel.js +14 -58
- package/dist/rbac/backfillAdmins.js +24 -130
- package/dist/rbac/fieldViews.js +46 -141
- package/dist/rbac/membershipOrgAdminSource.js +28 -99
- package/dist/rbac/middleware.js +30 -96
- package/dist/rbac/modelRouterAccess.js +130 -307
- package/dist/rbac/permissionUtils.js +40 -179
- package/dist/rbac/realtimeAccess.js +81 -147
- package/dist/rbac/resolve.d.ts +1 -1
- package/dist/rbac/resolve.js +148 -342
- package/dist/rbac/roleManager.js +318 -700
- package/dist/rbac/roleModel.js +44 -171
- package/dist/rbac/routes.js +93 -196
- package/dist/rbac/scopes.js +39 -133
- package/dist/rbac/statements.js +23 -91
- package/dist/rbac/userPlugin.js +3 -29
- package/dist/realtime/changeStreamWatcher.js +489 -800
- package/dist/realtime/deprecation.js +8 -10
- package/dist/realtime/queryMatcher.js +110 -191
- package/dist/realtime/queryStore.js +35 -118
- package/dist/realtime/realtimeApp.js +247 -401
- package/dist/realtime/registry.js +19 -30
- package/dist/realtime/sessionRevalidation.js +132 -358
- package/dist/realtime/socketAuth.js +89 -208
- package/dist/realtime/socketUser.js +10 -56
- package/dist/requestContext.js +93 -139
- package/dist/schemaMetadata.js +179 -274
- package/dist/scriptRunner.js +96 -266
- package/dist/scrubAdminFields.js +36 -108
- package/dist/secretProviders.js +130 -299
- package/dist/seedRunner.js +221 -514
- package/dist/sync/adminBroadcastScope.js +58 -170
- package/dist/sync/adminWindowAccess.js +15 -86
- package/dist/sync/adminWindowMutation.js +110 -181
- package/dist/sync/executors.js +425 -629
- package/dist/sync/loadHarness.js +392 -689
- package/dist/sync/models.js +181 -341
- package/dist/sync/mutationHandler.js +533 -801
- package/dist/sync/registrationSideEffects.js +41 -95
- package/dist/sync/registry.js +29 -49
- package/dist/sync/routes.js +484 -817
- package/dist/sync/scripts/compactTombstones.js +83 -228
- package/dist/sync/serialize.js +18 -58
- package/dist/sync/socketHandlers.js +253 -478
- package/dist/sync/streams.js +49 -117
- package/dist/sync/syncApp.js +12 -15
- package/dist/sync/syncBatch.js +63 -168
- package/dist/sync/syncSeqPlugin.js +430 -702
- package/dist/syncConsents.js +113 -253
- package/dist/terrenoApp.js +166 -276
- package/dist/tests/createTestData.js +146 -256
- package/dist/tests/models.js +14 -54
- package/dist/tests/mongoTestSetup.js +42 -103
- package/dist/tests/testHelper.js +31 -93
- package/dist/tests.js +12 -51
- package/dist/transformers.js +61 -164
- package/dist/utils.js +31 -93
- package/dist/vendor/wesleytodd-openapi/index.js +37 -51
- package/dist/vendor/wesleytodd-openapi/lib/generate-doc.js +32 -40
- package/dist/vendor/wesleytodd-openapi/lib/layer-schema.js +3 -3
- package/dist/vendor/wesleytodd-openapi/lib/ui.js +50 -19
- package/dist/vendor/wesleytodd-openapi/lib/validate.js +19 -47
- package/dist/versionCheckPlugin.js +52 -100
- package/dist/webhooks/headerValue.js +2 -2
- package/dist/webhooks/idempotency/memoryStore.js +16 -59
- package/dist/webhooks/idempotency/mongoStore.js +55 -130
- package/dist/webhooks/idempotency/webhookReceipt.js +9 -9
- package/dist/webhooks/rawBody.js +1 -1
- package/dist/webhooks/timingSafeEqual.js +4 -5
- package/dist/webhooks/verifiers/hmac.js +17 -18
- package/dist/webhooks/verifiers/sendgrid.js +20 -22
- package/dist/webhooks/verifiers/stripe.js +31 -65
- package/dist/webhooks/verifiers/twilio.js +23 -73
- package/dist/webhooks/webhooksApp.js +78 -177
- package/dist/zodOpenApi.js +2 -2
- package/package.json +6 -6
- package/src/__tests__/browserLogsRoute.test.ts +165 -0
- package/src/audit/auditApp.test.ts +5 -5
- package/src/audit/auditEventModel.ts +7 -3
- package/src/audit/auditRouter.test.ts +5 -5
- package/src/audit/rbacSink.test.ts +2 -2
- package/src/audit/record.test.ts +2 -2
- package/src/audit/record.ts +3 -2
- package/src/auth.ts +1 -1
- package/src/authRecovery.test.ts +43 -0
- package/src/authRecovery.ts +16 -10
- package/src/authTokens.ts +6 -2
- package/src/authVerification.test.ts +22 -0
- package/src/browserLogsRoute.ts +110 -0
- package/src/configurationPlugin.test.ts +1 -11
- package/src/expressServer.ts +10 -0
- package/src/frameworkModelNames.test.ts +62 -0
- package/src/frameworkModelNames.ts +33 -0
- package/src/index.ts +7 -0
- package/src/isolated/migrateCliConnect.isolated.ts +51 -0
- package/src/mcp/auth.test.ts +34 -1
- package/src/migrations/cli.test.ts +0 -23
- package/src/models/mcpServiceToken.ts +4 -3
- package/src/models/notification.ts +3 -2
- package/src/models/notificationPreference.ts +1 -1
- package/src/notifications/notificationsApp.test.ts +24 -1
- package/src/notifiers/slackNotifier.test.ts +186 -1
- package/src/notifiers/slackNotifier.ts +167 -8
- package/src/orgs/orgPlugin.ts +1 -1
- package/src/orgs/organizationModel.ts +11 -6
- package/src/plugins.test.ts +5 -0
- package/src/plugins.ts +3 -0
- package/src/realtime/realtimeApp.ts +1 -1
- package/src/sync/integration.test.ts +2 -2
- package/src/terrenoApp.ts +3 -0
- package/src/types.d.ts +1 -0
- package/src/webhooks/idempotency/webhookReceipt.ts +4 -3
- package/tsconfig.json +13 -54
- package/tsconfig.typedoc.json +2 -0
package/dist/openApiValidator.js
CHANGED
|
@@ -35,79 +35,21 @@
|
|
|
35
35
|
* });
|
|
36
36
|
* ```
|
|
37
37
|
*/
|
|
38
|
-
var __assign = (this && this.__assign) || function () {
|
|
39
|
-
__assign = Object.assign || function(t) {
|
|
40
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
41
|
-
s = arguments[i];
|
|
42
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
43
|
-
t[p] = s[p];
|
|
44
|
-
}
|
|
45
|
-
return t;
|
|
46
|
-
};
|
|
47
|
-
return __assign.apply(this, arguments);
|
|
48
|
-
};
|
|
49
|
-
var __rest = (this && this.__rest) || function (s, e) {
|
|
50
|
-
var t = {};
|
|
51
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
52
|
-
t[p] = s[p];
|
|
53
|
-
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
54
|
-
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
55
|
-
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
56
|
-
t[p[i]] = s[p[i]];
|
|
57
|
-
}
|
|
58
|
-
return t;
|
|
59
|
-
};
|
|
60
|
-
var __values = (this && this.__values) || function(o) {
|
|
61
|
-
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
|
|
62
|
-
if (m) return m.call(o);
|
|
63
|
-
if (o && typeof o.length === "number") return {
|
|
64
|
-
next: function () {
|
|
65
|
-
if (o && i >= o.length) o = void 0;
|
|
66
|
-
return { value: o && o[i++], done: !o };
|
|
67
|
-
}
|
|
68
|
-
};
|
|
69
|
-
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
|
|
70
|
-
};
|
|
71
|
-
var __read = (this && this.__read) || function (o, n) {
|
|
72
|
-
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
73
|
-
if (!m) return o;
|
|
74
|
-
var i = m.call(o), r, ar = [], e;
|
|
75
|
-
try {
|
|
76
|
-
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
77
|
-
}
|
|
78
|
-
catch (error) { e = { error: error }; }
|
|
79
|
-
finally {
|
|
80
|
-
try {
|
|
81
|
-
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
82
|
-
}
|
|
83
|
-
finally { if (e) throw e.error; }
|
|
84
|
-
}
|
|
85
|
-
return ar;
|
|
86
|
-
};
|
|
87
|
-
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
88
|
-
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
89
|
-
if (ar || !(i in from)) {
|
|
90
|
-
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
91
|
-
ar[i] = from[i];
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
return to.concat(ar || Array.prototype.slice.call(from));
|
|
95
|
-
};
|
|
96
38
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
97
39
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
98
40
|
};
|
|
99
41
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
100
42
|
exports.buildQuerySchemaFromFields = exports.buildNumericListQueryFieldSchema = exports.buildStringListQueryFieldSchema = exports.createModelValidators = exports.validateModelRequestBody = exports.getSchemaFromModel = exports.validateResponseData = exports.createValidator = exports.validateQueryParams = exports.validateRequestBody = exports.isEscapedRegexLiteral = exports.resetOpenApiValidatorConfig = exports.getOpenApiValidatorConfig = exports.configureOpenApiValidator = exports.isOpenApiValidatorConfigured = void 0;
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
43
|
+
const ajv_1 = __importDefault(require("ajv"));
|
|
44
|
+
const ajv_formats_1 = __importDefault(require("ajv-formats"));
|
|
45
|
+
const adminTypes_1 = require("./adminTypes");
|
|
46
|
+
const errors_1 = require("./errors");
|
|
47
|
+
const logger_1 = require("./logger");
|
|
48
|
+
const populate_1 = require("./populate");
|
|
107
49
|
// Whether configureOpenApiValidator() has been called
|
|
108
|
-
|
|
50
|
+
let isConfigured = false;
|
|
109
51
|
// Global validator configuration - can be modified at runtime
|
|
110
|
-
|
|
52
|
+
let globalConfig = {
|
|
111
53
|
coerceTypes: true,
|
|
112
54
|
logValidationErrors: true,
|
|
113
55
|
removeAdditional: true,
|
|
@@ -118,7 +60,7 @@ var globalConfig = {
|
|
|
118
60
|
* Check whether `configureOpenApiValidator()` has been called.
|
|
119
61
|
* Validation middleware is a no-op when this returns false.
|
|
120
62
|
*/
|
|
121
|
-
|
|
63
|
+
const isOpenApiValidatorConfigured = () => {
|
|
122
64
|
return isConfigured;
|
|
123
65
|
};
|
|
124
66
|
exports.isOpenApiValidatorConfigured = isOpenApiValidatorConfigured;
|
|
@@ -139,21 +81,20 @@ exports.isOpenApiValidatorConfigured = isOpenApiValidatorConfigured;
|
|
|
139
81
|
* });
|
|
140
82
|
* ```
|
|
141
83
|
*/
|
|
142
|
-
|
|
143
|
-
if (config === void 0) { config = {}; }
|
|
84
|
+
const configureOpenApiValidator = (config = {}) => {
|
|
144
85
|
isConfigured = true;
|
|
145
|
-
globalConfig =
|
|
86
|
+
globalConfig = { ...globalConfig, ...config };
|
|
146
87
|
// Clear cached AJV instances so new config takes effect
|
|
147
88
|
ajvCache.clear();
|
|
148
89
|
validatorCache.clear();
|
|
149
|
-
logger_1.logger.debug(
|
|
90
|
+
logger_1.logger.debug(`OpenAPI validator configured: ${JSON.stringify(globalConfig)}`);
|
|
150
91
|
};
|
|
151
92
|
exports.configureOpenApiValidator = configureOpenApiValidator;
|
|
152
93
|
/**
|
|
153
94
|
* Get the current global validator configuration.
|
|
154
95
|
*/
|
|
155
|
-
|
|
156
|
-
return
|
|
96
|
+
const getOpenApiValidatorConfig = () => {
|
|
97
|
+
return { ...globalConfig };
|
|
157
98
|
};
|
|
158
99
|
exports.getOpenApiValidatorConfig = getOpenApiValidatorConfig;
|
|
159
100
|
/**
|
|
@@ -161,7 +102,7 @@ exports.getOpenApiValidatorConfig = getOpenApiValidatorConfig;
|
|
|
161
102
|
* Also resets `isConfigured` to false.
|
|
162
103
|
* Useful for testing.
|
|
163
104
|
*/
|
|
164
|
-
|
|
105
|
+
const resetOpenApiValidatorConfig = () => {
|
|
165
106
|
isConfigured = false;
|
|
166
107
|
globalConfig = {
|
|
167
108
|
coerceTypes: true,
|
|
@@ -175,19 +116,18 @@ var resetOpenApiValidatorConfig = function () {
|
|
|
175
116
|
};
|
|
176
117
|
exports.resetOpenApiValidatorConfig = resetOpenApiValidatorConfig;
|
|
177
118
|
// Lazy AJV instance cache keyed by coerceTypes + removeAdditional
|
|
178
|
-
|
|
119
|
+
const ajvCache = new Map();
|
|
179
120
|
/**
|
|
180
121
|
* Get or create an AJV instance with the current config settings.
|
|
181
122
|
*/
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
var instance = ajvCache.get(key);
|
|
123
|
+
const getAjvInstance = () => {
|
|
124
|
+
const key = `coerce:${globalConfig.coerceTypes ?? true},remove:${globalConfig.removeAdditional ?? true}`;
|
|
125
|
+
let instance = ajvCache.get(key);
|
|
186
126
|
if (!instance) {
|
|
187
127
|
instance = new ajv_1.default({
|
|
188
128
|
allErrors: true,
|
|
189
|
-
coerceTypes:
|
|
190
|
-
removeAdditional:
|
|
129
|
+
coerceTypes: globalConfig.coerceTypes ?? true,
|
|
130
|
+
removeAdditional: globalConfig.removeAdditional ?? true,
|
|
191
131
|
strict: false,
|
|
192
132
|
useDefaults: true,
|
|
193
133
|
validateSchema: false,
|
|
@@ -196,21 +136,21 @@ var getAjvInstance = function () {
|
|
|
196
136
|
instance.addKeyword({
|
|
197
137
|
keyword: ESCAPED_REGEX_LITERAL_KEYWORD,
|
|
198
138
|
type: "string",
|
|
199
|
-
validate:
|
|
139
|
+
validate: (_enabled, data) => (0, exports.isEscapedRegexLiteral)(data),
|
|
200
140
|
});
|
|
201
141
|
ajvCache.set(key, instance);
|
|
202
142
|
}
|
|
203
143
|
return instance;
|
|
204
144
|
};
|
|
205
145
|
// Cache compiled validators by schema hash + config key
|
|
206
|
-
|
|
146
|
+
const validatorCache = new Map();
|
|
207
147
|
/**
|
|
208
148
|
* Generate a simple hash for a schema to use as a cache key.
|
|
209
149
|
*/
|
|
210
|
-
|
|
150
|
+
const hashSchema = (schema) => {
|
|
211
151
|
return JSON.stringify(schema);
|
|
212
152
|
};
|
|
213
|
-
|
|
153
|
+
const VALID_JSON_SCHEMA_TYPES = new Set([
|
|
214
154
|
"string",
|
|
215
155
|
"number",
|
|
216
156
|
"integer",
|
|
@@ -219,7 +159,7 @@ var VALID_JSON_SCHEMA_TYPES = new Set([
|
|
|
219
159
|
"object",
|
|
220
160
|
"null",
|
|
221
161
|
]);
|
|
222
|
-
|
|
162
|
+
const REGEX_META_CHARACTERS = new Set([
|
|
223
163
|
"\\",
|
|
224
164
|
".",
|
|
225
165
|
"*",
|
|
@@ -236,11 +176,11 @@ var REGEX_META_CHARACTERS = new Set([
|
|
|
236
176
|
"]",
|
|
237
177
|
]);
|
|
238
178
|
/** Accepts literal text and escaped metacharacters (e.g. `\\.`), rejects executable patterns. */
|
|
239
|
-
|
|
240
|
-
for (
|
|
241
|
-
|
|
179
|
+
const isEscapedRegexLiteral = (value) => {
|
|
180
|
+
for (let index = 0; index < value.length; index += 1) {
|
|
181
|
+
const character = value[index];
|
|
242
182
|
if (character === "\\") {
|
|
243
|
-
|
|
183
|
+
const escapedCharacter = value[index + 1];
|
|
244
184
|
if (!escapedCharacter || !REGEX_META_CHARACTERS.has(escapedCharacter)) {
|
|
245
185
|
return false;
|
|
246
186
|
}
|
|
@@ -254,9 +194,9 @@ var isEscapedRegexLiteral = function (value) {
|
|
|
254
194
|
return true;
|
|
255
195
|
};
|
|
256
196
|
exports.isEscapedRegexLiteral = isEscapedRegexLiteral;
|
|
257
|
-
|
|
197
|
+
const ESCAPED_REGEX_LITERAL_KEYWORD = "escapedRegexLiteral";
|
|
258
198
|
// mongoose-to-swagger emits non-standard type strings for some Mongoose types
|
|
259
|
-
|
|
199
|
+
const MONGOOSE_TYPE_MAP = {
|
|
260
200
|
dateonly: { format: "date", type: "string" },
|
|
261
201
|
schemaobjectid: { type: "string" },
|
|
262
202
|
};
|
|
@@ -264,14 +204,13 @@ var MONGOOSE_TYPE_MAP = {
|
|
|
264
204
|
* Recursively replace non-standard mongoose-to-swagger types with valid JSON Schema types
|
|
265
205
|
* so AJV can compile the schema.
|
|
266
206
|
*/
|
|
267
|
-
|
|
268
|
-
var e_1, _a;
|
|
207
|
+
const sanitizeSchemaForAjv = (schema) => {
|
|
269
208
|
if (!schema || typeof schema !== "object") {
|
|
270
209
|
return schema;
|
|
271
210
|
}
|
|
272
|
-
|
|
211
|
+
const result = { ...schema };
|
|
273
212
|
if (typeof result.type === "string" && !VALID_JSON_SCHEMA_TYPES.has(result.type)) {
|
|
274
|
-
|
|
213
|
+
const mapped = MONGOOSE_TYPE_MAP[result.type];
|
|
275
214
|
if (mapped) {
|
|
276
215
|
result.type = mapped.type;
|
|
277
216
|
if (mapped.format && !result.format) {
|
|
@@ -286,22 +225,12 @@ var sanitizeSchemaForAjv = function (schema) {
|
|
|
286
225
|
result.items = sanitizeSchemaForAjv(result.items);
|
|
287
226
|
}
|
|
288
227
|
if (result.properties && typeof result.properties === "object") {
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
? sanitizeSchemaForAjv(value)
|
|
296
|
-
: value;
|
|
297
|
-
}
|
|
298
|
-
}
|
|
299
|
-
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
300
|
-
finally {
|
|
301
|
-
try {
|
|
302
|
-
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
|
303
|
-
}
|
|
304
|
-
finally { if (e_1) throw e_1.error; }
|
|
228
|
+
const sanitizedProps = {};
|
|
229
|
+
for (const [key, value] of Object.entries(result.properties)) {
|
|
230
|
+
sanitizedProps[key] =
|
|
231
|
+
typeof value === "object" && value !== null
|
|
232
|
+
? sanitizeSchemaForAjv(value)
|
|
233
|
+
: value;
|
|
305
234
|
}
|
|
306
235
|
result.properties = sanitizedProps;
|
|
307
236
|
}
|
|
@@ -309,52 +238,47 @@ var sanitizeSchemaForAjv = function (schema) {
|
|
|
309
238
|
result.additionalProperties = sanitizeSchemaForAjv(result.additionalProperties);
|
|
310
239
|
}
|
|
311
240
|
if (Array.isArray(result.oneOf)) {
|
|
312
|
-
result.oneOf = result.oneOf.map(
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
: entry;
|
|
316
|
-
});
|
|
241
|
+
result.oneOf = result.oneOf.map((entry) => typeof entry === "object" && entry !== null
|
|
242
|
+
? sanitizeSchemaForAjv(entry)
|
|
243
|
+
: entry);
|
|
317
244
|
}
|
|
318
245
|
if (typeof result.required === "boolean") {
|
|
319
246
|
delete result.required;
|
|
320
247
|
}
|
|
321
248
|
return result;
|
|
322
249
|
};
|
|
323
|
-
|
|
250
|
+
const stripBooleanRequiredFlag = (schema) => {
|
|
324
251
|
if (schema.required === true || schema.required === false) {
|
|
325
|
-
|
|
326
|
-
|
|
252
|
+
const { required: _, ...rest } = schema;
|
|
253
|
+
const cleaned = { ...rest };
|
|
327
254
|
if (cleaned.properties) {
|
|
328
|
-
cleaned.properties = Object.fromEntries(Object.entries(cleaned.properties).map(
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
stripBooleanRequiredFlag(value),
|
|
333
|
-
];
|
|
334
|
-
}));
|
|
255
|
+
cleaned.properties = Object.fromEntries(Object.entries(cleaned.properties).map(([key, value]) => [
|
|
256
|
+
key,
|
|
257
|
+
stripBooleanRequiredFlag(value),
|
|
258
|
+
]));
|
|
335
259
|
}
|
|
336
260
|
if (cleaned.items) {
|
|
337
261
|
cleaned.items = stripBooleanRequiredFlag(cleaned.items);
|
|
338
262
|
}
|
|
339
263
|
if (cleaned.oneOf) {
|
|
340
|
-
cleaned.oneOf = cleaned.oneOf.map(
|
|
264
|
+
cleaned.oneOf = cleaned.oneOf.map((entry) => stripBooleanRequiredFlag(entry));
|
|
341
265
|
}
|
|
342
266
|
return cleaned;
|
|
343
267
|
}
|
|
344
268
|
if (schema.properties) {
|
|
345
|
-
return
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
269
|
+
return {
|
|
270
|
+
...schema,
|
|
271
|
+
properties: Object.fromEntries(Object.entries(schema.properties).map(([key, value]) => [
|
|
272
|
+
key,
|
|
273
|
+
stripBooleanRequiredFlag(value),
|
|
274
|
+
])),
|
|
275
|
+
};
|
|
352
276
|
}
|
|
353
277
|
if (schema.items) {
|
|
354
|
-
return
|
|
278
|
+
return { ...schema, items: stripBooleanRequiredFlag(schema.items) };
|
|
355
279
|
}
|
|
356
280
|
if (schema.oneOf) {
|
|
357
|
-
return
|
|
281
|
+
return { ...schema, oneOf: schema.oneOf.map((entry) => stripBooleanRequiredFlag(entry)) };
|
|
358
282
|
}
|
|
359
283
|
return schema;
|
|
360
284
|
};
|
|
@@ -364,23 +288,22 @@ var stripBooleanRequiredFlag = function (schema) {
|
|
|
364
288
|
* Sanitizes non-standard mongoose-to-swagger types before compilation.
|
|
365
289
|
* Returns null if the schema still cannot be compiled after sanitization.
|
|
366
290
|
*/
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
var cached = validatorCache.get(hash);
|
|
291
|
+
const getValidator = (schema) => {
|
|
292
|
+
const ajv = getAjvInstance();
|
|
293
|
+
const configKey = `coerce:${globalConfig.coerceTypes ?? true},remove:${globalConfig.removeAdditional ?? true}`;
|
|
294
|
+
const hash = `${configKey}:${hashSchema(schema)}`;
|
|
295
|
+
const cached = validatorCache.get(hash);
|
|
373
296
|
if (cached !== undefined) {
|
|
374
297
|
return cached;
|
|
375
298
|
}
|
|
376
|
-
|
|
299
|
+
const sanitized = sanitizeSchemaForAjv(schema);
|
|
377
300
|
try {
|
|
378
|
-
|
|
301
|
+
const validator = ajv.compile(sanitized);
|
|
379
302
|
validatorCache.set(hash, validator);
|
|
380
303
|
return validator;
|
|
381
304
|
}
|
|
382
305
|
catch (err) {
|
|
383
|
-
logger_1.logger.debug(
|
|
306
|
+
logger_1.logger.debug(`Could not compile validation schema after sanitization: ${err.message}`);
|
|
384
307
|
validatorCache.set(hash, null);
|
|
385
308
|
return null;
|
|
386
309
|
}
|
|
@@ -388,12 +311,12 @@ var getValidator = function (schema) {
|
|
|
388
311
|
/**
|
|
389
312
|
* Format AJV errors into a human-readable string.
|
|
390
313
|
*/
|
|
391
|
-
|
|
314
|
+
const formatValidationErrors = (errors) => {
|
|
392
315
|
return errors
|
|
393
|
-
.map(
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
return
|
|
316
|
+
.map((err) => {
|
|
317
|
+
const path = err.instancePath || "/";
|
|
318
|
+
const message = err.message || "validation failed";
|
|
319
|
+
return `${path}: ${message}`;
|
|
397
320
|
})
|
|
398
321
|
.join("; ");
|
|
399
322
|
};
|
|
@@ -402,40 +325,23 @@ var formatValidationErrors = function (errors) {
|
|
|
402
325
|
* Strips `required` from individual properties (OpenAPI-style) and moves it
|
|
403
326
|
* to the schema-level `required` array (JSON Schema-style) for AJV compatibility.
|
|
404
327
|
*/
|
|
405
|
-
|
|
406
|
-
var e_2, _a;
|
|
328
|
+
const propertiesToSchema = (properties, requiredFields, options) => {
|
|
407
329
|
// Extract required fields from properties marked required: true (OpenAPI builder style).
|
|
408
330
|
// PATCH bodies are partial: omit mongoose/OpenAPI required so one field can be sent.
|
|
409
|
-
|
|
331
|
+
const autoRequired = options?.partial
|
|
410
332
|
? []
|
|
411
333
|
: Object.entries(properties)
|
|
412
|
-
.filter(
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
})
|
|
416
|
-
.map(function (_a) {
|
|
417
|
-
var _b = __read(_a, 1), key = _b[0];
|
|
418
|
-
return key;
|
|
419
|
-
});
|
|
420
|
-
var allRequired = (options === null || options === void 0 ? void 0 : options.partial)
|
|
334
|
+
.filter(([_, prop]) => prop.required === true)
|
|
335
|
+
.map(([key]) => key);
|
|
336
|
+
const allRequired = options?.partial
|
|
421
337
|
? []
|
|
422
|
-
:
|
|
338
|
+
: [...new Set([...(requiredFields ?? []), ...autoRequired])];
|
|
423
339
|
// Strip boolean `required` from individual properties — AJV only accepts `required` arrays on objects
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
var _d = __read(_c.value, 2), key = _d[0], prop = _d[1];
|
|
428
|
-
cleanedProperties[key] = stripBooleanRequiredFlag(prop);
|
|
429
|
-
}
|
|
340
|
+
const cleanedProperties = {};
|
|
341
|
+
for (const [key, prop] of Object.entries(properties)) {
|
|
342
|
+
cleanedProperties[key] = stripBooleanRequiredFlag(prop);
|
|
430
343
|
}
|
|
431
|
-
|
|
432
|
-
finally {
|
|
433
|
-
try {
|
|
434
|
-
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
|
435
|
-
}
|
|
436
|
-
finally { if (e_2) throw e_2.error; }
|
|
437
|
-
}
|
|
438
|
-
var schema = {
|
|
344
|
+
const schema = {
|
|
439
345
|
properties: cleanedProperties,
|
|
440
346
|
required: allRequired.length > 0 ? allRequired : undefined,
|
|
441
347
|
type: "object",
|
|
@@ -457,42 +363,41 @@ var propertiesToSchema = function (properties, requiredFields, options) {
|
|
|
457
363
|
* @param options - Optional configuration for this validator
|
|
458
364
|
* @returns Express middleware function
|
|
459
365
|
*/
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
partial: options
|
|
366
|
+
const validateRequestBody = (schema, options) => {
|
|
367
|
+
const fullSchema = propertiesToSchema(schema, options?.required, {
|
|
368
|
+
partial: options?.partial,
|
|
463
369
|
});
|
|
464
|
-
return
|
|
465
|
-
var _a, _b, _c;
|
|
370
|
+
return (req, _res, next) => {
|
|
466
371
|
// No-op if not configured
|
|
467
372
|
if (!isConfigured) {
|
|
468
373
|
next();
|
|
469
374
|
return;
|
|
470
375
|
}
|
|
471
376
|
// Check if validation is enabled (route override takes precedence)
|
|
472
|
-
|
|
377
|
+
const isEnabled = options?.enabled ?? globalConfig.validateRequests;
|
|
473
378
|
if (!isEnabled) {
|
|
474
379
|
next();
|
|
475
380
|
return;
|
|
476
381
|
}
|
|
477
382
|
// Capture keys before validation for removeAdditional detection
|
|
478
|
-
|
|
383
|
+
const keysBefore = req.body && typeof req.body === "object" ? Object.keys(req.body) : [];
|
|
479
384
|
// Get validator at request time so config changes take effect
|
|
480
|
-
|
|
385
|
+
const validator = getValidator(fullSchema);
|
|
481
386
|
// If schema couldn't be compiled (e.g., non-standard types), skip validation
|
|
482
387
|
if (!validator) {
|
|
483
388
|
next();
|
|
484
389
|
return;
|
|
485
390
|
}
|
|
486
391
|
// Clone body if we might modify it (coercion or removeAdditional)
|
|
487
|
-
|
|
488
|
-
|
|
392
|
+
const bodyToValidate = { ...req.body };
|
|
393
|
+
const valid = validator(bodyToValidate);
|
|
489
394
|
if (!valid && validator.errors) {
|
|
490
|
-
|
|
395
|
+
const errors = validator.errors;
|
|
491
396
|
if (globalConfig.logValidationErrors) {
|
|
492
|
-
logger_1.logger.warn(
|
|
397
|
+
logger_1.logger.warn(`Request body validation failed for ${req.method} ${req.path}: ${formatValidationErrors(errors)}`);
|
|
493
398
|
}
|
|
494
399
|
// Use custom error handler if provided
|
|
495
|
-
|
|
400
|
+
const errorHandler = options?.onError ?? globalConfig.onValidationError;
|
|
496
401
|
if (errorHandler) {
|
|
497
402
|
errorHandler(errors, req);
|
|
498
403
|
next();
|
|
@@ -503,11 +408,11 @@ var validateRequestBody = function (schema, options) {
|
|
|
503
408
|
detail: formatValidationErrors(errors),
|
|
504
409
|
disableExternalErrorTracking: true,
|
|
505
410
|
meta: {
|
|
506
|
-
validationErrors: JSON.stringify(errors.map(
|
|
411
|
+
validationErrors: JSON.stringify(errors.map((e) => ({
|
|
507
412
|
message: e.message,
|
|
508
413
|
params: e.params,
|
|
509
414
|
path: e.instancePath,
|
|
510
|
-
})
|
|
415
|
+
}))),
|
|
511
416
|
},
|
|
512
417
|
source: {
|
|
513
418
|
pointer: "/body",
|
|
@@ -520,15 +425,15 @@ var validateRequestBody = function (schema, options) {
|
|
|
520
425
|
if (valid) {
|
|
521
426
|
// Detect removed properties (top-level only)
|
|
522
427
|
if (globalConfig.removeAdditional) {
|
|
523
|
-
|
|
524
|
-
|
|
428
|
+
const keysAfter = Object.keys(bodyToValidate);
|
|
429
|
+
const removedProperties = keysBefore.filter((k) => !keysAfter.includes(k));
|
|
525
430
|
if (removedProperties.length > 0) {
|
|
526
|
-
|
|
431
|
+
const hook = options?.onAdditionalPropertiesRemoved ?? globalConfig.onAdditionalPropertiesRemoved;
|
|
527
432
|
if (hook) {
|
|
528
433
|
hook(removedProperties, req);
|
|
529
434
|
}
|
|
530
435
|
if (globalConfig.logValidationErrors) {
|
|
531
|
-
logger_1.logger.debug(
|
|
436
|
+
logger_1.logger.debug(`Stripped additional properties from ${req.method} ${req.path}: ${removedProperties.join(", ")}`);
|
|
532
437
|
}
|
|
533
438
|
}
|
|
534
439
|
}
|
|
@@ -545,35 +450,34 @@ exports.validateRequestBody = validateRequestBody;
|
|
|
545
450
|
* @param options - Optional configuration for this validator
|
|
546
451
|
* @returns Express middleware function
|
|
547
452
|
*/
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
return
|
|
551
|
-
var _a, _b, _c, _d;
|
|
453
|
+
const validateQueryParams = (schema, options) => {
|
|
454
|
+
const fullSchema = propertiesToSchema(schema);
|
|
455
|
+
return (req, _res, next) => {
|
|
552
456
|
// No-op if not configured
|
|
553
457
|
if (!isConfigured) {
|
|
554
458
|
next();
|
|
555
459
|
return;
|
|
556
460
|
}
|
|
557
|
-
|
|
461
|
+
const isEnabled = options?.enabled ?? globalConfig.validateRequests;
|
|
558
462
|
if (!isEnabled) {
|
|
559
463
|
next();
|
|
560
464
|
return;
|
|
561
465
|
}
|
|
562
466
|
// Get validator at request time
|
|
563
|
-
|
|
467
|
+
const validator = getValidator(fullSchema);
|
|
564
468
|
// If schema couldn't be compiled, skip validation
|
|
565
469
|
if (!validator) {
|
|
566
470
|
next();
|
|
567
471
|
return;
|
|
568
472
|
}
|
|
569
|
-
|
|
570
|
-
|
|
473
|
+
const queryToValidate = globalConfig.coerceTypes ? { ...req.query } : req.query;
|
|
474
|
+
const valid = validator(queryToValidate);
|
|
571
475
|
if (!valid && validator.errors) {
|
|
572
|
-
|
|
476
|
+
const errors = validator.errors;
|
|
573
477
|
if (globalConfig.logValidationErrors) {
|
|
574
|
-
logger_1.logger.warn(
|
|
478
|
+
logger_1.logger.warn(`Query parameter validation failed for ${req.method} ${req.path}: ${formatValidationErrors(errors)}`);
|
|
575
479
|
}
|
|
576
|
-
|
|
480
|
+
const errorHandler = options?.onError ?? globalConfig.onValidationError;
|
|
577
481
|
if (errorHandler) {
|
|
578
482
|
errorHandler(errors, req);
|
|
579
483
|
next();
|
|
@@ -583,14 +487,14 @@ var validateQueryParams = function (schema, options) {
|
|
|
583
487
|
detail: formatValidationErrors(errors),
|
|
584
488
|
disableExternalErrorTracking: true,
|
|
585
489
|
meta: {
|
|
586
|
-
validationErrors: JSON.stringify(errors.map(
|
|
490
|
+
validationErrors: JSON.stringify(errors.map((e) => ({
|
|
587
491
|
message: e.message,
|
|
588
492
|
params: e.params,
|
|
589
493
|
path: e.instancePath,
|
|
590
|
-
})
|
|
494
|
+
}))),
|
|
591
495
|
},
|
|
592
496
|
source: {
|
|
593
|
-
parameter:
|
|
497
|
+
parameter: errors[0]?.instancePath?.replace("/", "") || "unknown",
|
|
594
498
|
},
|
|
595
499
|
status: 400,
|
|
596
500
|
title: "Query parameter validation failed",
|
|
@@ -622,17 +526,17 @@ exports.validateQueryParams = validateQueryParams;
|
|
|
622
526
|
* ], handler);
|
|
623
527
|
* ```
|
|
624
528
|
*/
|
|
625
|
-
|
|
626
|
-
|
|
529
|
+
const createValidator = (options) => {
|
|
530
|
+
const bodyValidator = options.body
|
|
627
531
|
? (0, exports.validateRequestBody)(options.body, { enabled: options.enabled })
|
|
628
532
|
: null;
|
|
629
|
-
|
|
533
|
+
const queryValidator = options.query
|
|
630
534
|
? (0, exports.validateQueryParams)(options.query, { enabled: options.enabled })
|
|
631
535
|
: null;
|
|
632
|
-
return
|
|
536
|
+
return (req, res, next) => {
|
|
633
537
|
// Run body validation first
|
|
634
538
|
if (bodyValidator) {
|
|
635
|
-
bodyValidator(req, res, (
|
|
539
|
+
bodyValidator(req, res, ((err) => {
|
|
636
540
|
if (err) {
|
|
637
541
|
next(err);
|
|
638
542
|
return;
|
|
@@ -663,19 +567,19 @@ exports.createValidator = createValidator;
|
|
|
663
567
|
* @param schema - The expected schema
|
|
664
568
|
* @returns Object with valid flag and any errors
|
|
665
569
|
*/
|
|
666
|
-
|
|
570
|
+
const validateResponseData = (data, schema) => {
|
|
667
571
|
if (!globalConfig.validateResponses) {
|
|
668
572
|
return { valid: true };
|
|
669
573
|
}
|
|
670
|
-
|
|
671
|
-
|
|
574
|
+
const fullSchema = propertiesToSchema(schema);
|
|
575
|
+
const validator = getValidator(fullSchema);
|
|
672
576
|
if (!validator) {
|
|
673
577
|
return { valid: true };
|
|
674
578
|
}
|
|
675
|
-
|
|
579
|
+
const valid = validator(data);
|
|
676
580
|
if (!valid && validator.errors) {
|
|
677
581
|
if (globalConfig.logValidationErrors) {
|
|
678
|
-
logger_1.logger.warn(
|
|
582
|
+
logger_1.logger.warn(`Response validation failed: ${formatValidationErrors(validator.errors)}`);
|
|
679
583
|
}
|
|
680
584
|
return { errors: validator.errors, valid: false };
|
|
681
585
|
}
|
|
@@ -690,16 +594,16 @@ exports.validateResponseData = validateResponseData;
|
|
|
690
594
|
* @param model - A Mongoose model
|
|
691
595
|
* @returns Schema properties suitable for validation
|
|
692
596
|
*/
|
|
693
|
-
|
|
694
|
-
|
|
597
|
+
const getSchemaFromModel = (model) => {
|
|
598
|
+
const { properties } = (0, populate_1.getOpenApiSpecForModel)(model);
|
|
695
599
|
return properties;
|
|
696
600
|
};
|
|
697
601
|
exports.getSchemaFromModel = getSchemaFromModel;
|
|
698
602
|
/**
|
|
699
603
|
* Extract required field names from a Mongoose model's swagger schema.
|
|
700
604
|
*/
|
|
701
|
-
|
|
702
|
-
|
|
605
|
+
const getRequiredFieldsFromModel = (model) => {
|
|
606
|
+
const { required } = (0, populate_1.getOpenApiSpecForModel)(model);
|
|
703
607
|
return required;
|
|
704
608
|
};
|
|
705
609
|
/**
|
|
@@ -710,19 +614,18 @@ var getRequiredFieldsFromModel = function (model) {
|
|
|
710
614
|
* @param options - Optional configuration for the validator
|
|
711
615
|
* @returns Express middleware function
|
|
712
616
|
*/
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
var _b = __read(_a, 1), key = _b[0];
|
|
721
|
-
return !excluded_1.has(key);
|
|
722
|
-
}));
|
|
723
|
-
requiredFields = requiredFields.filter(function (f) { return !excluded_1.has(f); });
|
|
617
|
+
const validateModelRequestBody = (model, options) => {
|
|
618
|
+
let schema = (0, exports.getSchemaFromModel)(model);
|
|
619
|
+
let requiredFields = options?.partial ? [] : getRequiredFieldsFromModel(model);
|
|
620
|
+
if (options?.excludeFields?.length) {
|
|
621
|
+
const excluded = new Set(options.excludeFields);
|
|
622
|
+
schema = Object.fromEntries(Object.entries(schema).filter(([key]) => !excluded.has(key)));
|
|
623
|
+
requiredFields = requiredFields.filter((f) => !excluded.has(f));
|
|
724
624
|
}
|
|
725
|
-
return (0, exports.validateRequestBody)(schema,
|
|
625
|
+
return (0, exports.validateRequestBody)(schema, {
|
|
626
|
+
...options,
|
|
627
|
+
required: options?.partial ? [] : [...(options?.required ?? []), ...requiredFields],
|
|
628
|
+
});
|
|
726
629
|
};
|
|
727
630
|
exports.validateModelRequestBody = validateModelRequestBody;
|
|
728
631
|
/**
|
|
@@ -733,24 +636,24 @@ exports.validateModelRequestBody = validateModelRequestBody;
|
|
|
733
636
|
* @param options - Configuration options
|
|
734
637
|
* @returns Object with create and update validation middleware
|
|
735
638
|
*/
|
|
736
|
-
|
|
737
|
-
|
|
639
|
+
const createModelValidators = (model, options) => {
|
|
640
|
+
const schema = (0, exports.getSchemaFromModel)(model);
|
|
738
641
|
return {
|
|
739
642
|
create: (0, exports.validateRequestBody)(schema, {
|
|
740
|
-
enabled: options
|
|
741
|
-
onAdditionalPropertiesRemoved: options
|
|
742
|
-
onError: options
|
|
643
|
+
enabled: options?.validateCreate,
|
|
644
|
+
onAdditionalPropertiesRemoved: options?.onAdditionalPropertiesRemoved,
|
|
645
|
+
onError: options?.onError,
|
|
743
646
|
}),
|
|
744
647
|
update: (0, exports.validateRequestBody)(schema, {
|
|
745
|
-
enabled: options
|
|
746
|
-
onAdditionalPropertiesRemoved: options
|
|
747
|
-
onError: options
|
|
648
|
+
enabled: options?.validateUpdate,
|
|
649
|
+
onAdditionalPropertiesRemoved: options?.onAdditionalPropertiesRemoved,
|
|
650
|
+
onError: options?.onError,
|
|
748
651
|
partial: true,
|
|
749
652
|
}),
|
|
750
653
|
};
|
|
751
654
|
};
|
|
752
655
|
exports.createModelValidators = createModelValidators;
|
|
753
|
-
|
|
656
|
+
const choiceEmptyInItemSchema = (baseItem) => {
|
|
754
657
|
if (baseItem.enum) {
|
|
755
658
|
return {
|
|
756
659
|
oneOf: [
|
|
@@ -766,7 +669,7 @@ var choiceEmptyInItemSchema = function (baseItem) {
|
|
|
766
669
|
],
|
|
767
670
|
};
|
|
768
671
|
};
|
|
769
|
-
|
|
672
|
+
const buildInOperatorSchema = (itemSchema) => ({
|
|
770
673
|
additionalProperties: false,
|
|
771
674
|
properties: {
|
|
772
675
|
$in: {
|
|
@@ -777,8 +680,8 @@ var buildInOperatorSchema = function (itemSchema) { return ({
|
|
|
777
680
|
},
|
|
778
681
|
required: ["$in"],
|
|
779
682
|
type: "object",
|
|
780
|
-
});
|
|
781
|
-
|
|
683
|
+
});
|
|
684
|
+
const buildRegexOperatorSchema = () => ({
|
|
782
685
|
additionalProperties: false,
|
|
783
686
|
properties: {
|
|
784
687
|
$options: { enum: ["i"], type: "string" },
|
|
@@ -786,8 +689,8 @@ var buildRegexOperatorSchema = function () { return ({
|
|
|
786
689
|
},
|
|
787
690
|
required: ["$regex", "$options"],
|
|
788
691
|
type: "object",
|
|
789
|
-
});
|
|
790
|
-
|
|
692
|
+
});
|
|
693
|
+
const buildRangeOperatorSchema = (valueSchema) => ({
|
|
791
694
|
additionalProperties: false,
|
|
792
695
|
properties: {
|
|
793
696
|
$gt: valueSchema,
|
|
@@ -796,11 +699,11 @@ var buildRangeOperatorSchema = function (valueSchema) { return ({
|
|
|
796
699
|
$lte: valueSchema,
|
|
797
700
|
},
|
|
798
701
|
type: "object",
|
|
799
|
-
});
|
|
702
|
+
});
|
|
800
703
|
/** Query schema for string/enum fields: scalar, `$in`, or escaped `$regex`. */
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
704
|
+
const buildStringListQueryFieldSchema = (fieldSchema) => {
|
|
705
|
+
const scalar = stripBooleanRequiredFlag(fieldSchema);
|
|
706
|
+
const inItems = fieldSchema.enum
|
|
804
707
|
? choiceEmptyInItemSchema(fieldSchema)
|
|
805
708
|
: stripBooleanRequiredFlag(fieldSchema);
|
|
806
709
|
return {
|
|
@@ -809,9 +712,9 @@ var buildStringListQueryFieldSchema = function (fieldSchema) {
|
|
|
809
712
|
};
|
|
810
713
|
exports.buildStringListQueryFieldSchema = buildStringListQueryFieldSchema;
|
|
811
714
|
/** Query schema for number/date fields: scalar or comparison operators. */
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
715
|
+
const buildNumericListQueryFieldSchema = (fieldSchema) => {
|
|
716
|
+
const scalar = stripBooleanRequiredFlag(fieldSchema);
|
|
717
|
+
const valueSchema = stripBooleanRequiredFlag(fieldSchema);
|
|
815
718
|
return {
|
|
816
719
|
oneOf: [scalar, buildRangeOperatorSchema(valueSchema)],
|
|
817
720
|
};
|
|
@@ -825,40 +728,28 @@ exports.buildNumericListQueryFieldSchema = buildNumericListQueryFieldSchema;
|
|
|
825
728
|
* @param queryFields - Array of field names allowed for querying
|
|
826
729
|
* @returns Schema properties suitable for query validation
|
|
827
730
|
*/
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
var modelSchema = (0, exports.getSchemaFromModel)(model);
|
|
832
|
-
var querySchema = {
|
|
731
|
+
const buildQuerySchemaFromFields = (model, queryFields = []) => {
|
|
732
|
+
const modelSchema = (0, exports.getSchemaFromModel)(model);
|
|
733
|
+
const querySchema = {
|
|
833
734
|
limit: { type: "number" },
|
|
834
735
|
page: { type: "number" },
|
|
835
736
|
sort: { type: "string" },
|
|
836
737
|
};
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
querySchema[field] = (0, exports.buildStringListQueryFieldSchema)({ type: "string" });
|
|
843
|
-
continue;
|
|
844
|
-
}
|
|
845
|
-
if (modelField.type === "boolean") {
|
|
846
|
-
querySchema[field] = stripBooleanRequiredFlag(modelField);
|
|
847
|
-
continue;
|
|
848
|
-
}
|
|
849
|
-
if (modelField.type === "number" || modelField.format === "date-time") {
|
|
850
|
-
querySchema[field] = (0, exports.buildNumericListQueryFieldSchema)(modelField);
|
|
851
|
-
continue;
|
|
852
|
-
}
|
|
853
|
-
querySchema[field] = (0, exports.buildStringListQueryFieldSchema)(modelField);
|
|
738
|
+
for (const field of queryFields) {
|
|
739
|
+
const modelField = modelSchema[field];
|
|
740
|
+
if (!modelField) {
|
|
741
|
+
querySchema[field] = (0, exports.buildStringListQueryFieldSchema)({ type: "string" });
|
|
742
|
+
continue;
|
|
854
743
|
}
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
744
|
+
if (modelField.type === "boolean") {
|
|
745
|
+
querySchema[field] = stripBooleanRequiredFlag(modelField);
|
|
746
|
+
continue;
|
|
747
|
+
}
|
|
748
|
+
if (modelField.type === "number" || modelField.format === "date-time") {
|
|
749
|
+
querySchema[field] = (0, exports.buildNumericListQueryFieldSchema)(modelField);
|
|
750
|
+
continue;
|
|
860
751
|
}
|
|
861
|
-
|
|
752
|
+
querySchema[field] = (0, exports.buildStringListQueryFieldSchema)(modelField);
|
|
862
753
|
}
|
|
863
754
|
return querySchema;
|
|
864
755
|
};
|