A flexible validation pipe for NestJS that leverages @standard-schema to support multiple validation libraries like Zod, Valibot, ArkType and more.
- 🔄 Drop-in replacement for NestJS's ValidationPipe
- 🎯 Support for multiple validation libraries (Zod, Valibot, ArkType, etc.)
- 🚀 Easy integration with existing NestJS applications
- 📦 Lightweight with minimal dependencies
npm install nestjs-standard-schema
yarn add nestjs-standard-schema
pnpm add nestjs-standard-schema
- Create your DTO using your preferred validation library (example with Zod):
import { z } from 'zod';
import { createStandardSchemaDTO } from 'nestjs-standard-schema';
const createUserSchema = z.object({
name: z.string(),
age: z.number(),
});
export const CreateUserDTO = createStandardSchemaDTO(createUserSchema);
- Use the
CreateUserDTO
in your NestJS application:
import { Controller, Post, Body } from '@nestjs/common';
import { StandardSchemaValidationPipe } from 'nestjs-standard-schema';
@Controller('users')
export class UsersController {
@Post()
createUser(@Body(new StandardSchemaValidationPipe()) user: CreateUserDTO) {
return user;
}
}
To use the validation pipe globally, register it in your app module:
import { StandardSchemaValidationPipe } from 'nestjs-standard-schema';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
app.useGlobalPipes(new StandardSchemaValidationPipe());
await app.listen(3000);
}
bootstrap();
- [ ] Support for all ValidationPipe options from @nestjs/common
- [ ] Custom error messages
- [ ] Transform options
- [ ] Whitelist/strip options
- [ ] Detailed validation error messages
Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'Add some amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
- NestJS - The progressive Node.js framework
- @standard-schema - Standard Schema specification