Skip to content

Let's use AI to Earn!

906 B
Raw Blame
import type { ArgumentMetadata, PipeTransform } from '@nestjs/common'
import type { ZodType } from 'zod'
import type { ZodDto } from '../utils'
import { ValidationPipe } from '@nestjs/common'
import { isZodDto, zodValidate } from '../utils'

export class ZodValidationPipe extends ValidationPipe implements PipeTransform {
  constructor(private schemaOrDto?: ZodType | ZodDto) {
    super({
      transform: true,
      whitelist: true,
      transformOptions: {
        excludeExtraneousValues: true,
      },
    })
  }

  public override transform(value: unknown, metadata: ArgumentMetadata): Promise<unknown> {
    if (this.schemaOrDto) {
      return Promise.resolve(zodValidate(value, this.schemaOrDto))
    }

    const { metatype } = metadata

    if (!isZodDto(metatype)) {
      return super.transform(value, metadata)
    }

    return Promise.resolve(zodValidate(value, metatype.schema))
  }
}