Skip to content

Commit 35e589d

Browse files
PleBeabraaar
andauthored
feat: update UUID validation to support versions 1-8, nil, max, and all (#2647)
* fix: update UUID validation to support versions 1-8, nil, max, and all * feat: add loose UUID validation and update documentation * feat: enhance isUUID function to support array of versions and add corresponding tests * test: update loose UUID validation tests with new valid and invalid cases * Enhance @IsUUID description for version support Updated the @IsUUID description to include support for an array of versions. --------- Co-authored-by: Brage Sekse Aarset <brage.aarset@gmail.com>
1 parent 042fb57 commit 35e589d

3 files changed

Lines changed: 448 additions & 9 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -930,7 +930,7 @@ isBoolean(value);
930930
| `@IsTaxId()` | Checks if the string is a valid tax ID. Default locale is `en-US`. |
931931
| `@IsUrl(options?: IsURLOptions)` | Checks if the string is a URL. |
932932
| `@IsMagnetURI()` | Checks if the string is a [magnet uri format](https://en.wikipedia.org/wiki/Magnet_URI_scheme). |
933-
| `@IsUUID(version?: UUIDVersion)` | Checks if the string is a UUID (version 3, 4, 5 or all ). |
933+
| `@IsUUID(version?: UUIDVersion)` | Checks if the string is a UUID (version 1-8, nil, max, loose, all). Also accepts array of versions. |
934934
| `@IsFirebasePushId()` | Checks if the string is a [Firebase Push ID](https://firebase.googleblog.com/2015/02/the-2120-ways-to-ensure-unique_68.html) |
935935
| `@IsUppercase()` | Checks if the string is uppercase. |
936936
| `@Length(min: number, max?: number)` | Checks if the string's length falls in a range. |

src/decorator/string/IsUUID.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,30 @@ import * as ValidatorJS from 'validator';
55

66
export const IS_UUID = 'isUuid';
77

8+
export type IsUUIDVersion = ValidatorJS.UUIDVersion | ValidatorJS.UUIDVersion[];
9+
810
/**
9-
* Checks if the string is a UUID (version 3, 4 or 5).
11+
* Checks if the string is a UUID (version 1-8, nil, max, loose, all).
1012
* If given value is not a string, then it returns false.
13+
* Supports single version or array of versions.
1114
*/
12-
export function isUUID(value: unknown, version?: ValidatorJS.UUIDVersion): boolean {
13-
return typeof value === 'string' && isUuidValidator(value, version);
15+
export function isUUID(value: unknown, version?: IsUUIDVersion): boolean {
16+
if (typeof value !== 'string') return false;
17+
if (Array.isArray(version)) {
18+
for (const v of version) {
19+
if (isUuidValidator(value, v)) return true;
20+
}
21+
return false;
22+
}
23+
return isUuidValidator(value, version);
1424
}
1525

1626
/**
17-
* Checks if the string is a UUID (version 3, 4 or 5).
27+
* Checks if the string is a UUID (version 1-8, nil, max, loose, all).
1828
* If given value is not a string, then it returns false.
29+
* Supports single version or array of versions.
1930
*/
20-
export function IsUUID(version?: ValidatorJS.UUIDVersion, validationOptions?: ValidationOptions): PropertyDecorator {
31+
export function IsUUID(version?: IsUUIDVersion, validationOptions?: ValidationOptions): PropertyDecorator {
2132
return ValidateBy(
2233
{
2334
name: IS_UUID,

0 commit comments

Comments
 (0)