-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschemas.ts
84 lines (75 loc) · 1.92 KB
/
schemas.ts
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
import Joi, { ObjectSchema, CustomHelpers } from "joi";
import parsePhoneNumber from 'libphonenumber-js/mobile';
const isPhoneValid = (value: string, helpers: CustomHelpers) => {
if (parsePhoneNumber(value)?.isValid()) return value;
return helpers.error('any.invalid', { message: 'Phone number is invalid' });
}
const emailOrPhone = Joi.object().keys({
email: Joi
.string()
.trim()
.lowercase()
.email()
.optional(),
phone: Joi
.string()
.trim()
.custom(isPhoneValid)
.optional(),
}).or('email', 'phone');
const auth = Joi.object().keys({
email: Joi
.string()
.trim()
.lowercase()
.email()
.optional(),
phone: Joi
.string()
.trim()
.custom(isPhoneValid)
.optional(),
password: Joi
.string()
.min(8)
.required(),
}).or('email', 'phone');
const resetPassword = Joi.object().keys({
hash: Joi.string().optional(),
token: Joi.string().length(6).optional(),
password: Joi
.string()
.min(8)
.required(),
confirmPassword: Joi.string().valid(Joi.ref('password')).required(),
}).or('hash', 'token');
const changePassword = Joi.object().keys({
oldPassword: Joi
.string()
.min(8)
.required(),
newPassword: Joi
.string()
.min(8)
.required(),
confirmPassword: Joi.string().valid(Joi.ref('newPassword')).required(),
});
const sendVerificationCode = Joi.object().keys({
type: Joi.string().valid('email', 'phone').required()
});
const verifyEmail = Joi.object().keys({
hash: Joi.string().optional(),
token: Joi.string().optional(),
}).or('hash', 'token');
const verifyPhone = Joi.object().keys({
token: Joi.string().required(),
});
export default {
"emailOrPhone": emailOrPhone,
"auth": auth,
"resetPassword": resetPassword,
"changePassword": changePassword,
"sendVerificationCode": sendVerificationCode,
"verifyEmail": verifyEmail,
"verifyPhone": verifyPhone,
} as { [key: string]: ObjectSchema };