Skip to content

Commit

Permalink
docs(pipes): improve zod examples
Browse files Browse the repository at this point in the history
use ZodSchema interface instead of ZodObject
  • Loading branch information
LucasHang authored Dec 16, 2023
1 parent cd926be commit f643f69
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions content/pipes.md
Original file line number Diff line number Diff line change
Expand Up @@ -249,34 +249,33 @@ In the next section, you'll see how we supply the appropriate schema for a given
```typescript
@@filename()
import { PipeTransform, ArgumentMetadata, BadRequestException } from '@nestjs/common';
import { ZodObject } from 'zod';
import { ZodSchema } from 'zod';

export class ZodValidationPipe implements PipeTransform {
constructor(private schema: ZodObject<any>) {}
constructor(private schema: ZodSchema) {}

transform(value: unknown, metadata: ArgumentMetadata) {
try {
this.schema.parse(value);
const parsedValue = this.schema.parse(value);
return parsedValue;
} catch (error) {
throw new BadRequestException('Validation failed');
}
return value;
}
}
@@switch
import { BadRequestException } from '@nestjs/common';
import { ZodObject } from 'zod';

export class ZodValidationPip {
export class ZodValidationPipe {
constructor(private schema) {}

transform(value, metadata) {
try {
this.schema.parse(value);
const parsedValue = this.schema.parse(value);
return parsedValue;
} catch (error) {
throw new BadRequestException('Validation failed');
}
return value;
}
}

Expand Down

0 comments on commit f643f69

Please sign in to comment.