@terreno/api 0.0.17 → 0.1.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/.claude/CLAUDE.local.md +204 -0
- package/.cursor/rules/00-root.mdc +338 -0
- package/.github/copilot-instructions.md +333 -0
- package/AGENTS.md +333 -0
- package/README.md +76 -7
- package/biome.jsonc +1 -1
- package/dist/api.d.ts +68 -1
- package/dist/api.js +140 -5
- package/dist/api.query.test.js +1 -1
- package/dist/api.test.js +222 -484
- package/dist/auth.js +3 -1
- package/dist/errors.js +15 -12
- package/dist/example.js +7 -7
- package/dist/expressServer.d.ts +8 -2
- package/dist/expressServer.js +8 -1
- package/dist/githubAuth.d.ts +64 -0
- package/dist/githubAuth.js +293 -0
- package/dist/githubAuth.test.d.ts +1 -0
- package/dist/githubAuth.test.js +351 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +3 -0
- package/dist/logger.js +1 -1
- package/dist/middleware.js +1 -1
- package/dist/notifiers/googleChatNotifier.js +1 -1
- package/dist/notifiers/googleChatNotifier.test.js +1 -1
- package/dist/notifiers/slackNotifier.js +1 -1
- package/dist/notifiers/slackNotifier.test.js +1 -1
- package/dist/notifiers/zoomNotifier.js +1 -1
- package/dist/notifiers/zoomNotifier.test.js +1 -1
- package/dist/openApi.test.js +8 -5
- package/dist/openApiBuilder.d.ts +69 -1
- package/dist/openApiBuilder.js +109 -5
- package/dist/openApiValidator.d.ts +296 -0
- package/dist/openApiValidator.js +698 -0
- package/dist/openApiValidator.test.d.ts +1 -0
- package/dist/openApiValidator.test.js +346 -0
- package/dist/permissions.js +1 -1
- package/dist/plugins.test.js +3 -3
- package/dist/terrenoPlugin.d.ts +4 -0
- package/dist/terrenoPlugin.js +2 -0
- package/dist/tests/bunSetup.js +2 -2
- package/dist/tests.js +34 -24
- package/package.json +7 -2
- package/src/__snapshots__/openApi.test.ts.snap +399 -0
- package/src/__snapshots__/openApiBuilder.test.ts.snap +108 -0
- package/src/api.query.test.ts +1 -1
- package/src/api.test.ts +161 -374
- package/src/api.ts +210 -4
- package/src/auth.ts +3 -1
- package/src/errors.ts +15 -12
- package/src/example.ts +7 -7
- package/src/expressServer.ts +18 -2
- package/src/githubAuth.test.ts +223 -0
- package/src/githubAuth.ts +335 -0
- package/src/index.ts +3 -0
- package/src/logger.ts +1 -1
- package/src/middleware.ts +1 -1
- package/src/notifiers/googleChatNotifier.test.ts +1 -1
- package/src/notifiers/googleChatNotifier.ts +1 -1
- package/src/notifiers/slackNotifier.test.ts +1 -1
- package/src/notifiers/slackNotifier.ts +1 -1
- package/src/notifiers/zoomNotifier.test.ts +1 -1
- package/src/notifiers/zoomNotifier.ts +1 -1
- package/src/openApi.test.ts +8 -5
- package/src/openApiBuilder.ts +188 -15
- package/src/openApiValidator.test.ts +241 -0
- package/src/openApiValidator.ts +860 -0
- package/src/permissions.ts +1 -1
- package/src/plugins.test.ts +3 -3
- package/src/terrenoPlugin.ts +5 -0
- package/src/tests/bunSetup.ts +2 -2
- package/src/tests.ts +34 -24
- package/CLAUDE.md +0 -107
- package/dist/response.d.ts +0 -0
- package/dist/response.js +0 -1
- package/index.ts +0 -1
- package/src/response.ts +0 -0
package/dist/openApiBuilder.js
CHANGED
|
@@ -66,6 +66,7 @@ exports.createOpenApiBuilder = createOpenApiBuilder;
|
|
|
66
66
|
var merge_1 = __importDefault(require("lodash/merge"));
|
|
67
67
|
var logger_1 = require("./logger");
|
|
68
68
|
var openApi_1 = require("./openApi");
|
|
69
|
+
var openApiValidator_1 = require("./openApiValidator");
|
|
69
70
|
/**
|
|
70
71
|
* A fluent builder for constructing OpenAPI middleware.
|
|
71
72
|
*
|
|
@@ -99,10 +100,13 @@ var OpenApiMiddlewareBuilder = /** @class */ (function () {
|
|
|
99
100
|
* @param options - Router options containing the OpenAPI path configuration
|
|
100
101
|
*/
|
|
101
102
|
function OpenApiMiddlewareBuilder(options) {
|
|
103
|
+
/** Store the raw query parameter schemas for validation */
|
|
104
|
+
this.queryParamSchemas = {};
|
|
102
105
|
this.options = options;
|
|
103
106
|
this.config = {
|
|
104
107
|
responses: {},
|
|
105
108
|
};
|
|
109
|
+
this.validationConfig = {};
|
|
106
110
|
}
|
|
107
111
|
/**
|
|
108
112
|
* Sets the tags for the OpenAPI operation.
|
|
@@ -201,6 +205,8 @@ var OpenApiMiddlewareBuilder = /** @class */ (function () {
|
|
|
201
205
|
_c),
|
|
202
206
|
required: (_e = options === null || options === void 0 ? void 0 : options.required) !== null && _e !== void 0 ? _e : true,
|
|
203
207
|
};
|
|
208
|
+
// Store the schema for validation
|
|
209
|
+
this.requestBodySchema = schema;
|
|
204
210
|
return this;
|
|
205
211
|
};
|
|
206
212
|
/**
|
|
@@ -324,6 +330,8 @@ var OpenApiMiddlewareBuilder = /** @class */ (function () {
|
|
|
324
330
|
required: (_c = options === null || options === void 0 ? void 0 : options.required) !== null && _c !== void 0 ? _c : false,
|
|
325
331
|
schema: schema,
|
|
326
332
|
});
|
|
333
|
+
// Store for validation
|
|
334
|
+
this.queryParamSchemas[name] = __assign(__assign({}, schema), { required: options === null || options === void 0 ? void 0 : options.required });
|
|
327
335
|
return this;
|
|
328
336
|
};
|
|
329
337
|
/**
|
|
@@ -358,6 +366,74 @@ var OpenApiMiddlewareBuilder = /** @class */ (function () {
|
|
|
358
366
|
});
|
|
359
367
|
return this;
|
|
360
368
|
};
|
|
369
|
+
/**
|
|
370
|
+
* Enables runtime validation for this route.
|
|
371
|
+
*
|
|
372
|
+
* When enabled, the built middleware will validate incoming requests
|
|
373
|
+
* against the documented schema before the handler runs.
|
|
374
|
+
*
|
|
375
|
+
* @param options - Optional configuration for validation
|
|
376
|
+
* @param options.body - Enable body validation (default: true if request body is defined)
|
|
377
|
+
* @param options.query - Enable query parameter validation (default: true if query params are defined)
|
|
378
|
+
* @param options.enabled - Override the global validation enabled setting
|
|
379
|
+
* @returns The builder instance for chaining
|
|
380
|
+
*
|
|
381
|
+
* @example
|
|
382
|
+
* ```typescript
|
|
383
|
+
* createOpenApiBuilder(options)
|
|
384
|
+
* .withRequestBody<{name: string}>({name: {type: "string", required: true}})
|
|
385
|
+
* .withValidation() // Enable validation
|
|
386
|
+
* .build();
|
|
387
|
+
* ```
|
|
388
|
+
*/
|
|
389
|
+
OpenApiMiddlewareBuilder.prototype.withValidation = function (options) {
|
|
390
|
+
var _c, _d, _e;
|
|
391
|
+
this.validationConfig = {
|
|
392
|
+
enabled: (_c = options === null || options === void 0 ? void 0 : options.enabled) !== null && _c !== void 0 ? _c : true,
|
|
393
|
+
validateBody: (_d = options === null || options === void 0 ? void 0 : options.body) !== null && _d !== void 0 ? _d : true,
|
|
394
|
+
validateQuery: (_e = options === null || options === void 0 ? void 0 : options.query) !== null && _e !== void 0 ? _e : true,
|
|
395
|
+
};
|
|
396
|
+
return this;
|
|
397
|
+
};
|
|
398
|
+
/**
|
|
399
|
+
* Builds and returns the OpenAPI middleware along with schemas.
|
|
400
|
+
*
|
|
401
|
+
* This method is useful when you want to use asyncHandler's integrated
|
|
402
|
+
* validation instead of separate validation middleware.
|
|
403
|
+
*
|
|
404
|
+
* @returns Object containing middleware and schemas
|
|
405
|
+
*
|
|
406
|
+
* @example
|
|
407
|
+
* ```typescript
|
|
408
|
+
* const {middleware, bodySchema} = createOpenApiBuilder(options)
|
|
409
|
+
* .withRequestBody<{name: string}>({name: {type: "string", required: true}})
|
|
410
|
+
* .buildWithSchemas();
|
|
411
|
+
*
|
|
412
|
+
* router.post("/users", middleware, asyncHandler(async (req, res) => {
|
|
413
|
+
* // handler code
|
|
414
|
+
* }, {bodySchema, validate: true}));
|
|
415
|
+
* ```
|
|
416
|
+
*/
|
|
417
|
+
OpenApiMiddlewareBuilder.prototype.buildWithSchemas = function () {
|
|
418
|
+
var _c, _d, _e, _f, _g;
|
|
419
|
+
var noop = function (_a, _b, next) { return next(); };
|
|
420
|
+
// Build the OpenAPI documentation middleware only (no validation middleware)
|
|
421
|
+
var openApiMiddleware = noop;
|
|
422
|
+
if ((_c = this.options.openApi) === null || _c === void 0 ? void 0 : _c.path) {
|
|
423
|
+
openApiMiddleware = this.options.openApi.path((0, merge_1.default)(__assign(__assign({}, this.config), { responses: __assign(__assign({}, this.config.responses), openApi_1.defaultOpenApiErrorResponses) }), (_e = (_d = this.options.openApiOverwrite) === null || _d === void 0 ? void 0 : _d.get) !== null && _e !== void 0 ? _e : {}));
|
|
424
|
+
}
|
|
425
|
+
else {
|
|
426
|
+
logger_1.logger.debug("No options.openApi provided, skipping OpenApiMiddleware");
|
|
427
|
+
}
|
|
428
|
+
var globalConfig = (0, openApiValidator_1.getOpenApiValidatorConfig)();
|
|
429
|
+
var validationEnabled = (_f = this.validationConfig.enabled) !== null && _f !== void 0 ? _f : ((0, openApiValidator_1.isOpenApiValidatorConfigured)() && ((_g = globalConfig.validateRequests) !== null && _g !== void 0 ? _g : false));
|
|
430
|
+
return {
|
|
431
|
+
bodySchema: this.requestBodySchema,
|
|
432
|
+
middleware: openApiMiddleware,
|
|
433
|
+
querySchema: Object.keys(this.queryParamSchemas).length > 0 ? this.queryParamSchemas : undefined,
|
|
434
|
+
validationEnabled: validationEnabled,
|
|
435
|
+
};
|
|
436
|
+
};
|
|
361
437
|
/**
|
|
362
438
|
* Builds and returns the OpenAPI middleware.
|
|
363
439
|
*
|
|
@@ -365,10 +441,13 @@ var OpenApiMiddlewareBuilder = /** @class */ (function () {
|
|
|
365
441
|
* that integrates with the OpenAPI documentation system. If no OpenAPI
|
|
366
442
|
* path is configured in options, returns a no-op middleware.
|
|
367
443
|
*
|
|
444
|
+
* If validation was enabled via `withValidation()`, returns an array
|
|
445
|
+
* of middleware: [openApiDocMiddleware, validationMiddleware].
|
|
446
|
+
*
|
|
368
447
|
* Default error responses (400, 401, 403, 404, 405) are automatically
|
|
369
448
|
* merged with the configured responses.
|
|
370
449
|
*
|
|
371
|
-
* @returns Express middleware function for OpenAPI documentation
|
|
450
|
+
* @returns Express middleware function(s) for OpenAPI documentation and optional validation
|
|
372
451
|
*
|
|
373
452
|
* @example
|
|
374
453
|
* ```typescript
|
|
@@ -381,13 +460,38 @@ var OpenApiMiddlewareBuilder = /** @class */ (function () {
|
|
|
381
460
|
* ```
|
|
382
461
|
*/
|
|
383
462
|
OpenApiMiddlewareBuilder.prototype.build = function () {
|
|
384
|
-
var _c, _d, _e;
|
|
463
|
+
var _c, _d, _e, _f, _g;
|
|
385
464
|
var noop = function (_a, _b, next) { return next(); };
|
|
386
|
-
|
|
465
|
+
// Build the OpenAPI documentation middleware
|
|
466
|
+
var openApiMiddleware = noop;
|
|
467
|
+
if ((_c = this.options.openApi) === null || _c === void 0 ? void 0 : _c.path) {
|
|
468
|
+
openApiMiddleware = this.options.openApi.path((0, merge_1.default)(__assign(__assign({}, this.config), { responses: __assign(__assign({}, this.config.responses), openApi_1.defaultOpenApiErrorResponses) }), (_e = (_d = this.options.openApiOverwrite) === null || _d === void 0 ? void 0 : _d.get) !== null && _e !== void 0 ? _e : {}));
|
|
469
|
+
}
|
|
470
|
+
else {
|
|
387
471
|
logger_1.logger.debug("No options.openApi provided, skipping OpenApiMiddleware");
|
|
388
|
-
return noop;
|
|
389
472
|
}
|
|
390
|
-
|
|
473
|
+
// Check if validation should be enabled
|
|
474
|
+
var globalConfig = (0, openApiValidator_1.getOpenApiValidatorConfig)();
|
|
475
|
+
var shouldValidate = (_f = this.validationConfig.enabled) !== null && _f !== void 0 ? _f : ((0, openApiValidator_1.isOpenApiValidatorConfigured)() && ((_g = globalConfig.validateRequests) !== null && _g !== void 0 ? _g : false));
|
|
476
|
+
if (!shouldValidate) {
|
|
477
|
+
return openApiMiddleware;
|
|
478
|
+
}
|
|
479
|
+
// Build validation middleware
|
|
480
|
+
var validators = [openApiMiddleware];
|
|
481
|
+
// Add body validation if we have a request body schema
|
|
482
|
+
if (this.validationConfig.validateBody && this.requestBodySchema) {
|
|
483
|
+
validators.push((0, openApiValidator_1.validateRequestBody)(this.requestBodySchema, { enabled: true }));
|
|
484
|
+
}
|
|
485
|
+
// Add query validation if we have query parameter schemas
|
|
486
|
+
if (this.validationConfig.validateQuery && Object.keys(this.queryParamSchemas).length > 0) {
|
|
487
|
+
validators.push((0, openApiValidator_1.validateQueryParams)(this.queryParamSchemas, { enabled: true }));
|
|
488
|
+
}
|
|
489
|
+
// If only one middleware (the openApi one), return it directly
|
|
490
|
+
if (validators.length === 1) {
|
|
491
|
+
return openApiMiddleware;
|
|
492
|
+
}
|
|
493
|
+
// Return array of middleware to be spread in route definition
|
|
494
|
+
return validators;
|
|
391
495
|
};
|
|
392
496
|
return OpenApiMiddlewareBuilder;
|
|
393
497
|
}());
|
|
@@ -0,0 +1,296 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* OpenAPI Request Validator
|
|
3
|
+
*
|
|
4
|
+
* Provides runtime validation of incoming requests against OpenAPI schemas.
|
|
5
|
+
* Uses AJV for JSON Schema validation with OpenAPI-compatible settings.
|
|
6
|
+
*
|
|
7
|
+
* Validation is always installed as middleware but only activates after
|
|
8
|
+
* `configureOpenApiValidator()` is called. This makes it safe to include
|
|
9
|
+
* in modelRouter by default.
|
|
10
|
+
*
|
|
11
|
+
* @module openApiValidator
|
|
12
|
+
*
|
|
13
|
+
* @packageDocumentation
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```typescript
|
|
17
|
+
* // Enable validation globally at server startup
|
|
18
|
+
* configureOpenApiValidator({
|
|
19
|
+
* removeAdditional: true,
|
|
20
|
+
* onAdditionalPropertiesRemoved: (props, req) => {
|
|
21
|
+
* logger.warn(`Stripped: ${props.join(", ")} on ${req.method} ${req.path}`);
|
|
22
|
+
* },
|
|
23
|
+
* });
|
|
24
|
+
*
|
|
25
|
+
* // modelRouter automatically validates when configured
|
|
26
|
+
* modelRouter(Todo, {
|
|
27
|
+
* permissions: {...},
|
|
28
|
+
* validation: {
|
|
29
|
+
* validateCreate: true,
|
|
30
|
+
* validateUpdate: true,
|
|
31
|
+
* validateQuery: true,
|
|
32
|
+
* },
|
|
33
|
+
* });
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
import { type ErrorObject } from "ajv";
|
|
37
|
+
import type { NextFunction, Request, Response } from "express";
|
|
38
|
+
import type { Model } from "mongoose";
|
|
39
|
+
import type { OpenApiSchemaProperty } from "./openApiBuilder";
|
|
40
|
+
/**
|
|
41
|
+
* Global configuration for OpenAPI validation.
|
|
42
|
+
* This can be set at server startup to control validation behavior.
|
|
43
|
+
*/
|
|
44
|
+
export interface OpenApiValidatorConfig {
|
|
45
|
+
/**
|
|
46
|
+
* Enable or disable request body validation.
|
|
47
|
+
* Default: true (when configureOpenApiValidator is called)
|
|
48
|
+
*/
|
|
49
|
+
validateRequests?: boolean;
|
|
50
|
+
/**
|
|
51
|
+
* Enable or disable response validation.
|
|
52
|
+
* Default: false (response validation has performance overhead)
|
|
53
|
+
*/
|
|
54
|
+
validateResponses?: boolean;
|
|
55
|
+
/**
|
|
56
|
+
* Whether to coerce types (e.g., string "123" to number 123).
|
|
57
|
+
* Default: true
|
|
58
|
+
*/
|
|
59
|
+
coerceTypes?: boolean;
|
|
60
|
+
/**
|
|
61
|
+
* Whether to remove additional properties not in the schema.
|
|
62
|
+
* Default: true
|
|
63
|
+
*/
|
|
64
|
+
removeAdditional?: boolean;
|
|
65
|
+
/**
|
|
66
|
+
* Custom error handler for validation failures.
|
|
67
|
+
* If not provided, throws an APIError with status 400.
|
|
68
|
+
*/
|
|
69
|
+
onValidationError?: (errors: ErrorObject[], req: Request) => void;
|
|
70
|
+
/**
|
|
71
|
+
* Log validation errors for debugging.
|
|
72
|
+
* Default: true
|
|
73
|
+
*/
|
|
74
|
+
logValidationErrors?: boolean;
|
|
75
|
+
/**
|
|
76
|
+
* Callback fired when additional properties are removed from a request body.
|
|
77
|
+
* Only fires when `removeAdditional: true` and extra properties are present.
|
|
78
|
+
* Receives the list of removed property names and the request.
|
|
79
|
+
*/
|
|
80
|
+
onAdditionalPropertiesRemoved?: (removedProperties: string[], req: Request) => void;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Check whether `configureOpenApiValidator()` has been called.
|
|
84
|
+
* Validation middleware is a no-op when this returns false.
|
|
85
|
+
*/
|
|
86
|
+
export declare function isOpenApiValidatorConfigured(): boolean;
|
|
87
|
+
/**
|
|
88
|
+
* Configure the global OpenAPI validator settings.
|
|
89
|
+
* Calling this function activates validation — middleware that was previously
|
|
90
|
+
* installed as a no-op will begin validating requests.
|
|
91
|
+
*
|
|
92
|
+
* @param config - Configuration options to merge with existing config
|
|
93
|
+
*
|
|
94
|
+
* @example
|
|
95
|
+
* ```typescript
|
|
96
|
+
* configureOpenApiValidator({
|
|
97
|
+
* removeAdditional: true,
|
|
98
|
+
* onAdditionalPropertiesRemoved: (props, req) => {
|
|
99
|
+
* Sentry.captureMessage(`Stripped: ${props.join(", ")} on ${req.method} ${req.path}`);
|
|
100
|
+
* },
|
|
101
|
+
* });
|
|
102
|
+
* ```
|
|
103
|
+
*/
|
|
104
|
+
export declare function configureOpenApiValidator(config?: Partial<OpenApiValidatorConfig>): void;
|
|
105
|
+
/**
|
|
106
|
+
* Get the current global validator configuration.
|
|
107
|
+
*/
|
|
108
|
+
export declare function getOpenApiValidatorConfig(): OpenApiValidatorConfig;
|
|
109
|
+
/**
|
|
110
|
+
* Reset the global validator configuration to defaults.
|
|
111
|
+
* Also resets `isConfigured` to false.
|
|
112
|
+
* Useful for testing.
|
|
113
|
+
*/
|
|
114
|
+
export declare function resetOpenApiValidatorConfig(): void;
|
|
115
|
+
/**
|
|
116
|
+
* Options for the request body validator middleware.
|
|
117
|
+
*/
|
|
118
|
+
export interface RequestBodyValidatorOptions {
|
|
119
|
+
/**
|
|
120
|
+
* Override the global validateRequests setting for this specific route.
|
|
121
|
+
*/
|
|
122
|
+
enabled?: boolean;
|
|
123
|
+
/**
|
|
124
|
+
* List of required field names.
|
|
125
|
+
*/
|
|
126
|
+
required?: string[];
|
|
127
|
+
/**
|
|
128
|
+
* Fields to exclude from validation (e.g. fields set by preCreate hooks).
|
|
129
|
+
* Excluded fields are removed from both the schema properties and the required array.
|
|
130
|
+
*/
|
|
131
|
+
excludeFields?: string[];
|
|
132
|
+
/**
|
|
133
|
+
* Custom error handler for this specific route.
|
|
134
|
+
*/
|
|
135
|
+
onError?: (errors: ErrorObject[], req: Request) => void;
|
|
136
|
+
/**
|
|
137
|
+
* Callback fired when additional properties are removed.
|
|
138
|
+
* Overrides the global onAdditionalPropertiesRemoved for this route.
|
|
139
|
+
*/
|
|
140
|
+
onAdditionalPropertiesRemoved?: (removedProperties: string[], req: Request) => void;
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Creates middleware that validates the request body against an OpenAPI schema.
|
|
144
|
+
*
|
|
145
|
+
* The middleware checks `isConfigured` at request time — if `configureOpenApiValidator()`
|
|
146
|
+
* has not been called, the middleware is a no-op.
|
|
147
|
+
*
|
|
148
|
+
* @param schema - The schema to validate against (same format as withRequestBody)
|
|
149
|
+
* @param options - Optional configuration for this validator
|
|
150
|
+
* @returns Express middleware function
|
|
151
|
+
*/
|
|
152
|
+
export declare function validateRequestBody(schema: Record<string, OpenApiSchemaProperty>, options?: RequestBodyValidatorOptions): (req: Request, res: Response, next: NextFunction) => void;
|
|
153
|
+
/**
|
|
154
|
+
* Options for the query parameter validator middleware.
|
|
155
|
+
*/
|
|
156
|
+
export interface QueryValidatorOptions {
|
|
157
|
+
/**
|
|
158
|
+
* Override the global validateRequests setting for this specific route.
|
|
159
|
+
*/
|
|
160
|
+
enabled?: boolean;
|
|
161
|
+
/**
|
|
162
|
+
* Custom error handler for this specific route.
|
|
163
|
+
*/
|
|
164
|
+
onError?: (errors: ErrorObject[], req: Request) => void;
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* Creates middleware that validates query parameters against an OpenAPI schema.
|
|
168
|
+
*
|
|
169
|
+
* @param schema - The schema to validate against
|
|
170
|
+
* @param options - Optional configuration for this validator
|
|
171
|
+
* @returns Express middleware function
|
|
172
|
+
*/
|
|
173
|
+
export declare function validateQueryParams(schema: Record<string, OpenApiSchemaProperty>, options?: QueryValidatorOptions): (req: Request, res: Response, next: NextFunction) => void;
|
|
174
|
+
/**
|
|
175
|
+
* Options for creating a combined validation middleware.
|
|
176
|
+
*/
|
|
177
|
+
export interface CreateValidatorOptions {
|
|
178
|
+
/**
|
|
179
|
+
* Schema for request body validation.
|
|
180
|
+
*/
|
|
181
|
+
body?: Record<string, OpenApiSchemaProperty>;
|
|
182
|
+
/**
|
|
183
|
+
* Schema for query parameter validation.
|
|
184
|
+
*/
|
|
185
|
+
query?: Record<string, OpenApiSchemaProperty>;
|
|
186
|
+
/**
|
|
187
|
+
* Override the global validation enabled setting.
|
|
188
|
+
*/
|
|
189
|
+
enabled?: boolean;
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* Creates a combined validation middleware for both body and query parameters.
|
|
193
|
+
*
|
|
194
|
+
* @param options - Configuration for what to validate
|
|
195
|
+
* @returns Express middleware function
|
|
196
|
+
*
|
|
197
|
+
* @example
|
|
198
|
+
* ```typescript
|
|
199
|
+
* router.post("/search", [
|
|
200
|
+
* openApiMiddleware,
|
|
201
|
+
* createValidator({
|
|
202
|
+
* body: {query: {type: "string", required: true}},
|
|
203
|
+
* query: {limit: {type: "number"}},
|
|
204
|
+
* }),
|
|
205
|
+
* ], handler);
|
|
206
|
+
* ```
|
|
207
|
+
*/
|
|
208
|
+
export declare function createValidator(options: CreateValidatorOptions): (req: Request, res: Response, next: NextFunction) => void;
|
|
209
|
+
/**
|
|
210
|
+
* Validates response data against a schema.
|
|
211
|
+
* This is primarily for development/testing to ensure responses match documentation.
|
|
212
|
+
*
|
|
213
|
+
* @param data - The response data to validate
|
|
214
|
+
* @param schema - The expected schema
|
|
215
|
+
* @returns Object with valid flag and any errors
|
|
216
|
+
*/
|
|
217
|
+
export declare function validateResponseData(data: unknown, schema: Record<string, OpenApiSchemaProperty>): {
|
|
218
|
+
valid: boolean;
|
|
219
|
+
errors?: ErrorObject[];
|
|
220
|
+
};
|
|
221
|
+
/**
|
|
222
|
+
* Extract an OpenAPI-compatible schema from a Mongoose model.
|
|
223
|
+
* This allows you to use the same schema definitions for both documentation
|
|
224
|
+
* and runtime validation.
|
|
225
|
+
*
|
|
226
|
+
* @param model - A Mongoose model
|
|
227
|
+
* @returns Schema properties suitable for validation
|
|
228
|
+
*/
|
|
229
|
+
export declare function getSchemaFromModel<T>(model: Model<T>): Record<string, OpenApiSchemaProperty>;
|
|
230
|
+
/**
|
|
231
|
+
* Creates a request body validator middleware from a Mongoose model.
|
|
232
|
+
* This is a convenience function that combines getSchemaFromModel and validateRequestBody.
|
|
233
|
+
*
|
|
234
|
+
* @param model - A Mongoose model to derive the schema from
|
|
235
|
+
* @param options - Optional configuration for the validator
|
|
236
|
+
* @returns Express middleware function
|
|
237
|
+
*/
|
|
238
|
+
export declare function validateModelRequestBody<T>(model: Model<T>, options?: RequestBodyValidatorOptions): (req: Request, res: Response, next: NextFunction) => void;
|
|
239
|
+
/**
|
|
240
|
+
* Options for creating validation middleware for a modelRouter.
|
|
241
|
+
*/
|
|
242
|
+
export interface ModelRouterValidationOptions {
|
|
243
|
+
/**
|
|
244
|
+
* Enable validation for create (POST) requests.
|
|
245
|
+
* Default: true (when validation is globally enabled)
|
|
246
|
+
*/
|
|
247
|
+
validateCreate?: boolean;
|
|
248
|
+
/**
|
|
249
|
+
* Enable validation for update (PATCH) requests.
|
|
250
|
+
* Default: true (when validation is globally enabled)
|
|
251
|
+
*/
|
|
252
|
+
validateUpdate?: boolean;
|
|
253
|
+
/**
|
|
254
|
+
* Enable validation for query (GET list) requests.
|
|
255
|
+
* Default: true (when validation is globally enabled)
|
|
256
|
+
*/
|
|
257
|
+
validateQuery?: boolean;
|
|
258
|
+
/**
|
|
259
|
+
* Fields to exclude from create validation (e.g. fields injected by preCreate).
|
|
260
|
+
*/
|
|
261
|
+
excludeFromCreate?: string[];
|
|
262
|
+
/**
|
|
263
|
+
* Fields to exclude from update validation (e.g. fields injected by preUpdate).
|
|
264
|
+
*/
|
|
265
|
+
excludeFromUpdate?: string[];
|
|
266
|
+
/**
|
|
267
|
+
* Custom error handler for validation failures.
|
|
268
|
+
*/
|
|
269
|
+
onError?: (errors: ErrorObject[], req: Request) => void;
|
|
270
|
+
/**
|
|
271
|
+
* Callback fired when additional properties are removed from a request body.
|
|
272
|
+
* Overrides the global onAdditionalPropertiesRemoved for this router.
|
|
273
|
+
*/
|
|
274
|
+
onAdditionalPropertiesRemoved?: (removedProperties: string[], req: Request) => void;
|
|
275
|
+
}
|
|
276
|
+
/**
|
|
277
|
+
* Creates validation middleware for use with modelRouter.
|
|
278
|
+
* Returns an object with middleware for each operation type.
|
|
279
|
+
*
|
|
280
|
+
* @param model - The Mongoose model
|
|
281
|
+
* @param options - Configuration options
|
|
282
|
+
* @returns Object with create and update validation middleware
|
|
283
|
+
*/
|
|
284
|
+
export declare function createModelValidators<T>(model: Model<T>, options?: ModelRouterValidationOptions): {
|
|
285
|
+
create: (req: Request, res: Response, next: NextFunction) => void;
|
|
286
|
+
update: (req: Request, res: Response, next: NextFunction) => void;
|
|
287
|
+
};
|
|
288
|
+
/**
|
|
289
|
+
* Build a query parameter schema from a model's Mongoose schema and queryFields array.
|
|
290
|
+
* Always includes pagination parameters (limit, page, sort).
|
|
291
|
+
*
|
|
292
|
+
* @param model - A Mongoose model
|
|
293
|
+
* @param queryFields - Array of field names allowed for querying
|
|
294
|
+
* @returns Schema properties suitable for query validation
|
|
295
|
+
*/
|
|
296
|
+
export declare function buildQuerySchemaFromFields<T>(model: Model<T>, queryFields?: string[]): Record<string, OpenApiSchemaProperty>;
|