Fastest-Validator module for Nest.JS based on the fastest-validator package.
- This package is deprecated and will be removed. Use this instead: https://www.npmjs.com/package/@fizbix/nest-fastest-validator
Changes in version 2.0.0:
- New decorator
@Shorthand()
- for shorthand validation e.g.@Shorthand('string | min:6 | max:32')
- Now you can extend classes with
@ValidationSchema()
decorator - properies will be inherited Validator
class for manual validation in services, controllers etc. To use it, injectValidator
class in your class constructor@InjectValidator(ValidationSchema)
and usevalidate()
,validateSync()
orvalidateReactive()
method.- You can inject root
FastestValidator
class in your class constructor. Just use@InjectFastestValidator()
decorator - Now instead function
decoratorFactory
you can usecreate
method inFactoryDecorator
class. It's the same, but you can use it to create your own decorators.
$ npm install @nest-up/nest-fastest-validator fastest-validator class-transformer
Only import the NestFastestValidatorModule in your app root module:
@Module({
imports: [
FastestValidatorModule.forRoot({
useNewCustomCheckerFunction: true
})
]
})
class AppModule {}
1. UseFactory
@Module({
imports: [
FastestValidatorModule.forRootAsync({
useFactory: () => ({
useNewCustomCheckerFunction: true
})
})
]
})
class AppModule {}
2. Use class
@Injectable()
class FastestValidatorConfig implements IFastestValidatorModuleOptionsFactory {
public createFastestValidatorModuleOptions(): TFastestValidatorModuleOptions {
return {
useNewCustomCheckerFunction: true
};
}
}
@Module({
imports: [
FastestValidatorModule.forRootAsync({
useClass: FastestValidatorConfig
})
]
})
class AppModule {}
After module configuration you can define your validation schemas:
NOTE: ValidationSchema
,String
,Number
and Time
are decorators taken from the @nest-up/nest-fastest-validator
package
@ValidationSchema()
class ProductDTO {
@Shorthand('string | min:2 | max:10 ')
public name: string;
@Number({
integer: false,
positive: true
})
public price: number;
@Time({
nullable: false
})
public createdAt: Date;
@Shorthand('string[]')
public tags: string[];
}
Now just prepare your controller:
NOTE: The FastestValidatorPipe is imported from "@nest-up/nest-fastest-validator" package
@Controller('/products')
@UsePipes(FastestValidatorPipe)
class ProductsController {
@Post('/create')
public createNewProduct(@Body() productDTO: ProductDTO) {
/// ...
}
}
Now if we send request with invalid body properties - the following error will be returned to us
{
"statusCode": 400,
"error": "Validation failed",
"messages": [
{
"field": "name",
"message": "The 'name' field is required."
},
{
"field": "price",
"message": "The 'price' field is required."
},
{
"field": "createdAt",
"message": "The 'createdAt' field is required."
}
]
}
We can also inject validator class to our controller and use it to validate our data:
@Controller('/products')
class ProductsController {
constructor(@InjectValidator(ProductDTO) private readonly validator: Validator) {}
@Post('/create')
public createNewProduct(@Body() productDTO: ProductDTO) {
this.validator.validate(productDTO);
/// ...
}
}
Of course you can can configure FastestValidatorPipe
options:
@UsePipes(
new FastestValidatorPipe({
transformToClass: true,
disableValidationErrorMessages: true,
httpErrorStatusCode: 404
})
)
You can also use FastestValidatorPipe
globally
async function bootstrap() {
const app = await NestFactory.create(AppModule);
app.useGlobalPipes(
new FastestValidatorPipe({
disableValidationErrorMessages: true
})
);
await app.listen(3000);
}
bootstrap();
All decorators accept an object of options that apply to the type being used, for a full list of options please refer to the fastest-validator documentation.
Licensed under the MIT license.