Ver código fonte

:recycle: trying to name every backend Joi model in use | consolidate some schemas

tags/0.0.1
J 4 anos atrás
pai
commit
4585c1fa80

+ 3
- 2
backend/lib/routes/membership/active.js Ver arquivo

@@ -4,6 +4,7 @@ const Joi = require('joi')
4 4
 const apiSchema = require('../../schemas/api')
5 5
 const errorSchema = require('../../schemas/errors')
6 6
 const groupingSchema = require('../../schemas/groupings')
7
+const params = require('../../schemas/params')
7 8
 
8 9
 const pluginConfig = {
9 10
     handlerType: 'grouping',
@@ -18,7 +19,7 @@ const validators = {
18 19
     // headers: true,
19 20
 
20 21
     /** Validate the route params (/active/{thing}) */
21
-    params: Joi.object({ profile_id: Joi.number() }),
22
+    params: params.profileId,
22 23
 
23 24
     /** Validate the route query (/active/{thing}?limit=10&offset=10) */
24 25
     query: Joi.object({ type: Joi.string().lowercase().min(6).max(11) }),
@@ -100,7 +101,7 @@ module.exports = {
100 101
         response: {
101 102
             schema: apiSchema.single.append({
102 103
                 data: responseSchemas.list
103
-            })
104
+            }).label('grouping_list_res')
104 105
         },
105 106
     },
106 107
 }

+ 6
- 5
backend/lib/routes/membership/join.js Ver arquivo

@@ -3,6 +3,7 @@ const Joi = require('joi')
3 3
 const apiSchema = require('../../schemas/api')
4 4
 const errorSchema = require('../../schemas/errors')
5 5
 const groupingSchema = require('../../schemas/groupings')
6
+const params = require('../../schemas/params')
6 7
 
7 8
 const pluginConfig = {
8 9
     handlerType: 'grouping',
@@ -13,18 +14,18 @@ const pluginConfig = {
13 14
 }
14 15
 
15 16
 const validators = {
16
-    params: Joi.object({ profile_id: Joi.number() }),
17
+    params: params.profileId,
17 18
     payload: groupingSchema.single.append({
18 19
         target_id: Joi.number().required(),
19 20
         role: Joi.string(),
20
-    })
21
+    }).label('grouping_membership_single')
21 22
 }
22 23
 
23 24
 const responseSchemas = {
24 25
     response: Joi.object({
25 26
         memberships: Joi.array().items(),
26 27
         hasMatch: Joi.boolean()
27
-    }),
28
+    }).label('grouping_membership_list'),
28 29
     error: errorSchema.single
29 30
 }
30 31
 
@@ -106,10 +107,10 @@ module.exports = {
106 107
             status: {
107 108
                 200: apiSchema.single.append({
108 109
                     data: responseSchemas.response,
109
-                }),
110
+                }).label('join_grouping_res'),
110 111
                 409: apiSchema.single.append({
111 112
                     data: responseSchemas.error,
112
-                }),
113
+                }).label('error_single_res'),
113 114
             },
114 115
         },
115 116
     },

+ 4
- 4
backend/lib/routes/notification/index.js Ver arquivo

@@ -1,6 +1,8 @@
1 1
 const Joi = require('joi')
2 2
 const apiSchema = require('../../schemas/api')
3 3
 const errorSchema = require('../../schemas/errors')
4
+const params = require('../../schemas/params')
5
+
4 6
 const Stream = require('stream')
5 7
 const PassThrough = require('stream').PassThrough
6 8
 
@@ -13,9 +15,7 @@ const pluginConfig = {
13 15
 }
14 16
 
15 17
 const validators = {
16
-    params: Joi.object({
17
-        profile_id: Joi.number(),
18
-    })
18
+    params: params.profileId
19 19
 }
20 20
 
21 21
 module.exports = {
@@ -44,7 +44,7 @@ module.exports = {
44 44
         },
45 45
 
46 46
         /** Validate based on validators object */
47
-         validate: {
47
+        validate: {
48 48
             ...validators,
49 49
             failAction: 'log'
50 50
         },

+ 4
- 3
backend/lib/routes/profile/get.js Ver arquivo

@@ -4,6 +4,7 @@ const Joi = require('joi')
4 4
 const apiSchema = require('../../schemas/api')
5 5
 const errorSchema = require('../../schemas/errors')
6 6
 const profileSchema = require('../../schemas/profiles')
7
+const params = require('../../schemas/params')
7 8
 
8 9
 const pluginConfig = {
9 10
     handlerType: 'profile',
@@ -19,7 +20,7 @@ const responseSchemas = {
19 20
 }
20 21
 
21 22
 const validators = {
22
-    params: Joi.object({ profile_id: Joi.number() }),
23
+    params: params.profileId
23 24
 }
24 25
 
25 26
 module.exports = {
@@ -66,10 +67,10 @@ module.exports = {
66 67
             status: {
67 68
                 200: apiSchema.single.append({
68 69
                     data: responseSchemas.profile,
69
-                }),
70
+                }).label('profile_single_res'),
70 71
                 409: apiSchema.single.append({
71 72
                     data: responseSchemas.error,
72
-                }),
73
+                }).label('error_single_res'),
73 74
             },
74 75
         },
75 76
     },

+ 2
- 2
backend/lib/routes/profile/match.js Ver arquivo

@@ -68,10 +68,10 @@ module.exports = {
68 68
             status: {
69 69
                 200: apiSchema.single.append({
70 70
                     data: responseSchemas.response,
71
-                }),
71
+                }).label('response_list_res'),
72 72
                 409: apiSchema.single.append({
73 73
                     data: responseSchemas.error,
74
-                })
74
+                }).label('error_single_res')
75 75
             },
76 76
         },
77 77
     },

+ 6
- 5
backend/lib/routes/profile/patch-queue.js Ver arquivo

@@ -4,6 +4,7 @@ const Joi = require('joi')
4 4
 const apiSchema = require('../../schemas/api')
5 5
 const errorSchema = require('../../schemas/errors')
6 6
 const profileSchema = require('../../schemas/profiles')
7
+const params = require('../../schemas/params')
7 8
 
8 9
 const pluginConfig = {
9 10
     handlerType: 'profile',
@@ -16,7 +17,7 @@ const pluginConfig = {
16 17
 const responseSchemas = {
17 18
     response: Joi.array().items(
18 19
         Joi.alternatives().try(
19
-            Joi.number(),
20
+            params.profileId,
20 21
             profileSchema.single,
21 22
         ),
22 23
     ),
@@ -24,8 +25,8 @@ const responseSchemas = {
24 25
 }
25 26
 
26 27
 const validators = {
27
-    params: Joi.object({ profile_id: Joi.number(), target_id: Joi.number() }),
28
-    query: Joi.object({ include_profile: Joi.bool(), reinsert: Joi.boolean() }),
28
+    params: params.profileId.append({ target_id: Joi.number() }),
29
+    query: params.profileInclude.append({ reinsert: Joi.boolean() }),
29 30
 }
30 31
 
31 32
 module.exports = {
@@ -80,10 +81,10 @@ module.exports = {
80 81
             status: {
81 82
                 200: apiSchema.single.append({
82 83
                     data: responseSchemas.response,
83
-                }),
84
+                }).label('match_queue_res'),
84 85
                 409: apiSchema.single.append({
85 86
                     data: responseSchemas.error,
86
-                })
87
+                }).label('error_single_res')
87 88
             },
88 89
         },
89 90
     },

+ 6
- 5
backend/lib/routes/profile/queue.js Ver arquivo

@@ -4,6 +4,7 @@ const Joi = require('joi')
4 4
 const apiSchema = require('../../schemas/api')
5 5
 const errorSchema = require('../../schemas/errors')
6 6
 const profileSchema = require('../../schemas/profiles')
7
+const params = require('../../schemas/params')
7 8
 
8 9
 const pluginConfig = {
9 10
     handlerType: 'profile',
@@ -16,7 +17,7 @@ const pluginConfig = {
16 17
 const responseSchemas = {
17 18
     response: Joi.array().items(
18 19
         Joi.alternatives().try(
19
-            Joi.number(),
20
+            params.profileId,
20 21
             profileSchema.single,
21 22
         )
22 23
     ),
@@ -24,8 +25,8 @@ const responseSchemas = {
24 25
 }
25 26
 
26 27
 const validators = {
27
-    params: Joi.object({ profile_id: Joi.number() }),
28
-    query: Joi.object({ include_profile: Joi.bool() }),
28
+    params: params.profileId,
29
+    query: params.profileInclude,
29 30
 }
30 31
 
31 32
 module.exports = {
@@ -85,10 +86,10 @@ module.exports = {
85 86
             status: {
86 87
                 200: apiSchema.single.append({
87 88
                     data: responseSchemas.response,
88
-                }),
89
+                }).label('match_queue_res'),
89 90
                 409: apiSchema.single.append({
90 91
                     data: responseSchemas.error,
91
-                })
92
+                }).label('error_single_res')
92 93
             },
93 94
         },
94 95
     },

+ 4
- 5
backend/lib/routes/profile/respond.js Ver arquivo

@@ -4,6 +4,7 @@ const Joi = require('joi')
4 4
 const apiSchema = require('../../schemas/api')
5 5
 const errorSchema = require('../../schemas/errors')
6 6
 const surveyResponseSchema = require('../../schemas/responses')
7
+const params = require('../../schemas/params')
7 8
 
8 9
 const pluginConfig = {
9 10
     handlerType: 'profile',
@@ -23,9 +24,7 @@ const validators = {
23 24
     // headers: true,
24 25
 
25 26
     /** Validate the route params (/active/{thing}) */
26
-    params: Joi.object({
27
-        profile_id: Joi.number(),
28
-    }),
27
+    params: params.profileId,
29 28
 
30 29
     /** Validate the route query (/active/{thing}?limit=10&offset=10) */
31 30
     query: Joi.object({
@@ -94,10 +93,10 @@ module.exports = {
94 93
             status: {
95 94
                 201: apiSchema.single.append({
96 95
                     data: responseSchemas.response,
97
-                }),
96
+                }).label('response_list_res'),
98 97
                 409: apiSchema.single.append({
99 98
                     data: responseSchemas.error,
100
-                })
99
+                }).label('error_single_res')
101 100
             },
102 101
         },
103 102
     },

+ 6
- 6
backend/lib/routes/profile/score.js Ver arquivo

@@ -3,6 +3,8 @@
3 3
 const Joi = require('joi')
4 4
 const apiSchema = require('../../schemas/api')
5 5
 const errorSchema = require('../../schemas/errors')
6
+const params = require('../../schemas/params')
7
+const profileSchema = require('../../schemas/profiles')
6 8
 
7 9
 const pluginConfig = {
8 10
     handlerType: 'score',
@@ -17,9 +19,7 @@ const validators = {
17 19
     // headers: true,
18 20
 
19 21
     /** Validate the route params (/active/{thing}) */
20
-    params: Joi.object({
21
-        profile_id: Joi.number(),
22
-    }),
22
+    params: params.profileId,
23 23
 
24 24
     /** Validate the route query (/active/{thing}?limit=10&offset=10) */
25 25
     query: Joi.object({
@@ -31,7 +31,7 @@ const validators = {
31 31
 }
32 32
 
33 33
 const responseSchemas = {
34
-    response: Joi.array().items(),
34
+    response: profileSchema.list,
35 35
     error: errorSchema.single
36 36
 }
37 37
 
@@ -97,10 +97,10 @@ module.exports = {
97 97
             status: {
98 98
                 200: apiSchema.single.append({
99 99
                     data: responseSchemas.response,
100
-                }),
100
+                }).label('profile_list_res'),
101 101
                 409: apiSchema.single.append({
102 102
                     data: responseSchemas.error,
103
-                })
103
+                }).label('error_single_res')
104 104
             },
105 105
         },
106 106
     },

+ 4
- 3
backend/lib/routes/profile/update.js Ver arquivo

@@ -4,6 +4,7 @@ const Joi = require('joi')
4 4
 const apiSchema = require('../../schemas/api')
5 5
 const errorSchema = require('../../schemas/errors')
6 6
 const surveyResponseSchema = require('../../schemas/responses')
7
+const params = require('../../schemas/params')
7 8
 
8 9
 const pluginConfig = {
9 10
     handlerType: 'profile',
@@ -23,7 +24,7 @@ const validators = {
23 24
     // headers: true,
24 25
 
25 26
     /** Validate the route params (/active/{thing}) */
26
-    params: Joi.object({ profile_id: Joi.number() }),
27
+    params: params.profileId,
27 28
 
28 29
     /** Validate the route query (/active/{thing}?limit=10&offset=10) */
29 30
     // query: true,
@@ -87,10 +88,10 @@ module.exports = {
87 88
             status: {
88 89
                 200: apiSchema.single.append({
89 90
                     data: responseSchemas.response,
90
-                }),
91
+                }).label('response_list_res'),
91 92
                 409: apiSchema.single.append({
92 93
                     data: responseSchemas.error,
93
-                })
94
+                }).label('error_single_res')
94 95
             },
95 96
         },
96 97
     },

+ 1
- 1
backend/lib/routes/survey/questions.js Ver arquivo

@@ -61,7 +61,7 @@ module.exports = {
61 61
         response: {
62 62
             schema: apiSchema.single.append({
63 63
                 data: responseSchemas.response
64
-            }),
64
+            }).label('question_list_res'),
65 65
             failAction: 'log',
66 66
         },
67 67
     },

+ 3
- 2
backend/lib/routes/survey/responses.js Ver arquivo

@@ -3,6 +3,7 @@
3 3
 const Joi = require('joi')
4 4
 const apiSchema = require('../../schemas/api')
5 5
 const surveyResponseSchema = require('../../schemas/responses')
6
+const params = require('../../schemas/params')
6 7
 
7 8
 const pluginConfig = {
8 9
     handlerType: 'survey',
@@ -17,7 +18,7 @@ const validators = {
17 18
     /** Validate the header (cookie check) */
18 19
     // headers: true,
19 20
     /** Validate the route params (/active/{thing}) */
20
-    params: Joi.object({ user_id: Joi.number() }),
21
+    params: params.userId,
21 22
     /** Validate the route query (/active/{thing}?limit=10&offset=10) */
22 23
     // query: true,
23 24
     /** Validate the incoming payload (POST method) */
@@ -61,7 +62,7 @@ module.exports = {
61 62
         response: {
62 63
             schema: apiSchema.single.append({
63 64
                 data: responseSchemas.response
64
-            }),
65
+            }).label('response_list_res'),
65 66
             failAction: 'log',
66 67
         },
67 68
     },

+ 5
- 9
backend/lib/routes/user/create-profile.js Ver arquivo

@@ -2,6 +2,7 @@
2 2
 
3 3
 const Joi = require('joi')
4 4
 const errorSchema = require('../../schemas/errors')
5
+const params = require('../../schemas/params')
5 6
 
6 7
 const pluginConfig = {
7 8
     handlerType: 'user',
@@ -16,7 +17,7 @@ const validators = {
16 17
     // headers: true,
17 18
 
18 19
     /** Validate the route params (/active/{thing}) */
19
-    params: Joi.object({ user_id: Joi.number() }),
20
+    params: params.userId,
20 21
 
21 22
     /** Validate the route query (/active/{thing}?limit=10&offset=10) */
22 23
     // query: true,
@@ -33,13 +34,8 @@ const responseSchemas = {
33 34
     response: Joi.object({
34 35
         profile_id: Joi.number(),
35 36
         user_id: Joi.number(),
36
-        // HELP: not sure if this is right, but attempting to fix
37
-        // ValidationError data[0].user_name is not allowed
38
-        // the logic being that CompleteProfile has had user_name added
39
-        // and getCompleteProfiles utilizes CompleteProfile
40
-        // and this route utilizes getCompleteProfiles
41 37
         user_name: Joi.string(),
42
-    }),
38
+    }).label('created_profile'),
43 39
     error: errorSchema.single
44 40
 }
45 41
 
@@ -107,12 +103,12 @@ module.exports = {
107 103
                     ok: Joi.bool(),
108 104
                     handler: Joi.string(),
109 105
                     data: responseSchemas.response,
110
-                }),
106
+                }).label('created_profile_res'),
111 107
                 409: Joi.object({
112 108
                     ok: Joi.bool(),
113 109
                     handler: Joi.string(),
114 110
                     data: responseSchemas.error,
115
-                }),
111
+                }).label('error_single_res'),
116 112
             },
117 113
         },
118 114
     },

+ 3
- 5
backend/lib/routes/user/current.js Ver arquivo

@@ -1,6 +1,7 @@
1 1
 'use strict'
2 2
 
3 3
 const Joi = require('joi')
4
+const params = require('../../schemas/params')
4 5
 
5 6
 const pluginConfig = {
6 7
     handlerType: 'user',
@@ -15,10 +16,7 @@ const pluginConfig = {
15 16
 /** Validator functions by request method */
16 17
 const validators = {
17 18
     get: {
18
-        params: Joi.object({
19
-            name: Joi.string().min(3).max(11),
20
-            all: Joi.array(),
21
-        }),
19
+        params: params.userName,
22 20
     },
23 21
 }
24 22
 
@@ -62,7 +60,7 @@ module.exports = {
62 60
                 ok: Joi.bool(),
63 61
                 handler: Joi.string(),
64 62
                 data: validators.get.params,
65
-            }),
63
+            }).label('user_name_res'),
66 64
             failAction: 'log',
67 65
         },
68 66
     },

+ 7
- 26
backend/lib/routes/user/list-profiles.js Ver arquivo

@@ -2,6 +2,8 @@
2 2
 
3 3
 const Joi = require('joi')
4 4
 const errorSchema = require('../../schemas/errors')
5
+const profileSchema = require('../../schemas/profiles')
6
+const params = require('../../schemas/params')
5 7
 
6 8
 const pluginConfig = {
7 9
     handlerType: 'user',
@@ -16,9 +18,7 @@ const validators = {
16 18
     // headers: true,
17 19
 
18 20
     /** Validate the route params (/active/{thing}) */
19
-    params: Joi.object({
20
-        user_id: Joi.number(),
21
-    }),
21
+    params: params.userId,
22 22
 
23 23
     /** Validate the route query (/active/{thing}?limit=10&offset=10) */
24 24
     // query: true,
@@ -27,26 +27,7 @@ const validators = {
27 27
 }
28 28
 
29 29
 const responseSchemas = {
30
-    profilesList: Joi.object({
31
-        profile_id: Joi.number().integer().greater(0).required(),
32
-        user_id: Joi.number().integer().greater(0).required(),
33
-        // HELP: not sure if this is right, but attempting to fix
34
-        // ValidationError data[0].user_name is not allowed
35
-        // the logic being that CompleteProfile has had user_name added
36
-        // and getCompleteProfiles utilizes CompleteProfile
37
-        // and this route utilizes getCompleteProfiles
38
-        user_name: Joi.string(),
39
-        tags: Joi.array().items(),
40
-        responses: Joi.array().items(
41
-            Joi.object({
42
-                response_key_id: Joi.number().required(),
43
-                profile_id: Joi.number().required(),
44
-                response_id: Joi.number().required(),
45
-                val: Joi.string().required(),
46
-            }),
47
-        ),
48
-        user_type: Joi.string().required(),
49
-    }),
30
+    profilesList: profileSchema.list,
50 31
     error: errorSchema.single
51 32
 }
52 33
 
@@ -95,13 +76,13 @@ module.exports = {
95 76
                 200: Joi.object({
96 77
                     ok: Joi.bool(),
97 78
                     handler: Joi.string(),
98
-                    data: Joi.array().items(responseSchemas.profilesList),
99
-                }),
79
+                    data: responseSchemas.profilesList,
80
+                }).label('list_profiles_res'),
100 81
                 500: Joi.object({
101 82
                     ok: Joi.bool(),
102 83
                     handler: Joi.string(),
103 84
                     data: responseSchemas.error,
104
-                }),
85
+                }).label('error_single_res'),
105 86
             },
106 87
         },
107 88
     },

+ 7
- 12
backend/lib/routes/user/login.js Ver arquivo

@@ -1,6 +1,8 @@
1 1
 'use strict'
2 2
 
3 3
 const Joi = require('joi')
4
+const errorSchema = require('../../schemas/errors')
5
+const userSchema = require('../../schemas/user')
4 6
 
5 7
 const pluginConfig = {
6 8
     handlerType: 'user',
@@ -14,18 +16,11 @@ const pluginConfig = {
14 16
 const validators = {
15 17
     post: {
16 18
         payload: Joi.object({
17
-            user: Joi.object(),
18
-            error: Joi.string(),
19
-        }),
19
+            user: userSchema.single,
20
+            error: errorSchema.single,
21
+        }).append().label('login_payload'),
20 22
     },
21
-    user: Joi.object({
22
-        user_id: Joi.number(),
23
-        user_name: Joi.string(),
24
-        user_email: Joi.string(),
25
-        created_at: Joi.date(),
26
-        updated_at: Joi.date(),
27
-        token: Joi.string(),
28
-    }),
23
+    user: userSchema.single,
29 24
 }
30 25
 
31 26
 module.exports = {
@@ -76,7 +71,7 @@ module.exports = {
76 71
                 ok: Joi.bool(),
77 72
                 handler: Joi.string(),
78 73
                 data: validators.user,
79
-            }),
74
+            }).label('login_res'),
80 75
             failAction: 'log',
81 76
         },
82 77
     },

+ 2
- 1
backend/lib/schemas/groupings.js Ver arquivo

@@ -1,6 +1,7 @@
1 1
 'use strict'
2 2
 
3 3
 const Joi = require('joi')
4
+const surveyProfileSchema = require('./profiles')
4 5
 
5 6
 const singleGrouping = Joi.object({
6 7
     grouping_id: Joi.number(),
@@ -12,7 +13,7 @@ const singleWithProfile = Joi.object({
12 13
     grouping_id: Joi.number(),
13 14
     grouping_name: Joi.string(),
14 15
     grouping_type: Joi.string(),
15
-    profile: Joi.object()
16
+    profile: surveyProfileSchema.single
16 17
 }).label('grouping_single_with_profile')
17 18
 
18 19
 module.exports = {

+ 11
- 0
backend/lib/schemas/params.js Ver arquivo

@@ -0,0 +1,11 @@
1
+const Joi = require('joi')
2
+
3
+module.exports = {
4
+    profileId: Joi.object({ profile_id: Joi.number() }).label('profile_id_param'),
5
+    profileInclude: Joi.object({ include_profile: Joi.bool() }),
6
+    userId: Joi.object({ user_id: Joi.number() }).label('user_id_param'),
7
+    userName: Joi.object({
8
+        name: Joi.string().min(3).max(11),
9
+        all: Joi.array(),
10
+    }).label('user_name_param')
11
+}

+ 2
- 1
backend/lib/schemas/profiles.js Ver arquivo

@@ -2,6 +2,7 @@
2 2
 
3 3
 const Joi = require('joi')
4 4
 const surveyResponseSchema = require('./responses')
5
+const userSchema = require('./user')
5 6
 
6 7
 const singleProfile = Joi.object({
7 8
     profile_id: Joi.number(),
@@ -10,7 +11,7 @@ const singleProfile = Joi.object({
10 11
     responses: surveyResponseSchema.list,
11 12
     tags: Joi.array().items(),
12 13
     user_type: Joi.any(),
13
-    user: Joi.object(),
14
+    user: userSchema.single,
14 15
     profile_description: Joi.string().allow(null, ''),
15 16
     profile_prefs: Joi.object(),
16 17
     profile_media: Joi.array().items(),

+ 16
- 0
backend/lib/schemas/user.js Ver arquivo

@@ -0,0 +1,16 @@
1
+'use strict'
2
+
3
+const Joi = require('joi')
4
+
5
+const singleUser = Joi.object({
6
+    user_id: Joi.number(),
7
+    user_name: Joi.string(),
8
+    user_email: Joi.string(),
9
+    created_at: Joi.date(),
10
+    updated_at: Joi.date(),
11
+    token: Joi.string()
12
+}).label('user_single')
13
+
14
+module.exports = {
15
+    single: singleUser
16
+}

Carregando…
Cancelar
Salvar