import { ArgumentMetadata, Injectable, PipeTransform } from '@nestjs/common';
import { ZodSchema } from 'zod';

/** Validates and transforms request payloads with a zod schema. Throws ZodError (handled by the exception filter). */
@Injectable()
export class ZodValidationPipe implements PipeTransform {
  constructor(private readonly schema: ZodSchema) {}

  transform(value: unknown, _metadata: ArgumentMetadata) {
    return this.schema.parse(value);
  }
}

export const zodBody = (schema: ZodSchema) => new ZodValidationPipe(schema);
export const zodQuery = (schema: ZodSchema) => new ZodValidationPipe(schema);
