@arkyn/server 3.0.1-beta.99 → 3.0.2

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 (265) hide show
  1. package/LICENSE.txt +184 -1
  2. package/README.md +277 -224
  3. package/dist/http/api/_deleteRequest.d.ts +17 -0
  4. package/dist/http/api/_deleteRequest.d.ts.map +1 -0
  5. package/dist/http/api/_getRequest.d.ts +16 -0
  6. package/dist/http/api/_getRequest.d.ts.map +1 -0
  7. package/dist/http/api/_logMapperService.d.ts +86 -0
  8. package/dist/http/api/_logMapperService.d.ts.map +1 -0
  9. package/dist/{api/arkynLogRequest.d.ts → http/api/_logRequest.d.ts} +22 -19
  10. package/dist/{api/arkynLogRequest.d.ts.map → http/api/_logRequest.d.ts.map} +1 -1
  11. package/dist/http/api/_makeRequest.d.ts +61 -0
  12. package/dist/http/api/_makeRequest.d.ts.map +1 -0
  13. package/dist/http/api/_patchRequest.d.ts +17 -0
  14. package/dist/http/api/_patchRequest.d.ts.map +1 -0
  15. package/dist/http/api/_postRequest.d.ts +17 -0
  16. package/dist/http/api/_postRequest.d.ts.map +1 -0
  17. package/dist/http/api/_putRequest.d.ts +17 -0
  18. package/dist/http/api/_putRequest.d.ts.map +1 -0
  19. package/dist/http/badResponses/_badResponse.d.ts +21 -7
  20. package/dist/http/badResponses/_badResponse.d.ts.map +1 -1
  21. package/dist/http/badResponses/badGateway.d.ts +12 -23
  22. package/dist/http/badResponses/badGateway.d.ts.map +1 -1
  23. package/dist/http/badResponses/badRequest.d.ts +10 -23
  24. package/dist/http/badResponses/badRequest.d.ts.map +1 -1
  25. package/dist/http/badResponses/conflict.d.ts +10 -23
  26. package/dist/http/badResponses/conflict.d.ts.map +1 -1
  27. package/dist/http/badResponses/forbidden.d.ts +10 -23
  28. package/dist/http/badResponses/forbidden.d.ts.map +1 -1
  29. package/dist/http/badResponses/notFound.d.ts +10 -23
  30. package/dist/http/badResponses/notFound.d.ts.map +1 -1
  31. package/dist/http/badResponses/notImplemented.d.ts +10 -23
  32. package/dist/http/badResponses/notImplemented.d.ts.map +1 -1
  33. package/dist/http/badResponses/serverError.d.ts +10 -23
  34. package/dist/http/badResponses/serverError.d.ts.map +1 -1
  35. package/dist/http/badResponses/unauthorized.d.ts +10 -23
  36. package/dist/http/badResponses/unauthorized.d.ts.map +1 -1
  37. package/dist/http/badResponses/unprocessableEntity.d.ts +23 -33
  38. package/dist/http/badResponses/unprocessableEntity.d.ts.map +1 -1
  39. package/dist/http/successResponses/_successResponse.d.ts +21 -7
  40. package/dist/http/successResponses/_successResponse.d.ts.map +1 -1
  41. package/dist/http/successResponses/created.d.ts +11 -29
  42. package/dist/http/successResponses/created.d.ts.map +1 -1
  43. package/dist/http/successResponses/found.d.ts +11 -32
  44. package/dist/http/successResponses/found.d.ts.map +1 -1
  45. package/dist/http/successResponses/noContent.d.ts +10 -16
  46. package/dist/http/successResponses/noContent.d.ts.map +1 -1
  47. package/dist/http/successResponses/success.d.ts +11 -29
  48. package/dist/http/successResponses/success.d.ts.map +1 -1
  49. package/dist/http/successResponses/updated.d.ts +12 -29
  50. package/dist/http/successResponses/updated.d.ts.map +1 -1
  51. package/dist/index.d.ts +9 -10
  52. package/dist/index.d.ts.map +1 -1
  53. package/dist/index.js +1069 -38
  54. package/dist/index.js.map +1 -0
  55. package/dist/modules/http/api/_deleteRequest.js +15 -0
  56. package/dist/modules/http/api/_deleteRequest.js.map +1 -0
  57. package/dist/modules/http/api/_getRequest.js +14 -0
  58. package/dist/modules/http/api/_getRequest.js.map +1 -0
  59. package/dist/modules/http/api/_logMapperService.js +33 -0
  60. package/dist/modules/http/api/_logMapperService.js.map +1 -0
  61. package/dist/modules/http/api/_logRequest.js +58 -0
  62. package/dist/modules/http/api/_logRequest.js.map +1 -0
  63. package/dist/modules/http/api/_makeRequest.js +72 -0
  64. package/dist/modules/http/api/_makeRequest.js.map +1 -0
  65. package/dist/modules/http/api/_patchRequest.js +15 -0
  66. package/dist/modules/http/api/_patchRequest.js.map +1 -0
  67. package/dist/modules/http/api/_postRequest.js +15 -0
  68. package/dist/modules/http/api/_postRequest.js.map +1 -0
  69. package/dist/modules/http/api/_putRequest.js +15 -0
  70. package/dist/modules/http/api/_putRequest.js.map +1 -0
  71. package/dist/modules/http/badResponses/_badResponse.js +65 -0
  72. package/dist/modules/http/badResponses/_badResponse.js.map +1 -0
  73. package/dist/modules/http/badResponses/badGateway.js +26 -0
  74. package/dist/modules/http/badResponses/badGateway.js.map +1 -0
  75. package/dist/modules/http/badResponses/badRequest.js +26 -0
  76. package/dist/modules/http/badResponses/badRequest.js.map +1 -0
  77. package/dist/modules/http/badResponses/conflict.js +26 -0
  78. package/dist/modules/http/badResponses/conflict.js.map +1 -0
  79. package/dist/modules/http/badResponses/forbidden.js +26 -0
  80. package/dist/modules/http/badResponses/forbidden.js.map +1 -0
  81. package/dist/modules/http/badResponses/notFound.js +26 -0
  82. package/dist/modules/http/badResponses/notFound.js.map +1 -0
  83. package/dist/modules/http/badResponses/notImplemented.js +26 -0
  84. package/dist/modules/http/badResponses/notImplemented.js.map +1 -0
  85. package/dist/modules/http/badResponses/serverError.js +26 -0
  86. package/dist/modules/http/badResponses/serverError.js.map +1 -0
  87. package/dist/modules/http/badResponses/unauthorized.js +26 -0
  88. package/dist/modules/http/badResponses/unauthorized.js.map +1 -0
  89. package/dist/modules/http/badResponses/unprocessableEntity.js +30 -0
  90. package/dist/modules/http/badResponses/unprocessableEntity.js.map +1 -0
  91. package/dist/modules/http/successResponses/_successResponse.js +64 -0
  92. package/dist/modules/http/successResponses/_successResponse.js.map +1 -0
  93. package/dist/modules/http/successResponses/created.js +26 -0
  94. package/dist/modules/http/successResponses/created.js.map +1 -0
  95. package/dist/modules/http/successResponses/found.js +26 -0
  96. package/dist/modules/http/successResponses/found.js.map +1 -0
  97. package/dist/modules/http/successResponses/noContent.js +19 -0
  98. package/dist/modules/http/successResponses/noContent.js.map +1 -0
  99. package/dist/modules/http/successResponses/success.js +26 -0
  100. package/dist/modules/http/successResponses/success.js.map +1 -0
  101. package/dist/modules/http/successResponses/updated.js +26 -0
  102. package/dist/modules/http/successResponses/updated.js.map +1 -0
  103. package/dist/modules/services/apiService.js +111 -0
  104. package/dist/modules/services/apiService.js.map +1 -0
  105. package/dist/modules/services/debugService.js +38 -0
  106. package/dist/modules/services/debugService.js.map +1 -0
  107. package/dist/modules/services/logService.js +23 -0
  108. package/dist/modules/services/logService.js.map +1 -0
  109. package/dist/modules/utilities/decodeRequestBody.js +22 -0
  110. package/dist/modules/utilities/decodeRequestBody.js.map +1 -0
  111. package/dist/modules/utilities/decodeRequestErrorMessage.js +8 -0
  112. package/dist/modules/utilities/decodeRequestErrorMessage.js.map +1 -0
  113. package/dist/modules/utilities/errorHandler.js +41 -0
  114. package/dist/modules/utilities/errorHandler.js.map +1 -0
  115. package/dist/modules/utilities/flushDebugLogs.js +18 -0
  116. package/dist/modules/utilities/flushDebugLogs.js.map +1 -0
  117. package/dist/modules/utilities/formAsyncParse.js +16 -0
  118. package/dist/modules/utilities/formAsyncParse.js.map +1 -0
  119. package/dist/modules/utilities/formParse.js +16 -0
  120. package/dist/modules/utilities/formParse.js.map +1 -0
  121. package/dist/modules/utilities/getScopedParams.js +11 -0
  122. package/dist/modules/utilities/getScopedParams.js.map +1 -0
  123. package/dist/modules/utilities/schemaValidator.js +57 -0
  124. package/dist/modules/utilities/schemaValidator.js.map +1 -0
  125. package/dist/modules/validations/validateCep.js +11 -0
  126. package/dist/modules/validations/validateCep.js.map +1 -0
  127. package/dist/modules/validations/validateCnpj.js +56 -0
  128. package/dist/modules/validations/validateCnpj.js.map +1 -0
  129. package/dist/modules/validations/validateCpf.js +29 -0
  130. package/dist/modules/validations/validateCpf.js.map +1 -0
  131. package/dist/modules/validations/validateDate.js +28 -0
  132. package/dist/modules/validations/validateDate.js.map +1 -0
  133. package/dist/modules/validations/validateEmail.js +56 -0
  134. package/dist/modules/validations/validateEmail.js.map +1 -0
  135. package/dist/modules/validations/validatePassword.js +14 -0
  136. package/dist/modules/validations/validatePassword.js.map +1 -0
  137. package/dist/modules/validations/validatePhone.js +12 -0
  138. package/dist/modules/validations/validatePhone.js.map +1 -0
  139. package/dist/modules/validations/validateRg.js +10 -0
  140. package/dist/modules/validations/validateRg.js.map +1 -0
  141. package/dist/services/apiService.d.ts +25 -26
  142. package/dist/services/apiService.d.ts.map +1 -1
  143. package/dist/services/debugService.d.ts +15 -32
  144. package/dist/services/debugService.d.ts.map +1 -1
  145. package/dist/services/logService.d.ts +33 -0
  146. package/dist/services/logService.d.ts.map +1 -0
  147. package/dist/utilities/decodeRequestBody.d.ts +18 -0
  148. package/dist/utilities/decodeRequestBody.d.ts.map +1 -0
  149. package/dist/utilities/decodeRequestErrorMessage.d.ts +19 -0
  150. package/dist/utilities/decodeRequestErrorMessage.d.ts.map +1 -0
  151. package/dist/utilities/errorHandler.d.ts +25 -0
  152. package/dist/utilities/errorHandler.d.ts.map +1 -0
  153. package/dist/utilities/flushDebugLogs.d.ts +24 -0
  154. package/dist/utilities/flushDebugLogs.d.ts.map +1 -0
  155. package/dist/utilities/formAsyncParse.d.ts +38 -0
  156. package/dist/utilities/formAsyncParse.d.ts.map +1 -0
  157. package/dist/utilities/formParse.d.ts +38 -0
  158. package/dist/utilities/formParse.d.ts.map +1 -0
  159. package/dist/utilities/getScopedParams.d.ts +19 -0
  160. package/dist/utilities/getScopedParams.d.ts.map +1 -0
  161. package/dist/utilities/schemaValidator.d.ts +75 -0
  162. package/dist/utilities/schemaValidator.d.ts.map +1 -0
  163. package/dist/validations/validateCep.d.ts +6 -11
  164. package/dist/validations/validateCep.d.ts.map +1 -1
  165. package/dist/validations/validateCnpj.d.ts +2 -3
  166. package/dist/validations/validateCnpj.d.ts.map +1 -1
  167. package/dist/validations/validateCpf.d.ts +4 -12
  168. package/dist/validations/validateCpf.d.ts.map +1 -1
  169. package/dist/validations/validateDate.d.ts +13 -24
  170. package/dist/validations/validateDate.d.ts.map +1 -1
  171. package/dist/validations/validateEmail.d.ts +3 -4
  172. package/dist/validations/validateEmail.d.ts.map +1 -1
  173. package/dist/validations/validatePassword.d.ts +3 -4
  174. package/dist/validations/validatePassword.d.ts.map +1 -1
  175. package/dist/validations/validatePhone.d.ts +7 -20
  176. package/dist/validations/validatePhone.d.ts.map +1 -1
  177. package/dist/validations/validateRg.d.ts +1 -2
  178. package/dist/validations/validateRg.d.ts.map +1 -1
  179. package/package.json +317 -15
  180. package/dist/api/arkynLogRequest.js +0 -88
  181. package/dist/api/deleteRequest.d.ts +0 -13
  182. package/dist/api/deleteRequest.d.ts.map +0 -1
  183. package/dist/api/deleteRequest.js +0 -14
  184. package/dist/api/getRequest.d.ts +0 -12
  185. package/dist/api/getRequest.d.ts.map +0 -1
  186. package/dist/api/getRequest.js +0 -13
  187. package/dist/api/makeRequest.d.ts +0 -38
  188. package/dist/api/makeRequest.d.ts.map +0 -1
  189. package/dist/api/makeRequest.js +0 -107
  190. package/dist/api/patchRequest.d.ts +0 -13
  191. package/dist/api/patchRequest.d.ts.map +0 -1
  192. package/dist/api/patchRequest.js +0 -14
  193. package/dist/api/postRequest.d.ts +0 -13
  194. package/dist/api/postRequest.d.ts.map +0 -1
  195. package/dist/api/postRequest.js +0 -14
  196. package/dist/api/putRequest.d.ts +0 -13
  197. package/dist/api/putRequest.d.ts.map +0 -1
  198. package/dist/api/putRequest.js +0 -14
  199. package/dist/bundle.js +0 -1507
  200. package/dist/bundle.umd.cjs +0 -10
  201. package/dist/http/badResponses/_badResponse.js +0 -20
  202. package/dist/http/badResponses/badGateway.js +0 -58
  203. package/dist/http/badResponses/badRequest.js +0 -58
  204. package/dist/http/badResponses/conflict.js +0 -58
  205. package/dist/http/badResponses/forbidden.js +0 -58
  206. package/dist/http/badResponses/notFound.js +0 -58
  207. package/dist/http/badResponses/notImplemented.js +0 -58
  208. package/dist/http/badResponses/serverError.js +0 -58
  209. package/dist/http/badResponses/unauthorized.js +0 -58
  210. package/dist/http/badResponses/unprocessableEntity.js +0 -65
  211. package/dist/http/successResponses/_successResponse.js +0 -37
  212. package/dist/http/successResponses/created.js +0 -57
  213. package/dist/http/successResponses/found.js +0 -60
  214. package/dist/http/successResponses/noContent.js +0 -38
  215. package/dist/http/successResponses/success.js +0 -57
  216. package/dist/http/successResponses/updated.js +0 -57
  217. package/dist/mapper/arkynLogRequestMapper.d.ts +0 -30
  218. package/dist/mapper/arkynLogRequestMapper.d.ts.map +0 -1
  219. package/dist/mapper/arkynLogRequestMapper.js +0 -44
  220. package/dist/services/apiService.js +0 -133
  221. package/dist/services/arkynLogService.d.ts +0 -44
  222. package/dist/services/arkynLogService.d.ts.map +0 -1
  223. package/dist/services/arkynLogService.js +0 -46
  224. package/dist/services/debugService.js +0 -57
  225. package/dist/services/decodeErrorMessageFromRequest.d.ts +0 -17
  226. package/dist/services/decodeErrorMessageFromRequest.d.ts.map +0 -1
  227. package/dist/services/decodeErrorMessageFromRequest.js +0 -30
  228. package/dist/services/decodeRequestBody.d.ts +0 -17
  229. package/dist/services/decodeRequestBody.d.ts.map +0 -1
  230. package/dist/services/decodeRequestBody.js +0 -38
  231. package/dist/services/errorHandler.d.ts +0 -44
  232. package/dist/services/errorHandler.d.ts.map +0 -1
  233. package/dist/services/errorHandler.js +0 -93
  234. package/dist/services/flushDebugLogs.d.ts +0 -8
  235. package/dist/services/flushDebugLogs.d.ts.map +0 -1
  236. package/dist/services/flushDebugLogs.js +0 -20
  237. package/dist/services/formAsyncParse.d.ts +0 -59
  238. package/dist/services/formAsyncParse.d.ts.map +0 -1
  239. package/dist/services/formAsyncParse.js +0 -58
  240. package/dist/services/formParse.d.ts +0 -59
  241. package/dist/services/formParse.d.ts.map +0 -1
  242. package/dist/services/formParse.js +0 -58
  243. package/dist/services/getCaller.d.ts +0 -17
  244. package/dist/services/getCaller.d.ts.map +0 -1
  245. package/dist/services/getCaller.js +0 -60
  246. package/dist/services/getScopedParams.d.ts +0 -28
  247. package/dist/services/getScopedParams.d.ts.map +0 -1
  248. package/dist/services/getScopedParams.js +0 -34
  249. package/dist/services/measureRouteExecution.d.ts +0 -3
  250. package/dist/services/measureRouteExecution.d.ts.map +0 -1
  251. package/dist/services/measureRouteExecution.js +0 -24
  252. package/dist/services/schemaValidator.d.ts +0 -158
  253. package/dist/services/schemaValidator.d.ts.map +0 -1
  254. package/dist/services/schemaValidator.js +0 -207
  255. package/dist/types/ApiResponseDTO.d.ts +0 -17
  256. package/dist/types/ApiResponseDTO.d.ts.map +0 -1
  257. package/dist/types/ApiResponseDTO.js +0 -1
  258. package/dist/validations/validateCep.js +0 -33
  259. package/dist/validations/validateCnpj.js +0 -52
  260. package/dist/validations/validateCpf.js +0 -54
  261. package/dist/validations/validateDate.js +0 -73
  262. package/dist/validations/validateEmail.js +0 -123
  263. package/dist/validations/validatePassword.js +0 -34
  264. package/dist/validations/validatePhone.js +0 -44
  265. package/dist/validations/validateRg.js +0 -31
@@ -1,123 +0,0 @@
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(
73
- // domain: string,
74
- // recordType: string
75
- // ): Promise<boolean> {
76
- // try {
77
- // await resolveDns(domain, recordType);
78
- // return true;
79
- // } catch {
80
- // return false;
81
- // }
82
- // }
83
- // Validates if domain has valid DNS records (MX, A, or AAAA records)
84
- // async function isValidDns(domain: string): Promise<boolean> {
85
- // for (const recordType of DNS_RECORD_TYPES) {
86
- // const hasRecord = await tryResolveDnsRecord(domain, recordType);
87
- // if (hasRecord) return true;
88
- // }
89
- // return false;
90
- // }
91
- /**
92
- * Validates if an email address is valid in all possible ways, including DNS validation.
93
- *
94
- * This function performs comprehensive email validation by:
95
- * - Checking basic email format and syntax
96
- * - Validating advanced RFC 5322 compliance rules
97
- * - Verifying that the domain has valid MX or A records in DNS
98
- *
99
- * @param rawEmail - The email address string to validate
100
- * @returns A promise that resolves to `true` if the email is valid (including DNS), otherwise `false`
101
- *
102
- * @example
103
- * ```typescript
104
- * await validateEmail("user@gmail.com"); // true
105
- * await validateEmail("user@gmil.com"); // false (invalid domain)
106
- * await validateEmail("invalid-email"); // false (invalid format)
107
- * ```
108
- */
109
- const validateEmail = async (rawEmail) => {
110
- if (!rawEmail || typeof rawEmail !== "string")
111
- return false;
112
- const email = rawEmail.trim();
113
- if (!isValidBasicFormat(email))
114
- return false;
115
- if (!isValidAdvancedSyntax(email))
116
- return false;
117
- const domain = extractDomain(email);
118
- if (!domain)
119
- return false;
120
- // return await isValidDns(domain);
121
- return true;
122
- };
123
- export { validateEmail };
@@ -1,34 +0,0 @@
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 };
@@ -1,44 +0,0 @@
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 };
@@ -1,31 +0,0 @@
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 };