Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/hodfords-solutions/nestjs-validation
A utility for simplifying validation and providing translated error messages in NestJS applications
https://github.com/hodfords-solutions/nestjs-validation
nestjs nodejs validation
Last synced: 1 day ago
JSON representation
A utility for simplifying validation and providing translated error messages in NestJS applications
- Host: GitHub
- URL: https://github.com/hodfords-solutions/nestjs-validation
- Owner: hodfords-solutions
- Created: 2024-09-23T02:53:53.000Z (4 months ago)
- Default Branch: main
- Last Pushed: 2024-10-28T10:22:09.000Z (3 months ago)
- Last Synced: 2025-01-15T23:49:58.516Z (8 days ago)
- Topics: nestjs, nodejs, validation
- Language: TypeScript
- Homepage: https://opensource.hodfords.uk/nestjs-validation
- Size: 159 KB
- Stars: 49
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
nestjs-validation enhances validation in your NestJS projects by providing a customized ValidationPipe that returns custom error messages. This library simplifies error handling by offering localized and user-friendly responses
## Installation ๐ค
Install the `nestjs-validation` package with:
```bash
npm install @hodfords/nestjs-validation --save
```## Usage ๐
First, create an instance of `ValidationPipe` with the desired configuration:
```typescript
import { ValidationPipe } from '@hodfords/nestjs-validation';
import { ValidateException } from '@hodfords/nestjs-exception';export const validateConfig = new ValidationPipe({
whitelist: true,
stopAtFirstError: true,
forbidUnknownValues: false,
exceptionFactory: (errors): ValidateException => new ValidateException(errors)
});
```Next, set the validation configuration globally in your bootstrap function:
```typescript
async function bootstrap() {
const app = await NestFactory.create(AppModule);
app.useGlobalPipes(validateConfig);
await app.listen(3000);
}
```### Customize Validation Error
The original error message provides basic information but lacks detail. With **nestjs-validation**, you can enhance these errors by adding meaningful context, such as the fieldโs property name, value, and target object.
**Original Validation Error**
```javascript
ValidationError {
target: AppDto { stringValue: undefined },
value: undefined,
property: 'stringValue',
children: [],
constraints: { isString: 'stringValue must be a string' }
}
```**Customized Validation Error**
```javascript
ValidationError {
target: AppDto { stringValue: undefined },
value: undefined,
property: 'stringValue',
children: [],
constraints: {
isString: {
message: '$property must be a string',
detail: { property: 'stringValue', target: 'AppDto', value: undefined }
}
}
}
```### Exception
When combined with [nestjs-exception](https://www.npmjs.com/package/@hodfords/nestjs-exception), errors are translated into localized messages:
```json
{
"message": "Validate Exception",
"errors": {
"stringValue": {
"messages": ["String Value must be a string"]
}
}
}
```## License ๐
This project is licensed under the MIT License