-
-
Notifications
You must be signed in to change notification settings - Fork 226
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add export for any validation regex #219
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.