feat: add validators for username, email, password

* not passed yet
This commit is contained in:
Lin, Hung-Shin authored and Annie Huang committed 2023-11-12 21:23:12 +08:00
1 parent 6a64f75950
commit 040b2a71e5
4 files changed
+82 -76

No files matched your search

@@ -0,0 +1,43 @@
import { query } from 'express-validator'
import { isValidUsername } from '@linux-odyssey/utils'
import { User } from '@linux-odyssey/models'
export const checkUsername = () =>
query('username')
.notEmpty()
.isString()
.custom((username) => {
if (!isValidUsername(username)) {
throw new Error('Invalid username')
}
return true
})
.trim()
export const checkNewUsername = () =>
checkUsername().custom(async (username) => {
if (await User.exists({ username })) {
throw new Error('Username taken')
}
return true
})
export const checkEmail = () => query('email').notEmpty().isEmail().trim()
export const checkNewEmail = () =>
checkEmail().custom(async (email) => {
if (await User.exists({ email })) {
throw new Error('Email taken')
}
return true
})
export const checkPassword = () =>
query('password').notEmpty().isStrongPassword({
minLength: 8,
minNumbers: 1,
minLowercase: 1,
minUppercase: 1,
minSymbols: 0,
})