Skip to content

Commit 836cd84

Browse files
Merge pull request #2924 from LucasHang/patch-2
docs(pipes): improve zod examples
2 parents cd926be + f643f69 commit 836cd84

File tree

1 file changed

+7
-8
lines changed

1 file changed

+7
-8
lines changed

content/pipes.md

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -249,34 +249,33 @@ In the next section, you'll see how we supply the appropriate schema for a given
249249
```typescript
250250
@@filename()
251251
import { PipeTransform, ArgumentMetadata, BadRequestException } from '@nestjs/common';
252-
import { ZodObject } from 'zod';
252+
import { ZodSchema } from 'zod';
253253

254254
export class ZodValidationPipe implements PipeTransform {
255-
constructor(private schema: ZodObject<any>) {}
255+
constructor(private schema: ZodSchema) {}
256256

257257
transform(value: unknown, metadata: ArgumentMetadata) {
258258
try {
259-
this.schema.parse(value);
259+
const parsedValue = this.schema.parse(value);
260+
return parsedValue;
260261
} catch (error) {
261262
throw new BadRequestException('Validation failed');
262263
}
263-
return value;
264264
}
265265
}
266266
@@switch
267267
import { BadRequestException } from '@nestjs/common';
268-
import { ZodObject } from 'zod';
269268

270-
export class ZodValidationPip {
269+
export class ZodValidationPipe {
271270
constructor(private schema) {}
272271

273272
transform(value, metadata) {
274273
try {
275-
this.schema.parse(value);
274+
const parsedValue = this.schema.parse(value);
275+
return parsedValue;
276276
} catch (error) {
277277
throw new BadRequestException('Validation failed');
278278
}
279-
return value;
280279
}
281280
}
282281

0 commit comments

Comments
 (0)