Spaces:
Runtime error
Runtime error
Commit ·
01458b3
1
Parent(s): 3d832f6
add user validator schema
Browse files
src/middlewares/validators/user.validator.ts
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { checkSchema } from 'express-validator';
|
| 2 |
+
import User from '../models/users';
|
| 3 |
+
import Role from '../models/roles';
|
| 4 |
+
|
| 5 |
+
export const validateUser = checkSchema({
|
| 6 |
+
name: {
|
| 7 |
+
notEmpty: {
|
| 8 |
+
errorMessage: 'Name is required',
|
| 9 |
+
},
|
| 10 |
+
},
|
| 11 |
+
email: {
|
| 12 |
+
notEmpty: {
|
| 13 |
+
errorMessage: 'Email is required',
|
| 14 |
+
},
|
| 15 |
+
isEmail: {
|
| 16 |
+
errorMessage: 'Invalid email address',
|
| 17 |
+
},
|
| 18 |
+
custom: {
|
| 19 |
+
options: async (value) => {
|
| 20 |
+
const ifUserExists = await User.findOne({ where: { email: value } });
|
| 21 |
+
if (ifUserExists) {
|
| 22 |
+
return Promise.reject('User already exists');
|
| 23 |
+
}
|
| 24 |
+
},
|
| 25 |
+
},
|
| 26 |
+
},
|
| 27 |
+
password: {
|
| 28 |
+
notEmpty: {
|
| 29 |
+
errorMessage: 'Password is required',
|
| 30 |
+
},
|
| 31 |
+
isLength: {
|
| 32 |
+
options: { min: 8 },
|
| 33 |
+
errorMessage: 'Password must be at least 8 characters long',
|
| 34 |
+
},
|
| 35 |
+
},
|
| 36 |
+
status: {
|
| 37 |
+
notEmpty: {
|
| 38 |
+
errorMessage: 'Status is required',
|
| 39 |
+
},
|
| 40 |
+
},
|
| 41 |
+
role_id: {
|
| 42 |
+
notEmpty: {
|
| 43 |
+
errorMessage: 'Role ID is required',
|
| 44 |
+
},
|
| 45 |
+
custom: {
|
| 46 |
+
options: async (value) => {
|
| 47 |
+
const role = await Role.findByPk(value);
|
| 48 |
+
if (!role) {
|
| 49 |
+
return Promise.reject('Role ID is invalid');
|
| 50 |
+
}
|
| 51 |
+
},
|
| 52 |
+
},
|
| 53 |
+
},
|
| 54 |
+
});
|