-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
269 lines (229 loc) · 6.74 KB
/
index.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
const express = require('express');
const swaggerJsdoc = require('swagger-jsdoc');
const swaggerUi = require('swagger-ui-express');
const Joi = require('joi');
const j2s = require('joi-to-swagger');
const app = express();
app.use(express.json());
const options = {
definition: {
openapi: '3.0.0',
info: {
title: 'Sample API',
description: 'Sample Project for feature request.',
version: '1.0.0'
}
},
apis: ['*.js']
};
const openapiSpecification = swaggerJsdoc(options);
app.use('/docs', swaggerUi.serve, swaggerUi.setup(openapiSpecification));
// we are creating a user
// we have some validation, with JOI
// we have Swagger Documentation, We want the Swagger Document contains the validations information
// Now we are defaining annotaction for Swagger Documentation
// and different validation model with JOI
// Featuren Request is to use JOI model as Schema for Swagger Document
// annotations for UserCreateModel
/**
* @openapi
* components:
* schemas:
* UserCreateModel:
* type: object
* required:
* - fullName
* - gender
* - phoneNumber
* - password
* properties:
* fullName:
* type: string
* minimum: 7
* maximum: 30
* phoneNumber:
* type: string
* minimum: 10
* maximum: 10
* message: "should contains numbers only"
* emergencyPhoneNumber:
* type: string
* minimum: 10
* maximum: 10
* gender:
* type: string
* enum:
* - MALE
* - FEMALE
* - OTHER
* password:
* type: string
* minimum: 7
* maximum: 50
* email:
* type: string
* format: email
*/
// JOI validation object for UserCreateModel
const UserCreateModel = Joi.object({
fullName: Joi.string().min(7).max(30).required(),
phoneNumber: Joi.string().length(10).regex(/^\d+$/).required().messages({
'string.pattern.base': `"phoneNumber" should contains numbers only`
}),
gender: Joi.string().valid('MALE', 'FEMALE', 'OTHER').required(),
emergencyPhoneNumber: Joi.string().length(10).regex(/^\d+$/).messages({
'string.pattern.base': `"phoneNumber" should contains numbers only`
}),
email: Joi.string().email(),
password: Joi.string().min(7).max(50).required()
});
// We are refrencing the UserCreateModel (the annotation one )
/**
* @openapi
* /user:
* post:
* tags:
* - "User"
* description: Create a user
* requestBody:
* content:
* 'application/json':
* schema:
* $ref: "#/components/schemas/UserCreateModel"
* responses:
* 200:
* description: Returns a user object.
*/
app.post('/user', (req, res, next) => {
value = UserCreateModel.validate(req.body, {
abortEarly: false
});
if (value.error) {
return res.status(422).json({
message: 'validation Error',
error: value.error
});
}
//create the user here
res.status(200).json({
value
});
});
app.listen(3000, () => {
console.log('\nFeature Request server on', 3000);
});
// now let's use joi-to-swagger
const j2sSchema = j2s(UserCreateModel);
// Now with the help of joi-to-swagger we have this Schema,
const UserCreateModel2 = j2sSchema.swagger;
// UserCreateModel2 is similar(nearly similar) to the annotation one.
console.log(UserCreateModel2);
// {
// type: 'object',
// properties: {
// fullName: { type: 'string', minLength: 7, maxLength: 30 },
// phoneNumber: { type: 'string', pattern: '^\\d+$', minLength: 10, maxLength: 10 },
// gender: { type: 'string', enum: [Array] },
// emergencyPhoneNumber: { type: 'string', pattern: '^\\d+$', minLength: 10, maxLength: 10 },
// email: { type: 'string', format: 'email' },
// password: { type: 'string', minLength: 7, maxLength: 50 }
// },
// required: [ 'fullName', 'phoneNumber', 'gender', 'password' ],
// additionalProperties: false
// }
// If we can use this UserCreateModel2, and refrence it on requestBody ($ref: #/components/schemas/UserCreateModel)
// I still want to use the anotation for API information
// still use
// * @openapi
// * /user:
// * post:
// ........
// But want to pass this Schema
// ie. Schema on JSON format
// can we do something like
const options2 = {
definition: {
openapi: '3.0.0',
info: {
title: 'Sample API',
description: 'Sample Project for feature request.',
version: '1.0.0'
}
},
apis: ['*.js'],
schemas: [{
name: 'UserCreateModel2',
schema: UserCreateModel2
}] // or any other way.
};
const openapiSpecification2 = swaggerJsdoc(options2);
// and use this Schema on request Body $ref
/**
* @openapi
* /user:
* post:
* tags:
* - "User"
* description: Create a user
* requestBody:
* content:
* 'application/json':
* schema:
* $ref: "#/components/schemas/UserCreateModel2"
* responses:
* 200:
* description: Returns a user object.
*/
app.post('/user', (req, res, next) => {});
// I think, if we can do so, we no longer need to write the model twice. (annotation and JOI)
// Actually we can already do that, using definition on option
// JOI validation object for UserLoginDTO
const UserLoginDTO = Joi.object({
phoneNumber: Joi.string().length(10).regex(/^\d+$/).required().messages({
'string.pattern.base': `"phoneNumber" should contains numbers only`
}),
password: Joi.string().min(7).max(50).required()
});
// now let's use joi-to-swagger
const j2sUserLoginDTO = j2s(UserLoginDTO);
// Now with the help of joi-to-swagger we have this Schema,
const UserLoginDTOSchema = j2sUserLoginDTO.swagger;
const options3 = {
definition: {
openapi: '3.0.0',
info: {
title: 'Sample API',
description: 'Sample Project for feature request.',
version: '1.0.0'
},
components: {
schemas: {
UserCreateModel2,
UserLoginDTO: UserLoginDTOSchema
}
}
},
apis: ['*.js']
};
const openapiSpecification3 = swaggerJsdoc(options3);
app.use('/docs3', swaggerUi.serve, swaggerUi.setup(openapiSpecification3));
// now we can use this here.
/**
* @openapi
* /login:
* post:
* tags:
* - "User"
* description: login a user
* requestBody:
* content:
* 'application/json':
* schema:
* $ref: "#/components/schemas/UserLoginDTO"
* responses:
* 200:
* description: returns a token
*/
app.post('/login', (req, res, next) => {
// stuffs
})