-
-
Notifications
You must be signed in to change notification settings - Fork 227
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,8 @@ | ||
/** | ||
* Non-digit regex. | ||
*/ | ||
const NON_DIGIT_REGEX = /\D/gu; | ||
|
||
/** | ||
* Checks whether a string with numbers corresponds to the luhn algorithm. | ||
* | ||
|
@@ -7,7 +12,7 @@ | |
*/ | ||
export function isLuhnAlgo(input: string) { | ||
// Remove any non-digit chars | ||
const number = input.replace(/\D/gu, ''); | ||
const number = input.replace(NON_DIGIT_REGEX, ''); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. needs reason is: a global regex ( |
||
|
||
// Create necessary variables | ||
let length = number.length; | ||
|
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); | ||
} |
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); | ||
} |
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.