-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathindex.ts
More file actions
83 lines (67 loc) · 2.13 KB
/
index.ts
File metadata and controls
83 lines (67 loc) · 2.13 KB
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
import { Result, ValueObject } from 'rich-domain';
import IsValidEmail from './email.validator.util';
export class Email extends ValueObject<string> {
protected static readonly BLOCKED_DOMAINS: Array<string> = [];
protected static readonly VALID_DOMAINS: Array<string> = [];
protected static readonly MESSAGE: string = 'Invalid email';
private constructor(props: string) {
super(props);
}
value(): string {
return this.props;
}
nick(): string {
return this.props.slice(0, this.props.indexOf('@'));
}
domain(): string {
return this.props
.slice(this.props.indexOf('@') + 1);
}
public static isValid(value: string): boolean {
return this.isValidProps(value);
}
/**
* @description validate email value
* @param email string
* @returns true if email value is valid and returns false if not.
*
* @requires email not greater than 256 char.
* @requires contain symbol @
* @requires contain [domain].[org].[optional country]
* @requires contain [nick letters] with [hifen or numbers]
* @requires starts [a-z]
* @requires ends [a-z]
*/
public static isValidProps(email: string): boolean {
const isValid = IsValidEmail(email);
if (!isValid) return false;
const domain = email.slice(email.indexOf('@') + 1).toLowerCase();
const isBlockedDomain = Email.BLOCKED_DOMAINS.map(
(blockedDomain) => blockedDomain.toLowerCase().includes(domain),
).includes(true);
if (Email.VALID_DOMAINS.length === 0) {
return !isBlockedDomain;
}
const isAvailable = Email.VALID_DOMAINS.map((freeDomain) =>
freeDomain.toLowerCase().includes(domain),
).includes(true);
return isAvailable && !isBlockedDomain;
}
/**
*
* @param value value as string
* @returns instance of Email or throw an error
*/
public static init(value: string): Email {
const isValidValue = Email.isValidProps(value);
if (!isValidValue) throw new Error(Email.MESSAGE);
return new Email(value.toLowerCase());
}
public static create(value: string): Result<Email | null> {
if (!Email.isValidProps(value)) {
return Result.fail(Email.MESSAGE);
}
return Result.Ok(new Email(value.toLowerCase()));
}
}
export default Email;