✨ add class-validator & class-transformer
This commit is contained in:
		
							parent
							
								
									3ef5af8667
								
							
						
					
					
						commit
						9e6e36e115
					
				| @ -31,6 +31,8 @@ | |||||||
|     "@nestjs/mapped-types": "^1.2.2", |     "@nestjs/mapped-types": "^1.2.2", | ||||||
|     "@nestjs/platform-express": "^9.0.0", |     "@nestjs/platform-express": "^9.0.0", | ||||||
|     "@prisma/client": "^4.10.1", |     "@prisma/client": "^4.10.1", | ||||||
|  |     "class-transformer": "^0.5.1", | ||||||
|  |     "class-validator": "^0.14.0", | ||||||
|     "nestjs-prisma": "^0.20.0", |     "nestjs-prisma": "^0.20.0", | ||||||
|     "reflect-metadata": "^0.1.13", |     "reflect-metadata": "^0.1.13", | ||||||
|     "rxjs": "^7.2.0" |     "rxjs": "^7.2.0" | ||||||
|  | |||||||
| @ -13,6 +13,8 @@ specifiers: | |||||||
|   '@types/node': 18.11.18 |   '@types/node': 18.11.18 | ||||||
|   '@typescript-eslint/eslint-plugin': ^5.0.0 |   '@typescript-eslint/eslint-plugin': ^5.0.0 | ||||||
|   '@typescript-eslint/parser': ^5.0.0 |   '@typescript-eslint/parser': ^5.0.0 | ||||||
|  |   class-transformer: ^0.5.1 | ||||||
|  |   class-validator: ^0.14.0 | ||||||
|   eslint: ^8.0.1 |   eslint: ^8.0.1 | ||||||
|   eslint-config-prettier: ^8.3.0 |   eslint-config-prettier: ^8.3.0 | ||||||
|   eslint-plugin-prettier: ^4.0.0 |   eslint-plugin-prettier: ^4.0.0 | ||||||
| @ -31,12 +33,14 @@ specifiers: | |||||||
|   webpack: ^5.75.0 |   webpack: ^5.75.0 | ||||||
| 
 | 
 | ||||||
| dependencies: | dependencies: | ||||||
|   '@nestjs/common': 9.3.9_mnr6j2del53muneqly5h4y27ai |   '@nestjs/common': 9.3.9_welcnyot5bzd5wa2aovbkxpi4i | ||||||
|   '@nestjs/config': 2.3.1_jrq2rdgfp2sx67wmylmrqliwxe |   '@nestjs/config': 2.3.1_jrq2rdgfp2sx67wmylmrqliwxe | ||||||
|   '@nestjs/core': 9.3.9_q6agyr4hwia55oswpsa7zjxcpm |   '@nestjs/core': 9.3.9_q6agyr4hwia55oswpsa7zjxcpm | ||||||
|   '@nestjs/mapped-types': 1.2.2_ilxajbd2e536h4oohar7aljlry |   '@nestjs/mapped-types': 1.2.2_sm5si6oczf3vcbfl6qa276t67m | ||||||
|   '@nestjs/platform-express': 9.3.9_77foi4w27ghy47yutmnzv7krjy |   '@nestjs/platform-express': 9.3.9_77foi4w27ghy47yutmnzv7krjy | ||||||
|   '@prisma/client': 4.10.1_prisma@4.10.1 |   '@prisma/client': 4.10.1_prisma@4.10.1 | ||||||
|  |   class-transformer: 0.5.1 | ||||||
|  |   class-validator: 0.14.0 | ||||||
|   nestjs-prisma: 0.20.0_uhhmeuf5jto6tk72f36tv2cdfe |   nestjs-prisma: 0.20.0_uhhmeuf5jto6tk72f36tv2cdfe | ||||||
|   reflect-metadata: 0.1.13 |   reflect-metadata: 0.1.13 | ||||||
|   rxjs: 7.8.0 |   rxjs: 7.8.0 | ||||||
| @ -354,7 +358,7 @@ packages: | |||||||
|       - webpack-cli |       - webpack-cli | ||||||
|     dev: true |     dev: true | ||||||
| 
 | 
 | ||||||
|   /@nestjs/common/9.3.9_mnr6j2del53muneqly5h4y27ai: |   /@nestjs/common/9.3.9_welcnyot5bzd5wa2aovbkxpi4i: | ||||||
|     resolution: {integrity: sha512-GshTD9Xz+wD2em6NyzU4NXw5IXMUmapgDgD+iuj6XL0258hvDwODmNk37mBBnZvTZlqER+krvIUKnS34etqF/A==} |     resolution: {integrity: sha512-GshTD9Xz+wD2em6NyzU4NXw5IXMUmapgDgD+iuj6XL0258hvDwODmNk37mBBnZvTZlqER+krvIUKnS34etqF/A==} | ||||||
|     peerDependencies: |     peerDependencies: | ||||||
|       cache-manager: <=5 |       cache-manager: <=5 | ||||||
| @ -370,6 +374,8 @@ packages: | |||||||
|       class-validator: |       class-validator: | ||||||
|         optional: true |         optional: true | ||||||
|     dependencies: |     dependencies: | ||||||
|  |       class-transformer: 0.5.1 | ||||||
|  |       class-validator: 0.14.0 | ||||||
|       iterare: 1.2.1 |       iterare: 1.2.1 | ||||||
|       reflect-metadata: 0.1.13 |       reflect-metadata: 0.1.13 | ||||||
|       rxjs: 7.8.0 |       rxjs: 7.8.0 | ||||||
| @ -384,7 +390,7 @@ packages: | |||||||
|       reflect-metadata: ^0.1.13 |       reflect-metadata: ^0.1.13 | ||||||
|       rxjs: ^6.0.0 || ^7.2.0 |       rxjs: ^6.0.0 || ^7.2.0 | ||||||
|     dependencies: |     dependencies: | ||||||
|       '@nestjs/common': 9.3.9_mnr6j2del53muneqly5h4y27ai |       '@nestjs/common': 9.3.9_welcnyot5bzd5wa2aovbkxpi4i | ||||||
|       dotenv: 16.0.3 |       dotenv: 16.0.3 | ||||||
|       dotenv-expand: 10.0.0 |       dotenv-expand: 10.0.0 | ||||||
|       lodash: 4.17.21 |       lodash: 4.17.21 | ||||||
| @ -411,7 +417,7 @@ packages: | |||||||
|       '@nestjs/websockets': |       '@nestjs/websockets': | ||||||
|         optional: true |         optional: true | ||||||
|     dependencies: |     dependencies: | ||||||
|       '@nestjs/common': 9.3.9_mnr6j2del53muneqly5h4y27ai |       '@nestjs/common': 9.3.9_welcnyot5bzd5wa2aovbkxpi4i | ||||||
|       '@nestjs/platform-express': 9.3.9_77foi4w27ghy47yutmnzv7krjy |       '@nestjs/platform-express': 9.3.9_77foi4w27ghy47yutmnzv7krjy | ||||||
|       '@nuxtjs/opencollective': 0.3.2 |       '@nuxtjs/opencollective': 0.3.2 | ||||||
|       fast-safe-stringify: 2.1.1 |       fast-safe-stringify: 2.1.1 | ||||||
| @ -425,7 +431,7 @@ packages: | |||||||
|       - encoding |       - encoding | ||||||
|     dev: false |     dev: false | ||||||
| 
 | 
 | ||||||
|   /@nestjs/mapped-types/1.2.2_ilxajbd2e536h4oohar7aljlry: |   /@nestjs/mapped-types/1.2.2_sm5si6oczf3vcbfl6qa276t67m: | ||||||
|     resolution: {integrity: sha512-3dHxLXs3M0GPiriAcCFFJQHoDFUuzTD5w6JDhE7TyfT89YKpe6tcCCIqOZWdXmt9AZjjK30RkHRSFF+QEnWFQg==} |     resolution: {integrity: sha512-3dHxLXs3M0GPiriAcCFFJQHoDFUuzTD5w6JDhE7TyfT89YKpe6tcCCIqOZWdXmt9AZjjK30RkHRSFF+QEnWFQg==} | ||||||
|     peerDependencies: |     peerDependencies: | ||||||
|       '@nestjs/common': ^7.0.8 || ^8.0.0 || ^9.0.0 |       '@nestjs/common': ^7.0.8 || ^8.0.0 || ^9.0.0 | ||||||
| @ -438,7 +444,9 @@ packages: | |||||||
|       class-validator: |       class-validator: | ||||||
|         optional: true |         optional: true | ||||||
|     dependencies: |     dependencies: | ||||||
|       '@nestjs/common': 9.3.9_mnr6j2del53muneqly5h4y27ai |       '@nestjs/common': 9.3.9_welcnyot5bzd5wa2aovbkxpi4i | ||||||
|  |       class-transformer: 0.5.1 | ||||||
|  |       class-validator: 0.14.0 | ||||||
|       reflect-metadata: 0.1.13 |       reflect-metadata: 0.1.13 | ||||||
|     dev: false |     dev: false | ||||||
| 
 | 
 | ||||||
| @ -448,7 +456,7 @@ packages: | |||||||
|       '@nestjs/common': ^9.0.0 |       '@nestjs/common': ^9.0.0 | ||||||
|       '@nestjs/core': ^9.0.0 |       '@nestjs/core': ^9.0.0 | ||||||
|     dependencies: |     dependencies: | ||||||
|       '@nestjs/common': 9.3.9_mnr6j2del53muneqly5h4y27ai |       '@nestjs/common': 9.3.9_welcnyot5bzd5wa2aovbkxpi4i | ||||||
|       '@nestjs/core': 9.3.9_q6agyr4hwia55oswpsa7zjxcpm |       '@nestjs/core': 9.3.9_q6agyr4hwia55oswpsa7zjxcpm | ||||||
|       body-parser: 1.20.1 |       body-parser: 1.20.1 | ||||||
|       cors: 2.8.5 |       cors: 2.8.5 | ||||||
| @ -654,6 +662,10 @@ packages: | |||||||
|       '@types/node': 18.11.18 |       '@types/node': 18.11.18 | ||||||
|     dev: true |     dev: true | ||||||
| 
 | 
 | ||||||
|  |   /@types/validator/13.7.12: | ||||||
|  |     resolution: {integrity: sha512-YVtyAPqpefU+Mm/qqnOANW6IkqKpCSrarcyV269C8MA8Ux0dbkEuQwM/4CjL47kVEM2LgBef/ETfkH+c6+moFA==} | ||||||
|  |     dev: false | ||||||
|  | 
 | ||||||
|   /@typescript-eslint/eslint-plugin/5.52.0_6cfvjsbua5ptj65675bqcn6oza: |   /@typescript-eslint/eslint-plugin/5.52.0_6cfvjsbua5ptj65675bqcn6oza: | ||||||
|     resolution: {integrity: sha512-lHazYdvYVsBokwCdKOppvYJKaJ4S41CgKBcPvyd0xjZNbvQdhn/pnJlGtQksQ/NhInzdaeaSarlBjDXHuclEbg==} |     resolution: {integrity: sha512-lHazYdvYVsBokwCdKOppvYJKaJ4S41CgKBcPvyd0xjZNbvQdhn/pnJlGtQksQ/NhInzdaeaSarlBjDXHuclEbg==} | ||||||
|     engines: {node: ^12.22.0 || ^14.17.0 || >=16.0.0} |     engines: {node: ^12.22.0 || ^14.17.0 || >=16.0.0} | ||||||
| @ -1214,6 +1226,18 @@ packages: | |||||||
|     engines: {node: '>=6.0'} |     engines: {node: '>=6.0'} | ||||||
|     dev: true |     dev: true | ||||||
| 
 | 
 | ||||||
|  |   /class-transformer/0.5.1: | ||||||
|  |     resolution: {integrity: sha512-SQa1Ws6hUbfC98vKGxZH3KFY0Y1lm5Zm0SY8XX9zbK7FJCyVEac3ATW0RIpwzW+oOfmHE5PMPufDG9hCfoEOMw==} | ||||||
|  |     dev: false | ||||||
|  | 
 | ||||||
|  |   /class-validator/0.14.0: | ||||||
|  |     resolution: {integrity: sha512-ct3ltplN8I9fOwUd8GrP8UQixwff129BkEtuWDKL5W45cQuLd19xqmTLu5ge78YDm/fdje6FMt0hGOhl0lii3A==} | ||||||
|  |     dependencies: | ||||||
|  |       '@types/validator': 13.7.12 | ||||||
|  |       libphonenumber-js: 1.10.19 | ||||||
|  |       validator: 13.9.0 | ||||||
|  |     dev: false | ||||||
|  | 
 | ||||||
|   /clean-stack/2.2.0: |   /clean-stack/2.2.0: | ||||||
|     resolution: {integrity: sha512-4diC9HaTE+KRAMWhDhrGOECgWZxoevMc5TlkObMqNSsVU62PYzXZ/SMTjzyGAFF1YusgxGcSWTEXBhp0CPwQ1A==} |     resolution: {integrity: sha512-4diC9HaTE+KRAMWhDhrGOECgWZxoevMc5TlkObMqNSsVU62PYzXZ/SMTjzyGAFF1YusgxGcSWTEXBhp0CPwQ1A==} | ||||||
|     engines: {node: '>=6'} |     engines: {node: '>=6'} | ||||||
| @ -2284,6 +2308,10 @@ packages: | |||||||
|       type-check: 0.4.0 |       type-check: 0.4.0 | ||||||
|     dev: true |     dev: true | ||||||
| 
 | 
 | ||||||
|  |   /libphonenumber-js/1.10.19: | ||||||
|  |     resolution: {integrity: sha512-MDZ1zLIkfSDZV5xBta3nuvbEOlsnKCPe4z5r3hyup/AXveevkl9A1eSWmLhd2FX4k7pJDe4MrLeQsux0HI/VWg==} | ||||||
|  |     dev: false | ||||||
|  | 
 | ||||||
|   /lilconfig/2.0.6: |   /lilconfig/2.0.6: | ||||||
|     resolution: {integrity: sha512-9JROoBW7pobfsx+Sq2JsASvCo6Pfo6WWoUW79HuB1BCoBXD4PLWJPqDF6fNj67pqBYTbAHkE57M1kS/+L1neOg==} |     resolution: {integrity: sha512-9JROoBW7pobfsx+Sq2JsASvCo6Pfo6WWoUW79HuB1BCoBXD4PLWJPqDF6fNj67pqBYTbAHkE57M1kS/+L1neOg==} | ||||||
|     engines: {node: '>=10'} |     engines: {node: '>=10'} | ||||||
| @ -2541,7 +2569,7 @@ packages: | |||||||
|     dependencies: |     dependencies: | ||||||
|       '@angular-devkit/core': 13.3.10 |       '@angular-devkit/core': 13.3.10 | ||||||
|       '@angular-devkit/schematics': 13.3.10 |       '@angular-devkit/schematics': 13.3.10 | ||||||
|       '@nestjs/common': 9.3.9_mnr6j2del53muneqly5h4y27ai |       '@nestjs/common': 9.3.9_welcnyot5bzd5wa2aovbkxpi4i | ||||||
|       '@prisma/client': 4.10.1_prisma@4.10.1 |       '@prisma/client': 4.10.1_prisma@4.10.1 | ||||||
|       '@schematics/angular': 13.3.10 |       '@schematics/angular': 13.3.10 | ||||||
|       prisma: 4.10.1 |       prisma: 4.10.1 | ||||||
| @ -3477,6 +3505,11 @@ packages: | |||||||
|     resolution: {integrity: sha512-wa7YjyUGfNZngI/vtK0UHAN+lgDCxBPCylVXGp0zu59Fz5aiGtNXaq3DhIov063MorB+VfufLh3JlF2KdTK3xg==} |     resolution: {integrity: sha512-wa7YjyUGfNZngI/vtK0UHAN+lgDCxBPCylVXGp0zu59Fz5aiGtNXaq3DhIov063MorB+VfufLh3JlF2KdTK3xg==} | ||||||
|     dev: true |     dev: true | ||||||
| 
 | 
 | ||||||
|  |   /validator/13.9.0: | ||||||
|  |     resolution: {integrity: sha512-B+dGG8U3fdtM0/aNK4/X8CXq/EcxU2WPrPEkJGslb47qyHsxmbggTWK0yEA4qnYVNF+nxNlN88o14hIcPmSIEA==} | ||||||
|  |     engines: {node: '>= 0.10'} | ||||||
|  |     dev: false | ||||||
|  | 
 | ||||||
|   /vary/1.1.2: |   /vary/1.1.2: | ||||||
|     resolution: {integrity: sha512-BNGbWLfd0eUPabhkXUVm0j8uuvREyTh5ovRa/dyow/BqAbZJyC+5fU+IzQOzmAKzYqYRAISoRhdQr3eIZ/PXqg==} |     resolution: {integrity: sha512-BNGbWLfd0eUPabhkXUVm0j8uuvREyTh5ovRa/dyow/BqAbZJyC+5fU+IzQOzmAKzYqYRAISoRhdQr3eIZ/PXqg==} | ||||||
|     engines: {node: '>= 0.8'} |     engines: {node: '>= 0.8'} | ||||||
|  | |||||||
| @ -1,4 +1,5 @@ | |||||||
| import { HttpAdapterHost, NestFactory } from '@nestjs/core' | import { HttpAdapterHost, NestFactory } from '@nestjs/core' | ||||||
|  | import { ValidationPipe } from '@nestjs/common' | ||||||
| import { ConfigService } from '@nestjs/config' | import { ConfigService } from '@nestjs/config' | ||||||
| import { PrismaClientExceptionFilter, PrismaService } from 'nestjs-prisma' | import { PrismaClientExceptionFilter, PrismaService } from 'nestjs-prisma' | ||||||
| import { AppModule } from './app.module' | import { AppModule } from './app.module' | ||||||
| @ -6,6 +7,9 @@ import { AppModule } from './app.module' | |||||||
| async function bootstrap() { | async function bootstrap() { | ||||||
|   const app = await NestFactory.create(AppModule) |   const app = await NestFactory.create(AppModule) | ||||||
| 
 | 
 | ||||||
|  |   // Validation
 | ||||||
|  |   app.useGlobalPipes(new ValidationPipe({ whitelist: true })) | ||||||
|  | 
 | ||||||
|   // enable shutdown hook
 |   // enable shutdown hook
 | ||||||
|   const prismaService = app.get(PrismaService) |   const prismaService = app.get(PrismaService) | ||||||
|   await prismaService.enableShutdownHooks(app) |   await prismaService.enableShutdownHooks(app) | ||||||
|  | |||||||
							
								
								
									
										24
									
								
								src/users/dto/create-user.dto.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										24
									
								
								src/users/dto/create-user.dto.ts
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,24 @@ | |||||||
|  | import { | ||||||
|  |   IsNotEmpty, | ||||||
|  |   IsOptional, | ||||||
|  |   IsString, | ||||||
|  |   Length, | ||||||
|  |   IsEmail, | ||||||
|  |   IsStrongPassword, | ||||||
|  | } from 'class-validator' | ||||||
|  | 
 | ||||||
|  | export class CreateUserDto { | ||||||
|  |   @IsNotEmpty() | ||||||
|  |   @IsEmail() | ||||||
|  |   email: string | ||||||
|  | 
 | ||||||
|  |   @IsOptional() | ||||||
|  |   @IsString() | ||||||
|  |   @Length(5, 20) | ||||||
|  |   username?: string | ||||||
|  | 
 | ||||||
|  |   @IsString() | ||||||
|  |   @IsNotEmpty() | ||||||
|  |   @IsStrongPassword() | ||||||
|  |   password: string | ||||||
|  | } | ||||||
| @ -1,5 +1,6 @@ | |||||||
| import { Controller, Get, Post, Body, Param } from '@nestjs/common' | import { Controller, Get, Post, Body, Param } from '@nestjs/common' | ||||||
| import { UsersService } from './users.service' | import { UsersService } from './users.service' | ||||||
|  | import { CreateUserDto } from './dto/create-user.dto' | ||||||
| 
 | 
 | ||||||
| @Controller() | @Controller() | ||||||
| export class UsersController { | export class UsersController { | ||||||
| @ -16,9 +17,7 @@ export class UsersController { | |||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   @Post('users') |   @Post('users') | ||||||
|   async createUser( |   async createUser(@Body() userData: CreateUserDto) { | ||||||
|     @Body() userData: { email: string; password: string; username?: string }, |  | ||||||
|   ) { |  | ||||||
|     return this.userService.createUser(userData) |     return this.userService.createUser(userData) | ||||||
|   } |   } | ||||||
| } | } | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user