-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidators.js
78 lines (51 loc) · 1.59 KB
/
validators.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
const { Role, User, Category, Product } = require('../models');
/**** ROLE ***/
const verifyRole = async ( role = '' ) =>{
const roleExists = await Role.findOne({ role });
if ( !roleExists ){
throw new Error(`This role '${ role.toUpperCase() }' is not valid, check it`);
}
}
/**** USER ***/
const verifyEmail = async ( email = '' ) => {
const emailExists = await User.findOne({ email });
if ( emailExists ) {
throw new Error(`This email '${ email.toUpperCase() }' is already registered, check it`);
};
}
const verifyID = async ( id ) => {
const IDExists = await User.findById( id );
if ( !IDExists ) {
throw new Error(`This ID ${ id } not exists, check it`);
};
}
/**** CATEGORY ***/
const verifyCategory = async ( id ) => {
const IDExists = await Category.findById( id );
if ( !IDExists ) {
throw new Error(`This Category ${ id } not exists, check it`);
};
}
/**** PRODUCTS ***/
const verifyProduct = async ( id ) => {
const IDExists = await Product.findById( id );
if ( !IDExists ) {
throw new Error(`This product ${ id } not exists, check it`);
};
}
/**** ALLOWED COLLECTIONS ***/
const allowedCollections = ( collection = '', collections = [] ) => {
const include = collections.includes( collection );
if (!include) {
throw new Error(`The collection ${ collection } is not allowed, the corrects ones are ${ collections }`)
}
return true;
}
module.exports = {
verifyRole,
verifyEmail,
verifyID,
verifyCategory,
verifyProduct,
allowedCollections
}