-
-
Notifications
You must be signed in to change notification settings - Fork 189
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #219 from fabian-hiller/feat/export-regex
Add export for any validation regex
- Loading branch information
Showing
19 changed files
with
117 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/** | ||
* [cuid2](https://github.com/paralleldrive/cuid2#cuid2) regex. | ||
*/ | ||
export const CUID2_REGEX = /^[a-z][\da-z]*$/u; | ||
|
||
/** | ||
* Email regex. | ||
*/ | ||
export const EMAIL_REGEX = | ||
/^[\w+-]+(?:\.[\w+-]+)*@[\da-z]+(?:[.-][\da-z]+)*\.[a-z]{2,}$/iu; | ||
|
||
/** | ||
* Emoji regex. | ||
*/ | ||
export const EMOJI_REGEX = /^[\p{Extended_Pictographic}\p{Emoji_Component}]+$/u; | ||
|
||
/** | ||
* [IMEI](https://en.wikipedia.org/wiki/International_Mobile_Equipment_Identity) regex. | ||
*/ | ||
export const IMEI_REGEX = /^\d{2}(?:[ /|-]?\d{6}){2}[ /|-]?\d$/u; | ||
|
||
/** | ||
* [IPv4](https://en.wikipedia.org/wiki/IPv4) regex. | ||
*/ | ||
// eslint-disable-next-line redos-detector/no-unsafe-regex -- false positive | ||
export const IPV4_REGEX = /^(?:(?:25[0-5]|(?:2[0-4]|1\d|[1-9])?\d)\.?\b){4}$/u; | ||
|
||
/** | ||
* [IPv6](https://en.wikipedia.org/wiki/IPv6) regex. | ||
*/ | ||
export const IPV6_REGEX = | ||
/^(?:(?:[\da-f]{1,4}:){7}[\da-f]{1,4}|(?:[\da-f]{1,4}:){1,7}:|(?:[\da-f]{1,4}:){1,6}:[\da-f]{1,4}|(?:[\da-f]{1,4}:){1,5}(?::[\da-f]{1,4}){1,2}|(?:[\da-f]{1,4}:){1,4}(?::[\da-f]{1,4}){1,3}|(?:[\da-f]{1,4}:){1,3}(?::[\da-f]{1,4}){1,4}|(?:[\da-f]{1,4}:){1,2}(?::[\da-f]{1,4}){1,5}|[\da-f]{1,4}:(?::[\da-f]{1,4}){1,6}|:(?:(?::[\da-f]{1,4}){1,7}|:)|fe80:(?::[\da-f]{0,4}){0,4}%[\da-z]+|::(?:f{4}(?::0{1,4})?:)?(?:(?:25[0-5]|(?:2[0-4]|1?\d)?\d)\.){3}(?:25[0-5]|(?:2[0-4]|1?\d)?\d)|(?:[\da-f]{1,4}:){1,4}:(?:(?:25[0-5]|(?:2[0-4]|1?\d)?\d)\.){3}(?:25[0-5]|(?:2[0-4]|1?\d)?\d))$/iu; | ||
|
||
/** | ||
* [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) date regex. | ||
*/ | ||
export const ISO_DATE_REGEX = | ||
/^\d{4}-(?:0[1-9]|1[0-2])-(?:[12]\d|0[1-9]|3[01])$/u; | ||
|
||
/** | ||
* [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) date-time regex. | ||
*/ | ||
export const ISO_DATE_TIME_REGEX = | ||
/^\d{4}-(?:0[1-9]|1[0-2])-(?:[12]\d|0[1-9]|3[01])T(?:0\d|1\d|2[0-3]):[0-5]\d$/u; | ||
|
||
/** | ||
* [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) time regex. | ||
*/ | ||
export const ISO_TIME_REGEX = /^(?:0\d|1\d|2[0-3]):[0-5]\d$/u; | ||
|
||
/** | ||
* [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) time with seconds regex. | ||
*/ | ||
export const ISO_TIME_SECOND_REGEX = /^(?:0\d|1\d|2[0-3])(?::[0-5]\d){2}$/u; | ||
|
||
/** | ||
* [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) timestamp regex. | ||
*/ | ||
export const ISO_TIMESTAMP_REGEX = | ||
/^\d{4}-(?:0[1-9]|1[0-2])-(?:[12]\d|0[1-9]|3[01])T(?:0\d|1\d|2[0-3])(?::[0-5]\d){2}\.\d{3}Z$/u; | ||
|
||
/** | ||
* [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) week regex. | ||
*/ | ||
export const ISO_WEEK_REGEX = /^\d{4}-W(?:0[1-9]|[1-4]\d|5[0-3])$/u; | ||
|
||
/** | ||
* [ULID](https://github.com/ulid/spec) regex. | ||
*/ | ||
export const ULID_REGEX = /^[\da-hjkmnp-tv-z]{26}$/iu; | ||
|
||
/** | ||
* [UUID](https://en.wikipedia.org/wiki/Universally_unique_identifier) regex. | ||
*/ | ||
export const UUID_REGEX = /^[\da-f]{8}(?:-[\da-f]{4}){3}-[\da-f]{12}$/iu; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,18 @@ | ||
import { IPV4_REGEX, IPV6_REGEX } from '../../regex.ts'; | ||
import type { ErrorMessage, PipeResult } from '../../types.ts'; | ||
import { getOutput, getPipeIssues } from '../../utils/index.ts'; | ||
|
||
/** | ||
* Creates a validation function that validates an IP v4 or v6 address. | ||
* Creates a validation function that validates an [IPv4](https://en.wikipedia.org/wiki/IPv4) | ||
* or [IPv6](https://en.wikipedia.org/wiki/IPv6) address. | ||
* | ||
* @param error The error message. | ||
* | ||
* @returns A validation function. | ||
*/ | ||
export function ip<TInput extends string>(error?: ErrorMessage) { | ||
return (input: TInput): PipeResult<TInput> => | ||
// eslint-disable-next-line redos-detector/no-unsafe-regex -- false positive | ||
!/^(?:(?:25[0-5]|(?:2[0-4]|1\d|[1-9])?\d)\.?\b){4}$/u.test(input) && | ||
!/^(?:(?:[\da-f]{1,4}:){7}[\da-f]{1,4}|(?:[\da-f]{1,4}:){1,7}:|(?:[\da-f]{1,4}:){1,6}:[\da-f]{1,4}|(?:[\da-f]{1,4}:){1,5}(?::[\da-f]{1,4}){1,2}|(?:[\da-f]{1,4}:){1,4}(?::[\da-f]{1,4}){1,3}|(?:[\da-f]{1,4}:){1,3}(?::[\da-f]{1,4}){1,4}|(?:[\da-f]{1,4}:){1,2}(?::[\da-f]{1,4}){1,5}|[\da-f]{1,4}:(?::[\da-f]{1,4}){1,6}|:(?:(?::[\da-f]{1,4}){1,7}|:)|fe80:(?::[\da-f]{0,4}){0,4}%[\da-z]+|::(?:f{4}(?::0{1,4})?:)?(?:(?:25[0-5]|(?:2[0-4]|1?\d)?\d)\.){3}(?:25[0-5]|(?:2[0-4]|1?\d)?\d)|(?:[\da-f]{1,4}:){1,4}:(?:(?:25[0-5]|(?:2[0-4]|1?\d)?\d)\.){3}(?:25[0-5]|(?:2[0-4]|1?\d)?\d))$/iu.test( | ||
input | ||
) | ||
!IPV4_REGEX.test(input) && !IPV6_REGEX.test(input) | ||
? getPipeIssues('ip', error || 'Invalid IP', input) | ||
: getOutput(input); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,17 @@ | ||
import { UUID_REGEX } from '../../regex.ts'; | ||
import type { ErrorMessage, PipeResult } from '../../types.ts'; | ||
import { getOutput, getPipeIssues } from '../../utils/index.ts'; | ||
|
||
/** | ||
* Creates a validation function that validates a UUID. | ||
* Creates a validation function that validates a [UUID](https://en.wikipedia.org/wiki/Universally_unique_identifier). | ||
* | ||
* @param error The error message. | ||
* | ||
* @returns A validation function. | ||
*/ | ||
export function uuid<TInput extends string>(error?: ErrorMessage) { | ||
return (input: TInput): PipeResult<TInput> => | ||
!/^[\da-f]{8}(?:-[\da-f]{4}){3}-[\da-f]{12}$/iu.test(input) | ||
!UUID_REGEX.test(input) | ||
? getPipeIssues('uuid', error || 'Invalid UUID', input) | ||
: getOutput(input); | ||
} |