data-validation-proximity 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/index.js +1035 -0
  2. package/package.json +11 -0
package/index.js ADDED
@@ -0,0 +1,1035 @@
1
+ //this is the middelware for data validation
2
+ const joi = require('joi');
3
+
4
+ //auth validation data
5
+ const userSchema = joi.object({
6
+ email: joi.string().email(),
7
+ phone: joi.string(),
8
+ username: joi.string().min(5).required(),
9
+ password: joi.string().min(6).required(),
10
+ password_confirmation: joi.string().min(6).required(),
11
+ role: joi.string().valid('user', 'admin', 'seller', 'paymentManager','manager','SuperManager').required(),
12
+ cart: joi.string().allow(null).allow(''),
13
+ google: joi.boolean(),
14
+ });
15
+ exports.userSchemaValidation = (req, res, next) => {
16
+ const { error } = userSchema.validate(req.body);
17
+ if (error) {
18
+ console.log(error);
19
+ return res.status(400).send(error.details[0].message);
20
+ }
21
+ next();
22
+ };
23
+
24
+ //reset password request validation data
25
+ const resetPasswordRequestSchema = joi.object({
26
+ email: joi.string().required(),
27
+ });
28
+ exports.resetPasswordRequestSchemaValidation = (req, res, next) => {
29
+ const { error } = resetPasswordRequestSchema.validate(req.body);
30
+ if (error) {
31
+ console.log(error);
32
+ return res.status(400).send(error.details[0].message);
33
+ }
34
+ next();
35
+ };
36
+
37
+ //reset password validation data
38
+ const resetPasswordSchema = joi.object({
39
+ password: joi.string().min(6).required(),
40
+ password_confirmation: joi.string().min(6).required(),
41
+ });
42
+ exports.resetPasswordSchemaValidation = (req, res, next) => {
43
+ const { error } = resetPasswordSchema.validate(req.body);
44
+ if (error) {
45
+ console.log(error);
46
+ return res.status(400).send(error.details[0].message);
47
+ }
48
+ next();
49
+ };
50
+
51
+ const userLoginSchema = joi.object({
52
+ email: joi.string().required(),
53
+ role: joi.string().valid('user', 'admin', 'seller', 'paymentManager','manager','SuperManager').required(),
54
+ password: joi.string().min(6),
55
+ google: joi.boolean(),
56
+ name: joi.string(),
57
+ });
58
+ exports.userLoginSchemaValidation = (req, res, next) => {
59
+ const { error } = userLoginSchema.validate(req.body);
60
+ if (error) {
61
+ console.log(error);
62
+ return res.status(400).send(error.details[0].message);
63
+ }
64
+ next();
65
+ };
66
+
67
+ const userVerificationSchema = joi.object({
68
+ email: joi.string().required(),
69
+ verificationCode: joi.string().min(3).max(5).required(),
70
+ });
71
+ //user registration data
72
+ exports.userVerificationSchemaValidation = (req, res, next) => {
73
+ const { error } = userVerificationSchema.validate(req.body);
74
+ if (error) {
75
+ console.log(error);
76
+ return res.status(400).send(error.details[0].message);
77
+ }
78
+ next();
79
+ };
80
+
81
+ const resendUserVerificationSchema = joi.object({
82
+ email: joi.string().required(),
83
+ });
84
+ //user registration data
85
+ exports.resendUserVerificationSchemaValidation = (req, res, next) => {
86
+ const { error } = resendUserVerificationSchema.validate(req.body);
87
+ if (error) {
88
+ console.log(error);
89
+ return res.status(400).send(error.details[0].message);
90
+ }
91
+ next();
92
+ };
93
+
94
+ const updateSchema = joi.object({
95
+ email: joi.string().email(),
96
+ password: joi.string().min(6),
97
+ password_confirmation: joi.string().min(6),
98
+ username: joi.string().min(5),
99
+ phone: joi.string().min(10),
100
+ profileImage: joi.string().min(3).max(200),
101
+ adresse: joi.object({
102
+ latitude: joi.number().required(),
103
+ longitude: joi.number().required(),
104
+ countryCode: joi.string().min(2),
105
+ country: joi.string().min(3),
106
+ city: joi.string().min(3),
107
+ streetName: joi.string().min(3),
108
+ postalCode: joi.string().min(3),
109
+ fullAdress: joi.string(),
110
+ region: joi.string(),
111
+ apartmentNumber: joi.string(),
112
+ }),
113
+ policy: joi
114
+ .object({
115
+ workingTime: joi
116
+ .object({
117
+ openTime: joi.string().required(),
118
+ closeTime: joi.string().required(),
119
+ })
120
+ .allow(null),
121
+ pickup: joi
122
+ .object({
123
+ timeLimit: joi.number().required(),
124
+ })
125
+ .allow(null),
126
+ delivery: joi
127
+ .object({
128
+ delivery: joi.boolean().required().allow(null),
129
+ })
130
+ .allow(null),
131
+ reservation: joi
132
+ .object({
133
+ duration: joi.number().allow(null).required(),
134
+ payment: joi
135
+ .object({
136
+ free: joi.boolean().allow(null).required(),
137
+ partial: joi
138
+ .object({
139
+ fixe: joi.number().allow(null).required(),
140
+ percentage: joi.number().allow(null).required(),
141
+ })
142
+ .allow(null),
143
+ total: joi.boolean().allow(null).required(),
144
+ })
145
+ .allow(null),
146
+ cancelation: joi
147
+ .object({
148
+ restrictions: joi
149
+ .object({
150
+ fixe: joi.number().allow(null).required(),
151
+ percentage: joi.number().allow(null).required(),
152
+ })
153
+ .allow(null),
154
+ })
155
+ .allow(null),
156
+ })
157
+ .allow(null),
158
+ return: joi
159
+ .object({
160
+ duration: joi.number().allow(null).required(),
161
+ productStatus: joi.string().allow('').allow(null).required(),
162
+ returnMethod: joi.string().allow('').allow(null).required(),
163
+ refund: joi
164
+ .object({
165
+ order: joi
166
+ .object({
167
+ fixe: joi.number().allow(null).required(),
168
+ percentage: joi.number().allow(null).required(),
169
+ })
170
+ .allow(null),
171
+ shipping: joi
172
+ .object({
173
+ fixe: joi.number().allow(null).required(),
174
+ percentage: joi.number().allow(null).required(),
175
+ })
176
+ .allow(null),
177
+ })
178
+ .allow(null),
179
+ })
180
+ .allow(null),
181
+ order: joi
182
+ .object({
183
+ notification: joi
184
+ .object({
185
+ realtime: joi.boolean().allow(null).required(),
186
+ time: joi.string().allow(null).required(),
187
+ perOrdersNbr: joi.number().allow(null).required(),
188
+ sendMode: joi
189
+ .object({
190
+ mail: joi.boolean().allow(null).required(),
191
+ sms: joi.boolean().allow(null).required(),
192
+ popup: joi.boolean().allow(null).required(),
193
+ vibration: joi.boolean().allow(null).required(),
194
+ ringing: joi.boolean().allow(null).required(),
195
+ })
196
+ .allow(null),
197
+ })
198
+ .allow(null),
199
+ })
200
+ .allow(null),
201
+ })
202
+ .allow(null),
203
+ discountCode: joi.string().min(3),
204
+ companyName: joi.string().min(3),
205
+ shippingAdress: joi.object({
206
+ countryCode: joi.string().min(2),
207
+ country: joi.string().min(3),
208
+ city: joi.string().min(3),
209
+ streetName: joi.string().min(3),
210
+ postalCode: joi.string().min(3),
211
+ fullAdress: joi.string(),
212
+ region: joi.string(),
213
+ apartmentNumber: joi.string(),
214
+ }),
215
+ storeCategories: joi.string().allow(null),
216
+ productCategories: joi.string().allow(null),
217
+ tags: joi.string().allow(null),
218
+ notification: joi.string().allow(null),
219
+ proximityRange: joi.number().min(0).max(1000),
220
+ });
221
+ exports.updateSchemaValidation = (req, res, next) => {
222
+ if (typeof req.body.policy === 'string' && req.body.policy != '') {
223
+ req.body.policy = JSON.parse(req.body.policy);
224
+ }
225
+
226
+ const { error } = updateSchema.validate(req.body);
227
+ if (error) {
228
+ console.log(error);
229
+ return res.status(400).send(error.details[0].message);
230
+ }
231
+ next();
232
+ };
233
+ //cart validation data
234
+
235
+ const itemSchema = joi.object({
236
+ productId: joi.string().required(),
237
+ quantity: joi.number().min(1),
238
+ variantId: joi.string().required(),
239
+ });
240
+ exports.itemSchemaValidation = (req, res, next) => {
241
+ const { error } = itemSchema.validate(req.body);
242
+ if (error) {
243
+ console.log(error);
244
+ return res.status(400).send(error.details[0].message);
245
+ }
246
+ next();
247
+ };
248
+
249
+ //category validation data
250
+
251
+ const createCategorySchema = joi.object({
252
+ name: joi.string().required(),
253
+ description: joi.string().required(),
254
+ });
255
+ exports.createCategorySchemaValidation = (req, res, next) => {
256
+ const { error } = createCategorySchema.validate(req.body);
257
+ if (error) {
258
+ console.log(error);
259
+ return res.status(400).send(error.details[0].message);
260
+ }
261
+ next();
262
+ };
263
+ //offer validation data
264
+ const schemaOffer = joi.object({
265
+ productId: joi.string().required(),
266
+ offerExpiration: joi.date(),
267
+ offerImage: joi.string(),
268
+ offerName: joi.string(),
269
+ offerDescription: joi.string(),
270
+ offerDiscount: joi.number().required(),
271
+ offerStock: joi.number().required(),
272
+ discountType: joi.string().valid('percentage', 'amount'),
273
+ });
274
+ exports.schemaOfferValidation = (req, res, next) => {
275
+ const { error } = schemaOffer.validate(req.body);
276
+ if (error) {
277
+ console.log(error);
278
+ return res.status(400).send(error.details[0].message);
279
+ }
280
+ next();
281
+ };
282
+ const schemaGetOffers = joi.object({
283
+ storeId: joi.string().required(),
284
+ });
285
+ exports.schemaGetOffersValidation = (req, res, next) => {
286
+ const { error } = schemaGetOffers.validate({ storeId: req.params.storeId });
287
+ if (error) {
288
+ console.log(error);
289
+ return res.status(400).send(error.details[0].message);
290
+ }
291
+ next();
292
+ };
293
+
294
+ const schemaGetOfferById = joi.object({
295
+ offerId: joi.string(),
296
+ });
297
+ exports.schemaGetOfferByIdValidation = (req, res, next) => {
298
+ const { error } = schemaGetOfferById.validate(req.body);
299
+ if (error) {
300
+ console.log(error);
301
+ return res.status(400).send(error.details[0].message);
302
+ }
303
+ next();
304
+ };
305
+
306
+ const schemaUpdateOffer = joi.object({
307
+ offerExpiration: joi.date(),
308
+ offerImage: joi.string(),
309
+ offerName: joi.string(),
310
+ offerDescription: joi.string(),
311
+ offerDiscount: joi.number(),
312
+ });
313
+ exports.schemaUpdateOfferValidation = (req, res, next) => {
314
+ const { error } = schemaUpdateOffer.validate(req.body);
315
+ if (error) {
316
+ console.log(error);
317
+ return res.status(400).send(error.details[0].message);
318
+ }
319
+ next();
320
+ };
321
+ //order validation data
322
+ const orderSchema = joi.object({
323
+ storeId: joi.string().required(),
324
+ clientId: joi.string().required(),
325
+ items: joi.array().items(
326
+ joi.object({
327
+ productId: joi.string().required(),
328
+ variantId: joi.string().required(),
329
+ policy: joi.object().allow(null),
330
+ price: joi.number().required().allow(null),
331
+ discountPrice: joi.number().required().allow(null),
332
+ quantity: joi.number().required(),
333
+ })
334
+ ),
335
+ paymentInfos: joi
336
+ .object({
337
+ totalAmount: joi.number().required(),
338
+ paymentAmount: joi.number().required(),
339
+ })
340
+ .required(),
341
+ reservation: joi.boolean(),
342
+ pickup: joi.boolean(),
343
+ delivery: joi.object({
344
+ shippingAmount: joi.number().required(),
345
+ nbrKm: joi.number(),
346
+ }),
347
+ canceled: joi.object({
348
+ byClient: joi.boolean().required(),
349
+ motif: joi.string().required(),
350
+ }),
351
+ status: joi.string(),
352
+ });
353
+ exports.orderSchemaValidation = (req, res, next) => {
354
+ const { error } = orderSchema.validate(req.body);
355
+ if (error) {
356
+ console.log(error);
357
+ return res.status(400).send(error.details[0].message);
358
+ }
359
+ next();
360
+ };
361
+ //product validation data
362
+ const updateProductSchema = joi.object({
363
+ name: joi.string().min(3),
364
+ sellerId: joi.string().required(),
365
+ price: joi.number().min(1).max(1000000),
366
+ description: joi.string().min(3).max(200),
367
+ tags: joi.array().items(joi.string().min(3)),
368
+ discount: joi.number().min(0).max(100),
369
+ images: joi.array().items(joi.string().min(3).max(200)),
370
+ storeId: joi.string().required(),
371
+ categoryId: joi.string(),
372
+ storeCategoryId: joi.string(),
373
+ subCategoryId: joi.string(),
374
+ rayonId: joi.string(),
375
+ variantes: joi.array().items(
376
+ joi.object({
377
+ _id: joi.string(),
378
+ name: joi.string().min(3),
379
+ quantity: joi.number().min(1).max(1000000),
380
+ price: joi.number().min(1).max(1000000),
381
+ description: joi.string().min(3).max(200),
382
+ image: joi.string().min(3).max(200),
383
+ characterstics: joi.array().items(
384
+ joi.object({
385
+ name: joi.string().min(3),
386
+ value: joi.string(),
387
+ })
388
+ ),
389
+ })
390
+ ),
391
+ policy: joi
392
+ .object({
393
+ workingTime: joi
394
+ .object({
395
+ openTime: joi.string().required(),
396
+ closeTime: joi.string().required(),
397
+ })
398
+ .allow(null),
399
+ pickup: joi
400
+ .object({
401
+ timeLimit: joi.number().required(),
402
+ })
403
+ .allow(null),
404
+ delivery: joi
405
+ .object({
406
+ delivery: joi.boolean().required().allow(null),
407
+ })
408
+ .allow(null),
409
+ reservation: joi
410
+ .object({
411
+ duration: joi.number().allow(null).required(),
412
+ payment: joi
413
+ .object({
414
+ free: joi.boolean().allow(null).required(),
415
+ partial: joi
416
+ .object({
417
+ fixe: joi.number().allow(null).required(),
418
+ percentage: joi.number().allow(null).required(),
419
+ })
420
+ .allow(null),
421
+ total: joi.boolean().allow(null).required(),
422
+ })
423
+ .allow(null),
424
+ cancelation: joi
425
+ .object({
426
+ restrictions: joi
427
+ .object({
428
+ fixe: joi.number().allow(null).required(),
429
+ percentage: joi.number().allow(null).required(),
430
+ })
431
+ .allow(null),
432
+ })
433
+ .allow(null),
434
+ })
435
+ .allow(null),
436
+ return: joi
437
+ .object({
438
+ duration: joi.number().allow(null).required(),
439
+ productStatus: joi.string().allow('').allow(null).required(),
440
+ returnMethod: joi.string().allow('').allow(null).required(),
441
+ refund: joi
442
+ .object({
443
+ order: joi
444
+ .object({
445
+ fixe: joi.number().allow(null).required(),
446
+ percentage: joi.number().allow(null).required(),
447
+ })
448
+ .allow(null),
449
+ shipping: joi
450
+ .object({
451
+ fixe: joi.number().allow(null).required(),
452
+ percentage: joi.number().allow(null).required(),
453
+ })
454
+ .allow(null),
455
+ })
456
+ .allow(null),
457
+ })
458
+ .allow(null),
459
+ order: joi
460
+ .object({
461
+ validation: joi
462
+ .object({
463
+ auto: joi.boolean().allow(null).required(),
464
+ manual: joi.boolean().allow(null).required(),
465
+ })
466
+ .allow(null),
467
+ notification: joi
468
+ .object({
469
+ realtime: joi.boolean().allow(null).required(),
470
+ time: joi.string().allow(null).required(),
471
+ perOrdersNbr: joi.number().allow(null).required(),
472
+ sendMode: joi
473
+ .object({
474
+ mail: joi.boolean().allow(null).required(),
475
+ sms: joi.boolean().allow(null).required(),
476
+ popup: joi.boolean().allow(null).required(),
477
+ vibration: joi.boolean().allow(null).required(),
478
+ ringing: joi.boolean().allow(null).required(),
479
+ })
480
+ .allow(null),
481
+ })
482
+ .allow(null),
483
+ })
484
+ .allow(null),
485
+ })
486
+ .allow(null),
487
+ });
488
+ exports.updateProductSchemaValidation = (req, res, next) => {
489
+ if (typeof req.body.variantes === 'string') {
490
+ req.body.variantes = JSON.parse(req.body.variantes);
491
+ }
492
+ console.log('req.body.variantes');
493
+ console.log(req.body.variantes);
494
+
495
+ if (typeof req.body.policy === 'string' && req.body.policy != '') {
496
+ req.body.policy = JSON.parse(req.body.policy);
497
+ }
498
+
499
+ const { error } = updateProductSchema.validate(req.body);
500
+ if (error) {
501
+ console.log(error);
502
+ return res.status(400).send(error.details[0].message);
503
+ }
504
+ next();
505
+ };
506
+
507
+ //product validation data
508
+ const createProductSchema = joi.object({
509
+ name: joi.string().min(3).required(),
510
+ price: joi.number().min(1).required(),
511
+ description: joi.string().min(3).max(800),
512
+ image: joi.string().min(3).max(200),
513
+ storeCategoryId: joi.string().allow(''),
514
+ categoryId: joi.string().allow(''),
515
+ subCategoryId: joi.string().allow(''),
516
+ rayonId: joi.string().allow(''),
517
+ subcategory: joi.string().min(3),
518
+ discount: joi.number().min(1).max(100),
519
+ tags: joi.array().items(joi.string().min(2)),
520
+ sellerId: joi.string(),
521
+ storeId: joi.string().required(),
522
+ images: joi.array().items(joi.string().min(3).max(200)),
523
+ characteristics: joi.object().pattern(joi.string(), joi.array().items(joi.string().min(1))),
524
+ variantes: joi.array().items(
525
+ joi.object({
526
+ price: joi.number().min(1).required(),
527
+ quantity: joi.number().min(1).required(),
528
+ characterstics: joi.array().items(
529
+ joi.object({
530
+ name: joi.string().min(3).required(),
531
+ value: joi.string().required(),
532
+ })
533
+ ),
534
+ })
535
+ ),
536
+ policy: joi
537
+ .object({
538
+ workingTime: joi
539
+ .object({
540
+ openTime: joi.string().required(),
541
+ closeTime: joi.string().required(),
542
+ })
543
+ .allow(null),
544
+ pickup: joi
545
+ .object({
546
+ timeLimit: joi.number().required(),
547
+ })
548
+ .allow(null),
549
+ delivery: joi
550
+ .object({
551
+ delivery: joi.boolean().required().allow(null),
552
+ })
553
+ .allow(null),
554
+ reservation: joi
555
+ .object({
556
+ duration: joi.number().allow(null).required(),
557
+ payment: joi
558
+ .object({
559
+ free: joi.boolean().allow(null).required(),
560
+ partial: joi
561
+ .object({
562
+ fixe: joi.number().allow(null).required(),
563
+ percentage: joi.number().allow(null).required(),
564
+ })
565
+ .allow(null),
566
+ total: joi.boolean().allow(null).required(),
567
+ })
568
+ .allow(null),
569
+ cancelation: joi
570
+ .object({
571
+ restrictions: joi
572
+ .object({
573
+ fixe: joi.number().allow(null).required(),
574
+ percentage: joi.number().allow(null).required(),
575
+ })
576
+ .allow(null),
577
+ })
578
+ .allow(null),
579
+ })
580
+ .allow(null),
581
+ return: joi
582
+ .object({
583
+ duration: joi.number().allow(null).required(),
584
+ productStatus: joi.string().allow('').allow(null).required(),
585
+ returnMethod: joi.string().allow('').allow(null).required(),
586
+ refund: joi
587
+ .object({
588
+ order: joi
589
+ .object({
590
+ fixe: joi.number().allow(null).required(),
591
+ percentage: joi.number().allow(null).required(),
592
+ })
593
+ .allow(null),
594
+ shipping: joi
595
+ .object({
596
+ fixe: joi.number().allow(null).required(),
597
+ percentage: joi.number().allow(null).required(),
598
+ })
599
+ .allow(null),
600
+ })
601
+ .allow(null),
602
+ })
603
+ .allow(null),
604
+ order: joi
605
+ .object({
606
+ validation: joi
607
+ .object({
608
+ auto: joi.boolean().allow(null).required(),
609
+ manual: joi.boolean().allow(null).required(),
610
+ })
611
+ .allow(null),
612
+ notification: joi
613
+ .object({
614
+ realtime: joi.boolean().allow(null).required(),
615
+ time: joi.string().allow(null).required(),
616
+ perOrdersNbr: joi.number().allow(null).required(),
617
+ sendMode: joi
618
+ .object({
619
+ mail: joi.boolean().allow(null).required(),
620
+ sms: joi.boolean().allow(null).required(),
621
+ popup: joi.boolean().allow(null).required(),
622
+ vibration: joi.boolean().allow(null).required(),
623
+ ringing: joi.boolean().allow(null).required(),
624
+ })
625
+ .allow(null),
626
+ })
627
+ .allow(null),
628
+ })
629
+ .allow(null),
630
+ })
631
+ .allow(null),
632
+ });
633
+ exports.createProductSchemaValidation = (req, res, next) => {
634
+ console.log(req.body);
635
+ if (typeof req.body.variantes === 'string' && req.body.variantes != '') {
636
+ req.body.variantes = JSON.parse(req.body.variantes);
637
+
638
+ if (req.files.images && req.files.images.name) {
639
+ req.files.images = [req.files.images];
640
+ }
641
+
642
+ if (req.files.varientsImages && req.files.varientsImages.name) {
643
+ req.files.varientsImages = [req.files.varientsImages];
644
+ }
645
+ }
646
+
647
+ if (typeof req.body.policy === 'string' && req.body.policy != '') {
648
+ req.body.policy = JSON.parse(req.body.policy);
649
+ }
650
+
651
+ const { error } = createProductSchema.validate(req.body);
652
+ if (error) {
653
+ console.log(error);
654
+ return res.status(400).send(error.details[0].message);
655
+ }
656
+ next();
657
+ };
658
+
659
+ //search validation data
660
+ const schemaSearchStore = joi.object({
661
+ langitude: joi.number().required(),
662
+ latitude: joi.number().required(),
663
+ radius: joi.number().required(),
664
+ });
665
+ exports.schemaSearchStoreValidation = (req, res, next) => {
666
+ const { error } = schemaSearchStore.validate(req.body);
667
+ if (error) {
668
+ console.log(error);
669
+ return res.status(400).send(error.details[0].message);
670
+ }
671
+ next();
672
+ };
673
+
674
+ const schemaSearchProduct = joi.object({
675
+ langitude: joi.number().required(),
676
+ latitude: joi.number().required(),
677
+ radius: joi.number().required(),
678
+ name: joi.string().min(3),
679
+ page: joi.number().min(0),
680
+ limit: joi.number().min(0),
681
+ });
682
+ exports.schemaSearchProductValidation = (req, res, next) => {
683
+ const { error } = schemaSearchProduct.validate(req.body);
684
+ if (error) {
685
+ console.log(error);
686
+ return res.status(400).send(error.details[0].message);
687
+ }
688
+ next();
689
+ };
690
+
691
+ //store validation data
692
+
693
+ const schemaStore = joi.object({
694
+ name: joi.string().min(2).required(),
695
+ description: joi.string().min(3).max(800).required(),
696
+ sellerId: joi.string().required(),
697
+ location: joi
698
+ .object({
699
+ type: joi.string().valid('Point').required(),
700
+ coordinates: joi.array().items().length(2).required(),
701
+ })
702
+ .required(),
703
+ address: joi.object({
704
+ city: joi.string().min(3).required(),
705
+ streetName: joi.string().min(3),
706
+ postalCode: joi.string().min(2).max(5),
707
+ fullAdress: joi.string().min(3),
708
+ region: joi.string().allow(null),
709
+ country: joi.string().min(3),
710
+ countryCode: joi.string().min(2),
711
+ }),
712
+ //* working time
713
+ workingTime: joi.object({
714
+ option: joi.string().min(1).required(),
715
+ fixedHours: joi.array().items(
716
+ joi.object({
717
+ openTime: joi.string().allow(null).required(),
718
+ closeTime: joi.string().allow(null).required(),
719
+ })
720
+ ),
721
+ customizedHours: joi
722
+ .object()
723
+ .allow(null)
724
+ .pattern(
725
+ joi.string(),
726
+ joi.array().items(
727
+ joi.object({
728
+ openTime: joi.string().allow(null).required(),
729
+ closeTime: joi.string().allow(null).required(),
730
+ })
731
+ )
732
+ ),
733
+ }),
734
+ policy: joi
735
+ .object({
736
+ workingTime: joi
737
+ .object({
738
+ openTime: joi.string().required(),
739
+ closeTime: joi.string().required(),
740
+ })
741
+ .allow(null),
742
+ pickup: joi
743
+ .object({
744
+ timeLimit: joi.number().required(),
745
+ })
746
+ .allow(null),
747
+ delivery: joi
748
+ .object({
749
+ delivery: joi.boolean().required().allow(null),
750
+ })
751
+ .allow(null),
752
+ reservation: joi
753
+ .object({
754
+ duration: joi.number().allow(null).required(),
755
+ payment: joi
756
+ .object({
757
+ free: joi.boolean().allow(null).required(),
758
+ partial: joi
759
+ .object({
760
+ fixe: joi.number().allow(null).required(),
761
+ percentage: joi.number().allow(null).required(),
762
+ })
763
+ .allow(null),
764
+ total: joi.boolean().allow(null).required(),
765
+ })
766
+ .allow(null),
767
+ cancelation: joi
768
+ .object({
769
+ restrictions: joi
770
+ .object({
771
+ fixe: joi.number().allow(null).required(),
772
+ percentage: joi.number().allow(null).required(),
773
+ })
774
+ .allow(null),
775
+ })
776
+ .allow(null),
777
+ })
778
+ .allow(null),
779
+ return: joi
780
+ .object({
781
+ duration: joi.number().allow(null).required(),
782
+ returnMethod: joi.string().allow('').allow(null).required(),
783
+ productStatus: joi.string().allow('').allow(null).required(),
784
+ refund: joi
785
+ .object({
786
+ order: joi
787
+ .object({
788
+ fixe: joi.number().allow(null).required(),
789
+ percentage: joi.number().allow(null).required(),
790
+ })
791
+ .allow(null),
792
+ shipping: joi
793
+ .object({
794
+ fixe: joi.number().allow(null).required(),
795
+ percentage: joi.number().allow(null).required(),
796
+ })
797
+ .allow(null),
798
+ })
799
+ .allow(null),
800
+ })
801
+ .allow(null),
802
+ order: joi
803
+ .object({
804
+ validation: joi
805
+ .object({
806
+ auto: joi.boolean().allow(null).required(),
807
+ manual: joi.boolean().allow(null).required(),
808
+ })
809
+ .allow(null),
810
+ notification: joi
811
+ .object({
812
+ realtime: joi.boolean().allow(null).required(),
813
+ time: joi.string().allow(null).required(),
814
+ perOrdersNbr: joi.number().allow(null).required(),
815
+ sendMode: joi
816
+ .object({
817
+ mail: joi.boolean().allow(null).required(),
818
+ sms: joi.boolean().allow(null).required(),
819
+ popup: joi.boolean().allow(null).required(),
820
+ vibration: joi.boolean().allow(null).required(),
821
+ ringing: joi.boolean().allow(null).required(),
822
+ })
823
+ .allow(null),
824
+ })
825
+ .allow(null),
826
+ })
827
+ .allow(null),
828
+ })
829
+ .allow(null),
830
+ image: joi.string().min(3),
831
+ });
832
+ exports.schemaStoreValidation = (req, res, next) => {
833
+ console.log('start store validation');
834
+
835
+ if (typeof req.body.location === 'string') {
836
+ req.body.location = JSON.parse(req.body.location);
837
+ }
838
+ if (typeof req.body.address === 'string') {
839
+ req.body.address = JSON.parse(req.body.address);
840
+ }
841
+ if (typeof req.body.policy === 'string' && req.body.policy != '') {
842
+ req.body.policy = JSON.parse(req.body.policy);
843
+ }
844
+ if (typeof req.body.workingTime === 'string' && req.body.workingTime != '') {
845
+ req.body.workingTime = JSON.parse(req.body.workingTime);
846
+ console.log('ff');
847
+ console.log(req.body.workingTime);
848
+ }
849
+
850
+ // const { error } = schemaStore.validate(req.body);
851
+ // if (error) {
852
+ // console.log("error");
853
+ // console.log(error.details[0].message);
854
+ // return res.status(400).send(error.details[0].message);
855
+ // }
856
+ console.log('next');
857
+ next();
858
+ };
859
+ const schemaUpdateStore = joi.object({
860
+ name: joi.string().min(3),
861
+ description: joi.string().min(3).max(200),
862
+
863
+ isActive: joi.bool(),
864
+ address: joi.object({
865
+ city: joi.string().min(3),
866
+ streetName: joi.string().min(3).max(40),
867
+ postalCode: joi.string().min(3),
868
+ country: joi.string().min(3),
869
+ countryCode: joi.string().min(2),
870
+ fullAdress: joi.string().min(3).max(40),
871
+ region: joi.string().min(3),
872
+ postalCode: joi.string().min(3),
873
+ }),
874
+ policy: joi
875
+ .object({
876
+ workingTime: joi
877
+ .object({
878
+ openTime: joi.string().required(),
879
+ closeTime: joi.string().required(),
880
+ })
881
+ .allow(null),
882
+ pickup: joi
883
+ .object({
884
+ timeLimit: joi.number().required(),
885
+ })
886
+ .allow(null),
887
+ delivery: joi
888
+ .object({
889
+ delivery: joi.boolean().required().allow(null),
890
+ })
891
+ .allow(null),
892
+ reservation: joi
893
+ .object({
894
+ duration: joi.number().allow(null).required(),
895
+ payment: joi
896
+ .object({
897
+ free: joi.boolean().allow(null).required(),
898
+ partial: joi
899
+ .object({
900
+ fixe: joi.number().allow(null).required(),
901
+ percentage: joi.number().allow(null).required(),
902
+ })
903
+ .allow(null),
904
+ total: joi.boolean().allow(null).required(),
905
+ })
906
+ .allow(null),
907
+ cancelation: joi
908
+ .object({
909
+ restrictions: joi
910
+ .object({
911
+ fixe: joi.number().allow(null).required(),
912
+ percentage: joi.number().allow(null).required(),
913
+ })
914
+ .allow(null),
915
+ })
916
+ .allow(null),
917
+ })
918
+ .allow(null),
919
+ return: joi
920
+ .object({
921
+ duration: joi.number().allow(null).required(),
922
+ productStatus: joi.string().allow('').allow(null).required(),
923
+ returnMethod: joi.string().allow('').allow(null).required(),
924
+ refund: joi
925
+ .object({
926
+ order: joi
927
+ .object({
928
+ fixe: joi.number().allow(null).required(),
929
+ percentage: joi.number().allow(null).required(),
930
+ })
931
+ .allow(null),
932
+ shipping: joi
933
+ .object({
934
+ fixe: joi.number().allow(null).required(),
935
+ percentage: joi.number().allow(null).required(),
936
+ })
937
+ .allow(null),
938
+ })
939
+ .allow(null),
940
+ })
941
+ .allow(null),
942
+ order: joi
943
+ .object({
944
+ validation: joi
945
+ .object({
946
+ auto: joi.boolean().allow(null).required(),
947
+ manual: joi.boolean().allow(null).required(),
948
+ })
949
+ .allow(null),
950
+ notification: joi
951
+ .object({
952
+ realtime: joi.boolean().allow(null).required(),
953
+ time: joi.string().allow(null).required(),
954
+ perOrdersNbr: joi.number().allow(null).required(),
955
+ sendMode: joi
956
+ .object({
957
+ mail: joi.boolean().allow(null).required(),
958
+ sms: joi.boolean().allow(null).required(),
959
+ popup: joi.boolean().allow(null).required(),
960
+ vibration: joi.boolean().allow(null).required(),
961
+ ringing: joi.boolean().allow(null).required(),
962
+ })
963
+ .allow(null),
964
+ })
965
+ .allow(null),
966
+ })
967
+ .allow(null),
968
+ })
969
+ .allow(null),
970
+ location: joi.object({
971
+ type: joi.string().valid('Point'),
972
+ coordinates: joi.array().items().length(2),
973
+ }),
974
+ workingTime: joi.object({
975
+ option: joi.string().min(1).required(),
976
+ fixedHours: joi.array().items(
977
+ joi.object({
978
+ openTime: joi.string().allow(null).required(),
979
+ closeTime: joi.string().allow(null).required(),
980
+ })
981
+ ),
982
+ customizedHours: joi
983
+ .object()
984
+ .allow(null)
985
+ .pattern(
986
+ joi.string(),
987
+ joi.array().items(
988
+ joi.object({
989
+ openTime: joi.string().allow(null).required(),
990
+ closeTime: joi.string().allow(null).required(),
991
+ })
992
+ )
993
+ ),
994
+ }),
995
+
996
+ image: joi.string().min(3),
997
+ });
998
+ exports.schemaUpdateStoreValidation = (req, res, next) => {
999
+ if (typeof req.body.location === 'string') {
1000
+ req.body.location = JSON.parse(req.body.location);
1001
+ }
1002
+ if (typeof req.body.address === 'string') {
1003
+ req.body.address = JSON.parse(req.body.address);
1004
+ }
1005
+
1006
+ if (typeof req.body.policy === 'string' && req.body.policy != '') {
1007
+ req.body.policy = JSON.parse(req.body.policy);
1008
+ }
1009
+ if (typeof req.body.workingTime === 'string' && req.body.workingTime != '') {
1010
+ req.body.workingTime = JSON.parse(req.body.workingTime);
1011
+ console.log('ff');
1012
+ console.log(req.body.workingTime);
1013
+ }
1014
+ // const { error } = schemaUpdateStore.validate(req.body);
1015
+ // if (error) {
1016
+ // console.log(error);
1017
+ // return res.status(400).send(error.details[0].message);
1018
+ // }
1019
+ next();
1020
+ };
1021
+
1022
+ const schemaUpdateStoreRating = joi.object({
1023
+ userId: joi.string().required(),
1024
+ storeId: joi.string().required(),
1025
+ rate: joi.number().required(),
1026
+ });
1027
+
1028
+ exports.schemaUpdateStoreRatingValidation = (req, res, next) => {
1029
+ const { error } = schemaUpdateStoreRating.validate(req.body);
1030
+ if (error) {
1031
+ console.log(error);
1032
+ return res.status(400).send(error.details[0].message);
1033
+ }
1034
+ next();
1035
+ };
package/package.json ADDED
@@ -0,0 +1,11 @@
1
+ {
2
+ "name": "data-validation-proximity",
3
+ "version": "1.0.0",
4
+ "description": "A library of Joi-based validation middlewares for Express.js",
5
+ "main": "index.js",
6
+ "license": "MIT",
7
+ "dependencies": {
8
+ "joi": "^17.x"
9
+ }
10
+ }
11
+