Skip to content

Commit

Permalink
fix(docs): describe typed-headers
Browse files Browse the repository at this point in the history
  • Loading branch information
glebbash committed Mar 7, 2022
1 parent 4cc35fc commit 1bd680d
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 5 deletions.
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,37 @@ All of the decorators support the following parameters:

Also decorators have additional parameters like: `min`, `max` for `IsNumber`.

### Headers validation

You can also validate request headers using `TypedHeaders` decorator.

```ts
export class TestHeaders {
@IsString({
// Note: header names will be lowercased automatically
name: 'country-code',
maxLength: 2,
minLength: 2,
example: 'US',
})
countryCode!: string;

@IsString({
isDate: { format: 'date-time' },
name: 'timestamp',
})
timestamp!: string;
}

@Controller({ path: 'test', version: '1' })
export class TestController {
@Get()
async test(@TypedHeaders() headers: TestHeaders): Promise<string> {
return headers.countryCode;
}
}
```

## Other

Bootstrapped with: [create-ts-lib-gh](https://github.com/glebbash/create-ts-lib-gh)
Expand Down
6 changes: 1 addition & 5 deletions src/decorators/typed-headers.decorator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ import { Controller, Get, INestApplication } from '@nestjs/common';
import { Test } from '@nestjs/testing';
import request from 'supertest';

import { IsString } from './is-string';
import { TypedHeaders } from './typed-headers.decorator';
import { IsString, TypedHeaders } from '../nestjs-swagger-dto';

export class TestHeaders {
@IsString({
description: '[ISO_3166-1_alpha-2](https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2)',
name: 'country-code',
maxLength: 2,
minLength: 2,
Expand All @@ -17,8 +15,6 @@ export class TestHeaders {

@IsString({
isDate: { format: 'date-time' },
example: '2011-12-01T00:00:00.000Z',
description: '[ISO 8601](https://en.wikipedia.org/wiki/ISO_8601)',
name: 'timestamp',
})
timestamp!: string;
Expand Down
1 change: 1 addition & 0 deletions src/nestjs-swagger-dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ export * from './decorators/is-nested';
export * from './decorators/is-number';
export * from './decorators/is-object';
export * from './decorators/is-string';
export * from './decorators/typed-headers.decorator';

0 comments on commit 1bd680d

Please sign in to comment.