-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlive.ts
More file actions
37 lines (31 loc) · 1012 Bytes
/
Copy pathlive.ts
File metadata and controls
37 lines (31 loc) · 1012 Bytes
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
import { LiveGateway } from '../live-render-express';
import { isEmail, isMobilePhone } from 'validator';
const gateway = new LiveGateway();
interface FormData {
name: string;
email: string;
phoneNumber: string;
}
type FormDataValidations = { [k in keyof FormData]?: string };
function validateFormInputs(data: FormData): FormDataValidations | undefined {
const validations: FormDataValidations = {};
if (data.email && !isEmail(data.email)) {
validations['email'] = 'This email is invalid';
}
if (data.phoneNumber && !isMobilePhone(data.phoneNumber, 'any')) {
validations['phoneNumber'] = 'This phone number is invalid';
}
if (Object.keys(validations).length > 0) {
return validations;
}
}
gateway.on('ready', client => {
client.on('formChange', message => {
if (message.type !== 'formChange') {
return;
}
const validationErrors = validateFormInputs(message.sender.data as any);
client.update({ validationErrors });
});
});
export default gateway;