Skip to content

Commit 695208b

Browse files
authored
Merge pull request #5 from StatelessStudio/v1.2.1
V1.2.1
2 parents 813e23f + f97bba9 commit 695208b

11 files changed

Lines changed: 236 additions & 217 deletions

README.md

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -212,24 +212,26 @@ For the complete list of error classes, usage examples, and custom error creatio
212212

213213
## Validation & Sanitization
214214

215-
api-machine supports request validation and sanitization using [valsan](https://www.npmjs.com/package/valsan). You can declare ObjectSanitizer members on your endpoint classes for `body`, `query`, `params`, or `headers`:
215+
api-machine supports request validation and sanitization using [valsan](https://www.npmjs.com/package/valsan). You can declare ObjectValSan members on your endpoint classes for `body`, `query`, `params`, or `headers`:
216216

217217
```typescript
218-
import { ObjectSanitizer, EmailValidator } from 'valsan';
218+
import { ObjectValSan, EmailValidator } from 'valsan';
219219
import { NameValSan } from './examples/complete-example/users/name-valsan';
220220

221221
class CreateUserEndpoint extends PostEndpoint {
222-
override path = '/users';
222+
override path = '/users';
223223

224-
override body = new ObjectSanitizer({
225-
name: new NameValSan(),
226-
email: new EmailValidator(),
227-
});
224+
override body = new ObjectValSan({
225+
schema: {
226+
name: new NameValSan(),
227+
email: new EmailValidator(),
228+
}
229+
});
228230

229-
async handle(request, response) {
230-
// request.body is validated & sanitized
231-
// ...
232-
}
231+
async handle(request, response) {
232+
// request.body is validated & sanitized
233+
// ...
234+
}
233235
}
234236
```
235237

examples/complete-example/users/create-user-endpoint.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { ApiRequest, ApiResponse, PostEndpoint } from '../../../src/index';
22
import { usersRepo, User } from './users-repository';
3-
import { ObjectSanitizer, EmailValidator, IntegerValidator } from 'valsan';
3+
import { ObjectValSan, EmailValidator, IntegerValidator } from 'valsan';
44
import { NameValSan } from './name-valsan';
55

66
/**
@@ -16,9 +16,11 @@ export class CreateUserEndpoint extends PostEndpoint {
1616
email: 'john@example.com',
1717
};
1818

19-
override body = new ObjectSanitizer({
20-
name: new NameValSan(),
21-
email: new EmailValidator(),
19+
override body = new ObjectValSan({
20+
schema: {
21+
name: new NameValSan(),
22+
email: new EmailValidator(),
23+
},
2224
});
2325

2426
override responseExample = {
@@ -28,10 +30,12 @@ export class CreateUserEndpoint extends PostEndpoint {
2830
created: new Date(),
2931
};
3032

31-
override response = new ObjectSanitizer({
32-
id: new IntegerValidator(),
33-
name: new NameValSan(),
34-
email: new EmailValidator(),
33+
override response = new ObjectValSan({
34+
schema: {
35+
id: new IntegerValidator(),
36+
name: new NameValSan(),
37+
email: new EmailValidator(),
38+
},
3539
});
3640

3741
async handle(request: ApiRequest, response: ApiResponse) {

examples/complete-example/users/get-user-endpoint.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { ApiRequest, ApiResponse, GetEndpoint } from '../../../src/index';
22
import { NotFoundError } from '../../../src/error';
33
import { usersRepo } from './users-repository';
4-
import { ObjectSanitizer, IntegerValidator, EmailValidator } from 'valsan';
4+
import { IntegerValidator, EmailValidator, ObjectValSan } from 'valsan';
55
import { NameValSan } from './name-valsan';
66

77
/**
@@ -20,10 +20,12 @@ export class GetUserEndpoint extends GetEndpoint {
2020
created: new Date('2023-01-01'),
2121
};
2222

23-
override response = new ObjectSanitizer({
24-
id: new IntegerValidator(),
25-
name: new NameValSan(),
26-
email: new EmailValidator(),
23+
override response = new ObjectValSan({
24+
schema: {
25+
id: new IntegerValidator(),
26+
name: new NameValSan(),
27+
email: new EmailValidator(),
28+
},
2729
});
2830

2931
async handle(request: ApiRequest, response: ApiResponse) {

examples/complete-example/users/list-users-endpoint.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {
22
ComposedValSan,
33
LengthValidator,
4-
ObjectSanitizer,
4+
ObjectValSan,
55
TrimSanitizer,
66
} from 'valsan';
77
import { ApiRequest, ApiResponse, GetEndpoint } from '../../../src/index';
@@ -16,15 +16,17 @@ import { usersRepo } from './users-repository';
1616
export class ListUsersEndpoint extends GetEndpoint {
1717
override path = '/';
1818

19-
override params = new ObjectSanitizer({
20-
name: new ComposedValSan(
21-
[new TrimSanitizer(), new LengthValidator({ minLength: 3 })],
22-
{ isOptional: true }
23-
),
24-
email: new ComposedValSan(
25-
[new TrimSanitizer(), new LengthValidator({ minLength: 5 })],
26-
{ isOptional: true }
27-
),
19+
override params = new ObjectValSan({
20+
schema: {
21+
name: new ComposedValSan(
22+
[new TrimSanitizer(), new LengthValidator({ minLength: 3 })],
23+
{ isOptional: true }
24+
),
25+
email: new ComposedValSan(
26+
[new TrimSanitizer(), new LengthValidator({ minLength: 5 })],
27+
{ isOptional: true }
28+
),
29+
},
2830
});
2931

3032
async handle(request: ApiRequest, response: ApiResponse) {

0 commit comments

Comments
 (0)