Skip to content

Commit 1851d0b

Browse files
committed
feat: add support for array-valsan (endpoint responses)
1 parent ac262e5 commit 1851d0b

9 files changed

Lines changed: 132 additions & 82 deletions

package-lock.json

Lines changed: 11 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@
2222
"publish": "npm run script -- publish"
2323
},
2424
"dependencies": {
25-
"auto-oas": "^1.2.1",
25+
"auto-oas": "^1.3.0",
2626
"cors": "^2.8.5",
2727
"express": "^5.1.0",
2828
"swagger-ui-express": "^5.0.1",
29-
"valsan": "^2.2.0"
29+
"valsan": "^2.3.0"
3030
},
3131
"devDependencies": {
3232
"@istanbuljs/nyc-config-typescript": "^1.0.2",
@@ -52,7 +52,7 @@
5252
"typescript": "~5.4.2"
5353
},
5454
"peerDependencies": {
55-
"valsan": "^2.2.0"
55+
"valsan": "^2.3.0"
5656
},
5757
"repository": {
5858
"type": "git",

src/oas/oas-endpoint-component-converter.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ObjectSanitizer } from 'valsan';
1+
import { ObjectSanitizer, ObjectValSan, ArrayValSan } from 'valsan';
22
import { SchemaObject } from 'auto-oas/oas/v3.1';
33

44
import { BaseApiEndpoint } from '../router';
@@ -41,7 +41,7 @@ export class OasEndpointComponentConverter {
4141
example,
4242
}: {
4343
name: string;
44-
sanitizer: ObjectSanitizer;
44+
sanitizer: ObjectSanitizer | ObjectValSan | ArrayValSan;
4545
// eslint-disable-next-line @typescript-eslint/no-explicit-any
4646
example?: any;
4747
}) {

src/oas/oas-endpoint-converter.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ObjectSanitizer } from 'valsan';
1+
import { ObjectSanitizer, ObjectValSan } from 'valsan';
22
import {
33
PathItemObject,
44
ParameterObject,
@@ -122,7 +122,7 @@ export class OasEndpointConverter {
122122
example,
123123
}: {
124124
location: 'path' | 'query' | 'header';
125-
sanitizer?: ObjectSanitizer;
125+
sanitizer?: ObjectSanitizer | ObjectValSan;
126126
// eslint-disable-next-line @typescript-eslint/no-explicit-any
127127
example?: any;
128128
}) {

src/router/endpoint.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { ObjectSanitizer } from 'valsan/object-sanitizer';
1010
import { BaseApiRoute } from './base';
1111
import { validateRequest } from './validation-middleware';
1212
import { BadRequestError, HTTPError, UnprocessableEntityError } from '../error';
13+
import { ArrayValSan, ObjectValSan } from 'valsan';
1314

1415
export type ApiRequest = ExpressRequest;
1516
export type ApiResponse = ExpressResponse;
@@ -33,23 +34,23 @@ export abstract class BaseApiEndpoint extends BaseApiRoute {
3334
public method: EndpointMethod = EndpointMethod.GET;
3435
public statusCode: number = 200;
3536

36-
public body?: ObjectSanitizer;
37+
public body?: ObjectSanitizer | ObjectValSan;
3738
// eslint-disable-next-line @typescript-eslint/no-explicit-any
3839
public bodyExample?: any;
3940

40-
public query?: ObjectSanitizer;
41+
public query?: ObjectSanitizer | ObjectValSan;
4142
// eslint-disable-next-line @typescript-eslint/no-explicit-any
4243
public queryExample?: any;
4344

44-
public params?: ObjectSanitizer;
45+
public params?: ObjectSanitizer | ObjectValSan;
4546
// eslint-disable-next-line @typescript-eslint/no-explicit-any
4647
public paramsExample?: any;
4748

48-
public headers?: ObjectSanitizer;
49+
public headers?: ObjectSanitizer | ObjectValSan;
4950
// eslint-disable-next-line @typescript-eslint/no-explicit-any
5051
public headersExample?: any;
5152

52-
public response?: ObjectSanitizer;
53+
public response?: ObjectSanitizer | ObjectValSan | ArrayValSan;
5354
// eslint-disable-next-line @typescript-eslint/no-explicit-any
5455
public responseExample?: any;
5556

src/router/validation-middleware.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
1-
import { ObjectSanitizer } from 'valsan';
1+
import {
2+
ArrayValSan,
3+
ObjectSanitizer,
4+
ObjectValSan,
5+
ObjectValSanOptions,
6+
} from 'valsan';
27
import { ApiRequest, BaseApiEndpoint } from './endpoint';
38
import { UnprocessableEntityError } from '../error';
49

510
/**
611
* Runs a valsan ObjectSanitizer on a value,
712
* throws with error details if validation fails.
813
*/
9-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
10-
export async function runSanitizer(sanitizer: ObjectSanitizer, value: any) {
14+
export async function runSanitizer(
15+
sanitizer: ObjectSanitizer | ObjectValSan | ArrayValSan,
16+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
17+
value: any
18+
) {
1119
const result = await sanitizer.run(value);
1220

1321
if (!result.success) {
@@ -35,6 +43,12 @@ export async function validateRequest(
3543
continue;
3644
}
3745

46+
if (part === 'headers' && 'options' in sanitizer) {
47+
(
48+
sanitizer.options as ObjectValSanOptions
49+
).allowAdditionalProperties = true;
50+
}
51+
3852
const sanitized = await runSanitizer(sanitizer, request[part]);
3953

4054
if (part === 'query') {

0 commit comments

Comments
 (0)