Skip to content

Commit

Permalink
feat: use official type for version in @IsUUID decorator (#1846)
Browse files Browse the repository at this point in the history
  • Loading branch information
NoNameProvided committed Dec 9, 2022
1 parent b0d4a23 commit a33eb7f
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -887,7 +887,7 @@ isBoolean(value);
| `@IsTaxId()` | Checks if the string is a valid tax ID. Default locale is `en-US`.
| `@IsUrl(options?: IsURLOptions)` | Checks if the string is a URL. |
| `@IsMagnetURI()` | Checks if the string is a [magnet uri format](https://en.wikipedia.org/wiki/Magnet_URI_scheme). |
| `@IsUUID(version?: "3"\|"4"\|"5"\|"all")` | Checks if the string is a UUID (version 3, 4, 5 or all ). |
| `@IsUUID(version?: UUIDVersion)` | Checks if the string is a UUID (version 3, 4, 5 or all ). |
| `@IsFirebasePushId()` | Checks if the string is a [Firebase Push ID](https://firebase.googleblog.com/2015/02/the-2120-ways-to-ensure-unique_68.html) |
| `@IsUppercase()` | Checks if the string is uppercase. |
| `@Length(min: number, max?: number)` | Checks if the string's length falls in a range. |
Expand Down
7 changes: 3 additions & 4 deletions src/decorator/string/IsUUID.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
import { ValidationOptions } from '../ValidationOptions';
import { buildMessage, ValidateBy } from '../common/ValidateBy';
import isUuidValidator from 'validator/lib/isUUID';

export type UUIDVersion = '3' | '4' | '5' | 'all' | 3 | 4 | 5;
import type ValidatorJS from 'validator';

export const IS_UUID = 'isUuid';

/**
* Checks if the string is a UUID (version 3, 4 or 5).
* If given value is not a string, then it returns false.
*/
export function isUUID(value: unknown, version?: UUIDVersion): boolean {
export function isUUID(value: unknown, version?: ValidatorJS.UUIDVersion): boolean {
return typeof value === 'string' && isUuidValidator(value, version);
}

/**
* Checks if the string is a UUID (version 3, 4 or 5).
* If given value is not a string, then it returns false.
*/
export function IsUUID(version?: UUIDVersion, validationOptions?: ValidationOptions): PropertyDecorator {
export function IsUUID(version?: ValidatorJS.UUIDVersion, validationOptions?: ValidationOptions): PropertyDecorator {
return ValidateBy(
{
name: IS_UUID,
Expand Down

0 comments on commit a33eb7f

Please sign in to comment.