@arkyn/server 3.0.1-beta.73 → 3.0.1-beta.74

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.
@@ -0,0 +1,121 @@
1
+ import dns from "node:dns";
2
+ const resolveDns = dns.promises.resolve;
3
+ // Validates basic email format using a comprehensive regex pattern
4
+ function isValidBasicFormat(email) {
5
+ const emailRegex = /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/;
6
+ return emailRegex.test(email);
7
+ }
8
+ // Validates the local part of email (before @)
9
+ function isValidLocalPart(localPart) {
10
+ if (localPart.length === 0 || localPart.length > 64)
11
+ return false;
12
+ if (localPart.startsWith(".") || localPart.endsWith("."))
13
+ return false;
14
+ if (localPart.includes(".."))
15
+ return false;
16
+ const validLocalRegex = /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+$/;
17
+ if (!validLocalRegex.test(localPart))
18
+ return false;
19
+ return true;
20
+ }
21
+ // Validates a single domain label
22
+ function isValidDomainLabel(label) {
23
+ if (label.length === 0 || label.length > 63)
24
+ return false;
25
+ if (label.startsWith("-") || label.endsWith("-"))
26
+ return false;
27
+ if (!/^[a-zA-Z0-9-]+$/.test(label))
28
+ return false;
29
+ return true;
30
+ }
31
+ // Validates the domain part of email (after @)
32
+ function isValidDomainPart(domainPart) {
33
+ if (domainPart.length === 0 || domainPart.length > 253)
34
+ return false;
35
+ if (domainPart.startsWith(".") ||
36
+ domainPart.endsWith(".") ||
37
+ domainPart.startsWith("-") ||
38
+ domainPart.endsWith("-")) {
39
+ return false;
40
+ }
41
+ const labels = domainPart.split(".");
42
+ if (labels.length < 2)
43
+ return false;
44
+ for (const label of labels)
45
+ if (!isValidDomainLabel(label))
46
+ return false;
47
+ const tld = labels[labels.length - 1];
48
+ if (tld.length < 2 || !/^[a-zA-Z]+$/.test(tld))
49
+ return false;
50
+ return true;
51
+ }
52
+ // Performs advanced syntax validation beyond basic regex
53
+ function isValidAdvancedSyntax(email) {
54
+ const parts = email.split("@");
55
+ if (parts.length !== 2)
56
+ return false;
57
+ const [localPart, domainPart] = parts;
58
+ if (!isValidLocalPart(localPart))
59
+ return false;
60
+ if (!isValidDomainPart(domainPart))
61
+ return false;
62
+ return true;
63
+ }
64
+ // Extracts the domain from email address
65
+ function extractDomain(email) {
66
+ const parts = email.split("@");
67
+ return parts.length === 2 ? parts[1].toLowerCase() : null;
68
+ }
69
+ // DNS record types to check for domain validation
70
+ const DNS_RECORD_TYPES = ["MX", "A", "AAAA"];
71
+ // Attempts to resolve a specific DNS record type for a domain
72
+ async function tryResolveDnsRecord(domain, recordType) {
73
+ try {
74
+ await resolveDns(domain, recordType);
75
+ return true;
76
+ }
77
+ catch {
78
+ return false;
79
+ }
80
+ }
81
+ // Validates if domain has valid DNS records (MX, A, or AAAA records)
82
+ async function isValidDns(domain) {
83
+ for (const recordType of DNS_RECORD_TYPES) {
84
+ const hasRecord = await tryResolveDnsRecord(domain, recordType);
85
+ if (hasRecord)
86
+ return true;
87
+ }
88
+ return false;
89
+ }
90
+ /**
91
+ * Validates if an email address is valid in all possible ways, including DNS validation.
92
+ *
93
+ * This function performs comprehensive email validation by:
94
+ * - Checking basic email format and syntax
95
+ * - Validating advanced RFC 5322 compliance rules
96
+ * - Verifying that the domain has valid MX or A records in DNS
97
+ *
98
+ * @param rawEmail - The email address string to validate
99
+ * @returns A promise that resolves to `true` if the email is valid (including DNS), otherwise `false`
100
+ *
101
+ * @example
102
+ * ```typescript
103
+ * await validateEmail("user@gmail.com"); // true
104
+ * await validateEmail("user@gmil.com"); // false (invalid domain)
105
+ * await validateEmail("invalid-email"); // false (invalid format)
106
+ * ```
107
+ */
108
+ const validateEmail = async (rawEmail) => {
109
+ if (!rawEmail || typeof rawEmail !== "string")
110
+ return false;
111
+ const email = rawEmail.trim();
112
+ if (!isValidBasicFormat(email))
113
+ return false;
114
+ if (!isValidAdvancedSyntax(email))
115
+ return false;
116
+ const domain = extractDomain(email);
117
+ if (!domain)
118
+ return false;
119
+ return await isValidDns(domain);
120
+ };
121
+ export { validateEmail };
@@ -0,0 +1,21 @@
1
+ type ValidatePasswordFunction = (rawPassword: string) => boolean;
2
+ /**
3
+ * Validates a password based on the following rules:
4
+ * - At least 8 characters
5
+ * - At least 1 uppercase letter
6
+ * - At least 1 letter (any case)
7
+ * - At least 1 number
8
+ * - At least 1 special character
9
+ *
10
+ * @param rawPassword - The raw password string.
11
+ * @returns `true` if password is valid, otherwise `false`.
12
+ *
13
+ * @example
14
+ * ```ts
15
+ * validatePassword("Senha@123"); // true
16
+ * validatePassword("senha123"); // false (no uppercase, no special char)
17
+ * ```
18
+ */
19
+ declare const validatePassword: ValidatePasswordFunction;
20
+ export { validatePassword };
21
+ //# sourceMappingURL=validatePassword.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"validatePassword.d.ts","sourceRoot":"","sources":["../../src/validations/validatePassword.ts"],"names":[],"mappings":"AAAA,KAAK,wBAAwB,GAAG,CAAC,WAAW,EAAE,MAAM,KAAK,OAAO,CAAC;AAEjE;;;;;;;;;;;;;;;;GAgBG;AAEH,QAAA,MAAM,gBAAgB,EAAE,wBAkBvB,CAAC;AAEF,OAAO,EAAE,gBAAgB,EAAE,CAAC"}
@@ -0,0 +1,34 @@
1
+ /**
2
+ * Validates a password based on the following rules:
3
+ * - At least 8 characters
4
+ * - At least 1 uppercase letter
5
+ * - At least 1 letter (any case)
6
+ * - At least 1 number
7
+ * - At least 1 special character
8
+ *
9
+ * @param rawPassword - The raw password string.
10
+ * @returns `true` if password is valid, otherwise `false`.
11
+ *
12
+ * @example
13
+ * ```ts
14
+ * validatePassword("Senha@123"); // true
15
+ * validatePassword("senha123"); // false (no uppercase, no special char)
16
+ * ```
17
+ */
18
+ const validatePassword = (rawPassword) => {
19
+ if (!rawPassword)
20
+ return false;
21
+ const hasMinLength = rawPassword.length >= 8;
22
+ const hasUppercase = /[A-Z]/.test(rawPassword);
23
+ const hasLetter = /[a-z]/.test(rawPassword);
24
+ const hasNumber = /\d/.test(rawPassword);
25
+ const hasSpecialChar = /[!@#$%^&*(),.?":{}|<>_\-+=~`[\]\\\/]/.test(rawPassword);
26
+ return [
27
+ hasMinLength,
28
+ hasUppercase,
29
+ hasLetter,
30
+ hasNumber,
31
+ hasSpecialChar,
32
+ ].every((condition) => condition);
33
+ };
34
+ export { validatePassword };
@@ -0,0 +1,29 @@
1
+ type ValidatePhoneFunction = (rawPhone: string) => boolean;
2
+ /**
3
+ * Validates a phone number against a list of country-specific formats.
4
+ *
5
+ * The function iterates through a predefined list of countries and checks if the
6
+ * provided phone number matches the format for any of the countries. It uses
7
+ * regular expressions to validate the phone number based on the country's code,
8
+ * prefix, and mask.
9
+ *
10
+ * Special handling is applied for Brazilian phone numbers (ISO code "BR"), which
11
+ * allows for an optional ninth digit.
12
+ *
13
+ * @param rawPhone - The phone number to validate as a string.
14
+ * @returns `true` if the phone number matches any country's format, otherwise `false`.
15
+ *
16
+ * @example
17
+ * ```typescript
18
+ * import { validatePhone } from "./validatePhone";
19
+ *
20
+ * validatePhone("+55 32912345678"); // true for a valid Brazilian phone number
21
+ * validatePhone("+55 3212345678"); // true for a valid Brazilian phone number
22
+ * validatePhone("+1-684 1234567"); // true for a valid American Samoa phone number
23
+ * validatePhone("+5532912345678"); // false for an invalid Brazilian phone number
24
+ * validatePhone("+55 1234567890"); // false for an invalid Brazilian phone number
25
+ * ```
26
+ */
27
+ declare const validatePhone: ValidatePhoneFunction;
28
+ export { validatePhone };
29
+ //# sourceMappingURL=validatePhone.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"validatePhone.d.ts","sourceRoot":"","sources":["../../src/validations/validatePhone.ts"],"names":[],"mappings":"AAEA,KAAK,qBAAqB,GAAG,CAAC,QAAQ,EAAE,MAAM,KAAK,OAAO,CAAC;AAE3D;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AAEH,QAAA,MAAM,aAAa,EAAE,qBAiBpB,CAAC;AAEF,OAAO,EAAE,aAAa,EAAE,CAAC"}
@@ -0,0 +1,44 @@
1
+ import { countries } from "@arkyn/templates";
2
+ /**
3
+ * Validates a phone number against a list of country-specific formats.
4
+ *
5
+ * The function iterates through a predefined list of countries and checks if the
6
+ * provided phone number matches the format for any of the countries. It uses
7
+ * regular expressions to validate the phone number based on the country's code,
8
+ * prefix, and mask.
9
+ *
10
+ * Special handling is applied for Brazilian phone numbers (ISO code "BR"), which
11
+ * allows for an optional ninth digit.
12
+ *
13
+ * @param rawPhone - The phone number to validate as a string.
14
+ * @returns `true` if the phone number matches any country's format, otherwise `false`.
15
+ *
16
+ * @example
17
+ * ```typescript
18
+ * import { validatePhone } from "./validatePhone";
19
+ *
20
+ * validatePhone("+55 32912345678"); // true for a valid Brazilian phone number
21
+ * validatePhone("+55 3212345678"); // true for a valid Brazilian phone number
22
+ * validatePhone("+1-684 1234567"); // true for a valid American Samoa phone number
23
+ * validatePhone("+5532912345678"); // false for an invalid Brazilian phone number
24
+ * validatePhone("+55 1234567890"); // false for an invalid Brazilian phone number
25
+ * ```
26
+ */
27
+ const validatePhone = (rawPhone) => {
28
+ for (const country of countries) {
29
+ const countryCode = country.code;
30
+ const prefix = country.prefix ? `-${country.prefix}` : "";
31
+ const digitCount = country.mask.replace(/[^_]/g, "").length;
32
+ if (country.iso === "BR") {
33
+ const brazilRegex = new RegExp(`^\\${countryCode} \\d{2}9?\\d{8}$`);
34
+ if (brazilRegex.test(rawPhone))
35
+ return true;
36
+ continue;
37
+ }
38
+ const regex = new RegExp(`^\\${countryCode}${prefix} \\d{${digitCount}}$`);
39
+ if (regex.test(rawPhone))
40
+ return true;
41
+ }
42
+ return false;
43
+ };
44
+ export { validatePhone };
@@ -0,0 +1,22 @@
1
+ type ValidateRgFunction = (rawRg: string) => boolean;
2
+ /**
3
+ * Validates a Brazilian RG (Registro Geral) in a generic way.
4
+ *
5
+ * This function does a basic structure validation:
6
+ * - Removes non-alphanumeric characters.
7
+ * - Ensures length is reasonable (7–9 digits).
8
+ * - Optionally allows for a final letter (verifier).
9
+ *
10
+ * @param rawRg - RG string, possibly formatted.
11
+ * @returns `true` if format seems valid, otherwise `false`.
12
+ *
13
+ * @example
14
+ * ```ts
15
+ * validateRg("12.345.678-9"); // true
16
+ * validateRg("MG-12.345.678"); // false (not supported)
17
+ * validateRg("12345678X"); // true
18
+ * ```
19
+ */
20
+ declare const validateRg: ValidateRgFunction;
21
+ export { validateRg };
22
+ //# sourceMappingURL=validateRg.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"validateRg.d.ts","sourceRoot":"","sources":["../../src/validations/validateRg.ts"],"names":[],"mappings":"AAAA,KAAK,kBAAkB,GAAG,CAAC,KAAK,EAAE,MAAM,KAAK,OAAO,CAAC;AAErD;;;;;;;;;;;;;;;;;GAiBG;AAEH,QAAA,MAAM,UAAU,EAAE,kBAajB,CAAC;AAEF,OAAO,EAAE,UAAU,EAAE,CAAC"}
@@ -0,0 +1,31 @@
1
+ /**
2
+ * Validates a Brazilian RG (Registro Geral) in a generic way.
3
+ *
4
+ * This function does a basic structure validation:
5
+ * - Removes non-alphanumeric characters.
6
+ * - Ensures length is reasonable (7–9 digits).
7
+ * - Optionally allows for a final letter (verifier).
8
+ *
9
+ * @param rawRg - RG string, possibly formatted.
10
+ * @returns `true` if format seems valid, otherwise `false`.
11
+ *
12
+ * @example
13
+ * ```ts
14
+ * validateRg("12.345.678-9"); // true
15
+ * validateRg("MG-12.345.678"); // false (not supported)
16
+ * validateRg("12345678X"); // true
17
+ * ```
18
+ */
19
+ const validateRg = (rawRg) => {
20
+ if (!rawRg)
21
+ return false;
22
+ const validFormat = /^[0-9a-zA-Z.-]+$/.test(rawRg);
23
+ if (!validFormat)
24
+ return false;
25
+ const rg = rawRg.replace(/[^a-zA-Z0-9]/g, "");
26
+ if (rg.length < 7 || rg.length > 9)
27
+ return false;
28
+ const isValidFormat = /^[0-9]{7,8}[0-9Xx]?$/.test(rg);
29
+ return isValidFormat;
30
+ };
31
+ export { validateRg };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@arkyn/server",
3
- "version": "3.0.1-beta.73",
3
+ "version": "3.0.1-beta.74",
4
4
  "author": "Arkyn | Lucas Gonçalves",
5
5
  "main": "./dist/bundle.js",
6
6
  "module": "./dist/bundle.js",
@@ -31,9 +31,10 @@
31
31
  "test": "vitest --config vitest.config.ts",
32
32
  "typecheck": "bunx tsc --project tsconfig.json --noEmit"
33
33
  },
34
- "dependencies": {
34
+ "peerDependencies": {
35
35
  "@arkyn/shared": "*",
36
- "zod": "^4.0.17"
36
+ "@arkyn/templates": "*",
37
+ "zod": ">=4.0.17"
37
38
  },
38
39
  "devDependencies": {
39
40
  "bun-types": "latest",