regexes.cjs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. "use strict";
  2. var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
  3. if (k2 === undefined) k2 = k;
  4. var desc = Object.getOwnPropertyDescriptor(m, k);
  5. if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
  6. desc = { enumerable: true, get: function() { return m[k]; } };
  7. }
  8. Object.defineProperty(o, k2, desc);
  9. }) : (function(o, m, k, k2) {
  10. if (k2 === undefined) k2 = k;
  11. o[k2] = m[k];
  12. }));
  13. var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
  14. Object.defineProperty(o, "default", { enumerable: true, value: v });
  15. }) : function(o, v) {
  16. o["default"] = v;
  17. });
  18. var __importStar = (this && this.__importStar) || function (mod) {
  19. if (mod && mod.__esModule) return mod;
  20. var result = {};
  21. if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
  22. __setModuleDefault(result, mod);
  23. return result;
  24. };
  25. Object.defineProperty(exports, "__esModule", { value: true });
  26. exports.sha384_hex = exports.sha256_base64url = exports.sha256_base64 = exports.sha256_hex = exports.sha1_base64url = exports.sha1_base64 = exports.sha1_hex = exports.md5_base64url = exports.md5_base64 = exports.md5_hex = exports.hex = exports.uppercase = exports.lowercase = exports.undefined = exports.null = exports.boolean = exports.number = exports.integer = exports.bigint = exports.string = exports.date = exports.e164 = exports.domain = exports.hostname = exports.base64url = exports.base64 = exports.cidrv6 = exports.cidrv4 = exports.mac = exports.ipv6 = exports.ipv4 = exports.browserEmail = exports.idnEmail = exports.unicodeEmail = exports.rfc5322Email = exports.html5Email = exports.email = exports.uuid7 = exports.uuid6 = exports.uuid4 = exports.uuid = exports.guid = exports.extendedDuration = exports.duration = exports.nanoid = exports.ksuid = exports.xid = exports.ulid = exports.cuid2 = exports.cuid = void 0;
  27. exports.sha512_base64url = exports.sha512_base64 = exports.sha512_hex = exports.sha384_base64url = exports.sha384_base64 = void 0;
  28. exports.emoji = emoji;
  29. exports.time = time;
  30. exports.datetime = datetime;
  31. const util = __importStar(require("./util.cjs"));
  32. exports.cuid = /^[cC][^\s-]{8,}$/;
  33. exports.cuid2 = /^[0-9a-z]+$/;
  34. exports.ulid = /^[0-9A-HJKMNP-TV-Za-hjkmnp-tv-z]{26}$/;
  35. exports.xid = /^[0-9a-vA-V]{20}$/;
  36. exports.ksuid = /^[A-Za-z0-9]{27}$/;
  37. exports.nanoid = /^[a-zA-Z0-9_-]{21}$/;
  38. /** ISO 8601-1 duration regex. Does not support the 8601-2 extensions like negative durations or fractional/negative components. */
  39. exports.duration = /^P(?:(\d+W)|(?!.*W)(?=\d|T\d)(\d+Y)?(\d+M)?(\d+D)?(T(?=\d)(\d+H)?(\d+M)?(\d+([.,]\d+)?S)?)?)$/;
  40. /** Implements ISO 8601-2 extensions like explicit +- prefixes, mixing weeks with other units, and fractional/negative components. */
  41. exports.extendedDuration = /^[-+]?P(?!$)(?:(?:[-+]?\d+Y)|(?:[-+]?\d+[.,]\d+Y$))?(?:(?:[-+]?\d+M)|(?:[-+]?\d+[.,]\d+M$))?(?:(?:[-+]?\d+W)|(?:[-+]?\d+[.,]\d+W$))?(?:(?:[-+]?\d+D)|(?:[-+]?\d+[.,]\d+D$))?(?:T(?=[\d+-])(?:(?:[-+]?\d+H)|(?:[-+]?\d+[.,]\d+H$))?(?:(?:[-+]?\d+M)|(?:[-+]?\d+[.,]\d+M$))?(?:[-+]?\d+(?:[.,]\d+)?S)?)??$/;
  42. /** A regex for any UUID-like identifier: 8-4-4-4-12 hex pattern */
  43. exports.guid = /^([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12})$/;
  44. /** Returns a regex for validating an RFC 9562/4122 UUID.
  45. *
  46. * @param version Optionally specify a version 1-8. If no version is specified, all versions are supported. */
  47. const uuid = (version) => {
  48. if (!version)
  49. return /^([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-8][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}|00000000-0000-0000-0000-000000000000|ffffffff-ffff-ffff-ffff-ffffffffffff)$/;
  50. return new RegExp(`^([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-${version}[0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12})$`);
  51. };
  52. exports.uuid = uuid;
  53. exports.uuid4 = (0, exports.uuid)(4);
  54. exports.uuid6 = (0, exports.uuid)(6);
  55. exports.uuid7 = (0, exports.uuid)(7);
  56. /** Practical email validation */
  57. exports.email = /^(?!\.)(?!.*\.\.)([A-Za-z0-9_'+\-\.]*)[A-Za-z0-9_+-]@([A-Za-z0-9][A-Za-z0-9\-]*\.)+[A-Za-z]{2,}$/;
  58. /** Equivalent to the HTML5 input[type=email] validation implemented by browsers. Source: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/email */
  59. exports.html5Email = /^[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])?)*$/;
  60. /** The classic emailregex.com regex for RFC 5322-compliant emails */
  61. exports.rfc5322Email = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
  62. /** A loose regex that allows Unicode characters, enforces length limits, and that's about it. */
  63. exports.unicodeEmail = /^[^\s@"]{1,64}@[^\s@]{1,255}$/u;
  64. exports.idnEmail = exports.unicodeEmail;
  65. exports.browserEmail = /^[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])?)*$/;
  66. // from https://thekevinscott.com/emojis-in-javascript/#writing-a-regular-expression
  67. const _emoji = `^(\\p{Extended_Pictographic}|\\p{Emoji_Component})+$`;
  68. function emoji() {
  69. return new RegExp(_emoji, "u");
  70. }
  71. exports.ipv4 = /^(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])$/;
  72. exports.ipv6 = /^(([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:))$/;
  73. const mac = (delimiter) => {
  74. const escapedDelim = util.escapeRegex(delimiter ?? ":");
  75. return new RegExp(`^(?:[0-9A-F]{2}${escapedDelim}){5}[0-9A-F]{2}$|^(?:[0-9a-f]{2}${escapedDelim}){5}[0-9a-f]{2}$`);
  76. };
  77. exports.mac = mac;
  78. exports.cidrv4 = /^((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\/([0-9]|[1-2][0-9]|3[0-2])$/;
  79. exports.cidrv6 = /^(([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}|::|([0-9a-fA-F]{1,4})?::([0-9a-fA-F]{1,4}:?){0,6})\/(12[0-8]|1[01][0-9]|[1-9]?[0-9])$/;
  80. // https://stackoverflow.com/questions/7860392/determine-if-string-is-in-base64-using-javascript
  81. exports.base64 = /^$|^(?:[0-9a-zA-Z+/]{4})*(?:(?:[0-9a-zA-Z+/]{2}==)|(?:[0-9a-zA-Z+/]{3}=))?$/;
  82. exports.base64url = /^[A-Za-z0-9_-]*$/;
  83. // based on https://stackoverflow.com/questions/106179/regular-expression-to-match-dns-hostname-or-ip-address
  84. // export const hostname: RegExp = /^([a-zA-Z0-9-]+\.)*[a-zA-Z0-9-]+$/;
  85. exports.hostname = /^(?=.{1,253}\.?$)[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[-0-9a-zA-Z]{0,61}[0-9a-zA-Z])?)*\.?$/;
  86. exports.domain = /^([a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,}$/;
  87. // https://blog.stevenlevithan.com/archives/validate-phone-number#r4-3 (regex sans spaces)
  88. // E.164: leading digit must be 1-9; total digits (excluding '+') between 7-15
  89. exports.e164 = /^\+[1-9]\d{6,14}$/;
  90. // const dateSource = `((\\d\\d[2468][048]|\\d\\d[13579][26]|\\d\\d0[48]|[02468][048]00|[13579][26]00)-02-29|\\d{4}-((0[13578]|1[02])-(0[1-9]|[12]\\d|3[01])|(0[469]|11)-(0[1-9]|[12]\\d|30)|(02)-(0[1-9]|1\\d|2[0-8])))`;
  91. const dateSource = `(?:(?:\\d\\d[2468][048]|\\d\\d[13579][26]|\\d\\d0[48]|[02468][048]00|[13579][26]00)-02-29|\\d{4}-(?:(?:0[13578]|1[02])-(?:0[1-9]|[12]\\d|3[01])|(?:0[469]|11)-(?:0[1-9]|[12]\\d|30)|(?:02)-(?:0[1-9]|1\\d|2[0-8])))`;
  92. exports.date = new RegExp(`^${dateSource}$`);
  93. function timeSource(args) {
  94. const hhmm = `(?:[01]\\d|2[0-3]):[0-5]\\d`;
  95. const regex = typeof args.precision === "number"
  96. ? args.precision === -1
  97. ? `${hhmm}`
  98. : args.precision === 0
  99. ? `${hhmm}:[0-5]\\d`
  100. : `${hhmm}:[0-5]\\d\\.\\d{${args.precision}}`
  101. : `${hhmm}(?::[0-5]\\d(?:\\.\\d+)?)?`;
  102. return regex;
  103. }
  104. function time(args) {
  105. return new RegExp(`^${timeSource(args)}$`);
  106. }
  107. // Adapted from https://stackoverflow.com/a/3143231
  108. function datetime(args) {
  109. const time = timeSource({ precision: args.precision });
  110. const opts = ["Z"];
  111. if (args.local)
  112. opts.push("");
  113. // if (args.offset) opts.push(`([+-]\\d{2}:\\d{2})`);
  114. if (args.offset)
  115. opts.push(`([+-](?:[01]\\d|2[0-3]):[0-5]\\d)`);
  116. const timeRegex = `${time}(?:${opts.join("|")})`;
  117. return new RegExp(`^${dateSource}T(?:${timeRegex})$`);
  118. }
  119. const string = (params) => {
  120. const regex = params ? `[\\s\\S]{${params?.minimum ?? 0},${params?.maximum ?? ""}}` : `[\\s\\S]*`;
  121. return new RegExp(`^${regex}$`);
  122. };
  123. exports.string = string;
  124. exports.bigint = /^-?\d+n?$/;
  125. exports.integer = /^-?\d+$/;
  126. exports.number = /^-?\d+(?:\.\d+)?$/;
  127. exports.boolean = /^(?:true|false)$/i;
  128. const _null = /^null$/i;
  129. exports.null = _null;
  130. const _undefined = /^undefined$/i;
  131. exports.undefined = _undefined;
  132. // regex for string with no uppercase letters
  133. exports.lowercase = /^[^A-Z]*$/;
  134. // regex for string with no lowercase letters
  135. exports.uppercase = /^[^a-z]*$/;
  136. // regex for hexadecimal strings (any length)
  137. exports.hex = /^[0-9a-fA-F]*$/;
  138. // Hash regexes for different algorithms and encodings
  139. // Helper function to create base64 regex with exact length and padding
  140. function fixedBase64(bodyLength, padding) {
  141. return new RegExp(`^[A-Za-z0-9+/]{${bodyLength}}${padding}$`);
  142. }
  143. // Helper function to create base64url regex with exact length (no padding)
  144. function fixedBase64url(length) {
  145. return new RegExp(`^[A-Za-z0-9_-]{${length}}$`);
  146. }
  147. // MD5 (16 bytes): base64 = 24 chars total (22 + "==")
  148. exports.md5_hex = /^[0-9a-fA-F]{32}$/;
  149. exports.md5_base64 = fixedBase64(22, "==");
  150. exports.md5_base64url = fixedBase64url(22);
  151. // SHA1 (20 bytes): base64 = 28 chars total (27 + "=")
  152. exports.sha1_hex = /^[0-9a-fA-F]{40}$/;
  153. exports.sha1_base64 = fixedBase64(27, "=");
  154. exports.sha1_base64url = fixedBase64url(27);
  155. // SHA256 (32 bytes): base64 = 44 chars total (43 + "=")
  156. exports.sha256_hex = /^[0-9a-fA-F]{64}$/;
  157. exports.sha256_base64 = fixedBase64(43, "=");
  158. exports.sha256_base64url = fixedBase64url(43);
  159. // SHA384 (48 bytes): base64 = 64 chars total (no padding)
  160. exports.sha384_hex = /^[0-9a-fA-F]{96}$/;
  161. exports.sha384_base64 = fixedBase64(64, "");
  162. exports.sha384_base64url = fixedBase64url(64);
  163. // SHA512 (64 bytes): base64 = 88 chars total (86 + "==")
  164. exports.sha512_hex = /^[0-9a-fA-F]{128}$/;
  165. exports.sha512_base64 = fixedBase64(86, "==");
  166. exports.sha512_base64url = fixedBase64url(86);