From f643f69f88060cc1d958e1c8e3f6367888115b52 Mon Sep 17 00:00:00 2001 From: Lucas Hang Date: Sat, 16 Dec 2023 17:31:35 -0300 Subject: [PATCH] docs(pipes): improve zod examples use ZodSchema interface instead of ZodObject --- content/pipes.md | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/content/pipes.md b/content/pipes.md index d3b20cbcda..a9933855c5 100644 --- a/content/pipes.md +++ b/content/pipes.md @@ -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) {} + 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; } }