Skip to content

Commit b442f55

Browse files
authored
Merge pull request #17 from SecJS/feat/len-add-validators
feat: Add @secjs/validator support in NestJS templates
2 parents 32fce7a + b2a7b87 commit b442f55

16 files changed

+123
-45
lines changed

app/templates/nestjsMongoose/Dtos/__name__Dto.ts.txt

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,17 @@
1-
import * as Joi from 'joi'
21
// import { ApiProperty } from '@nestjs/swagger'
2+
import { <%= namePascal %>Validator } from 'app/Validators/<%= namePascal %>Validator'
33

44
export class Create<%= namePascal %>Dto {
5-
// Schema validation
6-
static schema = Joi.object({
7-
// Props in here
8-
})
5+
static type = 'createSchema'
6+
static validator = <%= namePascal %>Validator
97

108
// @ApiProperty()
119
// <%= namePascal %> Props in here
1210
}
1311

1412
export class Update<%= namePascal %>Dto {
15-
// Schema validation
16-
static schema = Joi.object({
17-
// Props in here
18-
})
13+
static type = 'updateSchema'
14+
static validator = <%= namePascal %>Validator
1915

2016
// @ApiProperty()
2117
// <%= namePascal %> Props in here

app/templates/nestjsMongoose/E2E/__name__/store.spec.ts.txt

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ describe('\n[E2E] <%= namePascal %> 🧱', () => {
2424
expect(body.data.name).toBe(payload.name)
2525
})
2626

27-
// Implement all properties in app/Contracts/Dtos/<%= namePascal %>Dto.ts
27+
// Implement all properties in app/Validators/<%= namePascal %>Validator.ts
2828
// And create tests to validate if the schema validator are working well.
2929

3030
// it('should throw validation errors when trying to create a <%= nameCamel %> with validation errors', async () => {
@@ -38,9 +38,15 @@ describe('\n[E2E] <%= namePascal %> 🧱', () => {
3838
//
3939
// expect(body.status).toBe(status)
4040
// expect(body.error.message).toStrictEqual({
41-
// name: 'Validation Error',
41+
// name: 'Unprocessable Entity Error',
4242
// statusCode: status,
43-
// validations: ['"name" is required'],
43+
// message: [
44+
// {
45+
// field: 'name',
46+
// message: 'required validation failed on name',
47+
// validation: 'required',
48+
// },
49+
// ],
4450
// })
4551
// })
4652
})

app/templates/nestjsMongoose/E2E/__name__/update.spec.ts.txt

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ describe('\n[E2E] <%= namePascal %> 🛠', () => {
5151
})
5252
})
5353

54-
// Implement all properties in app/Contracts/Dtos/<%= namePascal %>Dto.ts
54+
// Implement all properties in app/Validators/<%= namePascal %>Validator.ts
5555
// And create tests to validate if the schema validator are working well.
5656

5757
// it('should throw validation errors when trying to update a <%= nameCamel %> with validation errors', async () => {
@@ -65,9 +65,15 @@ describe('\n[E2E] <%= namePascal %> 🛠', () => {
6565
//
6666
// expect(body.status).toBe(status)
6767
// expect(body.error.message).toStrictEqual({
68-
// name: 'Validation Error',
68+
// name: 'Unprocessable Entity Error',
6969
// statusCode: status,
70-
// validations: ['"name" must be a string'],
70+
// message: [
71+
// {
72+
// field: 'name',
73+
// message: 'string validation failed on name',
74+
// validation: 'string',
75+
// },
76+
// ],
7177
// })
7278
// })
7379
})
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { Injectable } from '@nestjs/common'
2+
import { Validator } from '@secjs/validator'
3+
4+
@Injectable()
5+
export class <%= namePascal %>Validator extends Validator {
6+
createSchema = () => {
7+
return {
8+
name: 'string|required',
9+
}
10+
}
11+
12+
updateSchema = () => {
13+
return {
14+
name: 'string',
15+
}
16+
}
17+
}

app/templates/nestjsPrismaOrm/Dtos/__name__Dto.ts.txt

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,17 @@
1-
import * as Joi from 'joi'
21
// import { ApiProperty } from '@nestjs/swagger'
2+
import { <%= namePascal %>Validator } from 'app/Validators/<%= namePascal %>Validator'
33

44
export class Create<%= namePascal %>Dto {
5-
// Schema validation
6-
static schema = Joi.object({
7-
// Props in here
8-
})
5+
static type = 'createSchema'
6+
static validator = <%= namePascal %>Validator
97

108
// @ApiProperty()
119
// <%= namePascal %> Props in here
1210
}
1311

1412
export class Update<%= namePascal %>Dto {
15-
// Schema validation
16-
static schema = Joi.object({
17-
// Props in here
18-
})
13+
static type = 'updateSchema'
14+
static validator = <%= namePascal %>Validator
1915

2016
// @ApiProperty()
2117
// <%= namePascal %> Props in here

app/templates/nestjsPrismaOrm/E2E/__name__/store.spec.ts.txt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,15 @@ describe('\n[E2E] <%= namePascal %> 🧱', () => {
3838
//
3939
// expect(body.status).toBe(status)
4040
// expect(body.error.message).toStrictEqual({
41-
// name: 'Validation Error',
41+
// name: 'Unprocessable Entity Error',
4242
// statusCode: status,
43-
// validations: ['"name" is required'],
43+
// message: [
44+
// {
45+
// field: 'name',
46+
// message: 'required validation failed on name',
47+
// validation: 'required',
48+
// },
49+
// ],
4450
// })
4551
// })
4652
})

app/templates/nestjsPrismaOrm/E2E/__name__/update.spec.ts.txt

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ describe('\n[E2E] <%= namePascal %> 🛠', () => {
5151
})
5252
})
5353

54-
// Implement all properties in app/Contracts/Dtos/<%= namePascal %>Dto.ts
54+
// Implement all properties in app/Validators/<%= namePascal %>Validator.ts
5555
// And create tests to validate if the schema validator are working well.
5656

5757
// it('should throw validation errors when trying to update a <%= nameCamel %> with validation errors', async () => {
@@ -65,9 +65,15 @@ describe('\n[E2E] <%= namePascal %> 🛠', () => {
6565
//
6666
// expect(body.status).toBe(status)
6767
// expect(body.error.message).toStrictEqual({
68-
// name: 'Validation Error',
68+
// name: 'Unprocessable Entity Error',
6969
// statusCode: status,
70-
// validations: ['"name" must be a string'],
70+
// message: [
71+
// {
72+
// field: 'name',
73+
// message: 'string validation failed on name',
74+
// validation: 'string',
75+
// },
76+
// ],
7177
// })
7278
// })
7379
})
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { Injectable } from '@nestjs/common'
2+
import { Validator } from '@secjs/validator'
3+
4+
@Injectable()
5+
export class <%= namePascal %>Validator extends Validator {
6+
createSchema = () => {
7+
return {
8+
name: 'string|required',
9+
}
10+
}
11+
12+
updateSchema = () => {
13+
return {
14+
name: 'string',
15+
}
16+
}
17+
}

app/templates/nestjsTypeOrm/Dtos/__name__Dto.ts.txt

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,17 @@
1-
import * as Joi from 'joi'
21
// import { ApiProperty } from '@nestjs/swagger'
2+
import { <%= namePascal %>Validator } from 'app/Validators/<%= namePascal %>Validator'
33

44
export class Create<%= namePascal %>Dto {
5-
// Schema validation
6-
static schema = Joi.object({
7-
// Props in here
8-
})
5+
static type = 'createSchema'
6+
static validator = <%= namePascal %>Validator
97

108
// @ApiProperty()
119
// <%= namePascal %> Props in here
1210
}
1311

1412
export class Update<%= namePascal %>Dto {
15-
// Schema validation
16-
static schema = Joi.object({
17-
// Props in here
18-
})
13+
static type = 'updateSchema'
14+
static validator = <%= namePascal %>Validator
1915

2016
// @ApiProperty()
2117
// <%= namePascal %> Props in here

app/templates/nestjsTypeOrm/E2E/__name__/store.spec.ts.txt

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ describe('\n[E2E] <%= namePascal %> 🧱', () => {
2424
expect(body.data.name).toBe(payload.name)
2525
})
2626

27-
// Implement all properties in app/Contracts/Dtos/<%= namePascal %>Dto.ts
27+
// Implement all properties in app/Validators/<%= namePascal %>Validator.ts
2828
// And create tests to validate if the schema validator are working well.
2929

3030
// it('should throw validation errors when trying to create a <%= nameCamel %> with validation errors', async () => {
@@ -38,9 +38,15 @@ describe('\n[E2E] <%= namePascal %> 🧱', () => {
3838
//
3939
// expect(body.status).toBe(status)
4040
// expect(body.error.message).toStrictEqual({
41-
// name: 'Validation Error',
41+
// name: 'Unprocessable Entity Error',
4242
// statusCode: status,
43-
// validations: ['"name" is required'],
43+
// message: [
44+
// {
45+
// field: 'name',
46+
// message: 'required validation failed on name',
47+
// validation: 'required',
48+
// },
49+
// ],
4450
// })
4551
// })
4652
})

0 commit comments

Comments
 (0)