Skip to content

Commit

Permalink
feat(validator): add validation to update route
Browse files Browse the repository at this point in the history
  • Loading branch information
jlenon7 committed May 8, 2024
1 parent 5a7f8c1 commit 335d46f
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 6 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@
"#src/middlewares/auth.middleware",
"#src/validators/login.validator",
"#src/validators/register.validator",
"#src/validators/update.validator",
"#src/interceptors/response.interceptor",
"#src/middlewares/pagination.middleware"
]
Expand Down
14 changes: 14 additions & 0 deletions src/providers/validator.provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { ValidationException } from '#src/exceptions/validation.exception'
type UniqueOptions = {
table: string
column?: string
max?: number
}

declare module '@vinejs/vine' {
Expand Down Expand Up @@ -43,6 +44,19 @@ export default class ValidatorProvider extends ServiceProvider {
options.column = field.name as string
}

if (options.max) {
const rows = await Database.table(options.table)
.select(options.column)
.where(options.column, value)
.findMany()

if (rows.length > options.max) {
field.report('The {{ field }} field is not unique', 'unique', field)
}

return
}

const existsRow = await Database.table(options.table)
.select(options.column)
.where(options.column, value)
Expand Down
4 changes: 3 additions & 1 deletion src/routes/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ Route.group(() => {
Route.group(() => {
Route.get('users', 'UserController.index').middleware('pagination')
Route.get('users/:id', 'UserController.show')
Route.put('users/:id', 'UserController.update')
Route.put('users/:id', 'UserController.update').middleware(
'update:validator'
)
Route.delete('users/:id', 'UserController.delete')
}).name('users')
}).middleware('auth')
Expand Down
2 changes: 0 additions & 2 deletions src/services/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ export class UserService {

switch (`${isEmailEqual}:${isPasswordEqual}`) {
case 'false:true':
// TODO Validate that email isn't already registered.
await Queue.queue('user:email').then(q =>
q.add({ user, token, email: data.email })
)
Expand All @@ -75,7 +74,6 @@ export class UserService {
)
break
case 'false:false':
// TODO Validate that email isn't already registered.
data.password = await bcrypt.hash(data.password, 10)

await Queue.queue('user:email:password').then(q =>
Expand Down
31 changes: 31 additions & 0 deletions src/validators/update.validator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { Middleware, type Context } from '@athenna/http'
import { BaseValidator } from '#src/validators/base.validator'

@Middleware({ name: 'update:validator' })
export class UpdateValidator extends BaseValidator {
public schema = this.validator.object({
name: this.validator.string().optional(),
email: this.validator
.string()
.email()
.unique({ table: 'users', max: 1 })
.optional(),
password: this.validator
.string()
.minLength(8)
.maxLength(32)
.confirmed()
.optional()
})

public async handle({ request }: Context) {
const data = request.only([
'name',
'email',
'password',
'password_confirmation'
])

await this.validate(data)
}
}
2 changes: 1 addition & 1 deletion storage/queues.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"default":[],"deadletter":[],"user:email":[],"user:password":[{"user":{"name":"João Lenon","email":"Darlene.Wilkinson@gmail.com"},"password":"$2b$10$gTXEZsXmkLJhOvvge3t/IO99RUw1AD6NmddfQ3mPByHweP3hWJdoO"}],"user:email:password":[],"user:confirm":[]}
{"default":[],"deadletter":[],"user:email":[],"user:password":[{"user":{"name":"João Lenon","email":"Monique_Johnson@yahoo.com"},"password":"$2b$10$utsMOdfjDFKeEdD6UKgXieeGgzh/KtoLPq0Q7il4Qlsvr7/IC23Qi"}],"user:email:password":[],"user:confirm":[]}
9 changes: 7 additions & 2 deletions tests/e2e/user.controller.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ export default class UserControllerTest extends BaseHttpTest {
const user = await User.find({ email: '[email protected]' })
const token = await ioc.use('authService').login('[email protected]', '12345')
const response = await request.put(`/api/v1/users/${user.id}`, {
body: { name: 'Customer Updated', password: '123456' },
body: { name: 'Customer Updated', password: '12345678', password_confirmation: '12345678' },
headers: { authorization: token }
})

Expand All @@ -192,7 +192,12 @@ export default class UserControllerTest extends BaseHttpTest {
const user = await User.find({ email: '[email protected]' })
const token = await ioc.use('authService').login('[email protected]', '12345')
const response = await request.put(`/api/v1/users/${user.id}`, {
body: { name: 'Customer Updated', email: '[email protected]', password: '123456' },
body: {
name: 'Customer Updated',
email: '[email protected]',
password: '12345678',
password_confirmation: '12345678'
},
headers: { authorization: token }
})

Expand Down

0 comments on commit 335d46f

Please sign in to comment.