Skip to content

Commit e176387

Browse files
committed
feat: add loose UUID validation and update documentation
1 parent ab98d65 commit e176387

File tree

3 files changed

+47
-3
lines changed

3 files changed

+47
-3
lines changed

README.md

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

src/decorator/string/IsUUID.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ import * as ValidatorJS from 'validator';
66
export const IS_UUID = 'isUuid';
77

88
/**
9-
* Checks if the string is a UUID (version 1-8, nil, max, all).
9+
* Checks if the string is a UUID (version 1-8, nil, max, loose, all).
1010
* If given value is not a string, then it returns false.
1111
*/
1212
export function isUUID(value: unknown, version?: ValidatorJS.UUIDVersion): boolean {
1313
return typeof value === 'string' && isUuidValidator(value, version);
1414
}
1515

1616
/**
17-
* Checks if the string is a UUID (version 1-8, nil, max, all).
17+
* Checks if the string is a UUID (version 1-8, nil, max, loose, all).
1818
* If given value is not a string, then it returns false.
1919
*/
2020
export function IsUUID(version?: ValidatorJS.UUIDVersion, validationOptions?: ValidationOptions): PropertyDecorator {

test/functional/validation-functions-and-decorators.spec.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4063,6 +4063,50 @@ describe('IsUUID all', () => {
40634063
});
40644064
});
40654065

4066+
describe('IsUUID loose', () => {
4067+
const validValues = [
4068+
'A987FBC9-4BED-3078-8F07-9141BA07C9F3',
4069+
'{A987FBC9-4BED-3078-8F07-9141BA07C9F3}',
4070+
'{a987fbc9-4bed-3078-8f07-9141ba07c9f3}',
4071+
'A987FBC94BED30788F079141BA07C9F3',
4072+
];
4073+
const invalidValues = [
4074+
null,
4075+
undefined,
4076+
'',
4077+
'xxxA987FBC9-4BED-3078-CF07-9141BA07C9F3',
4078+
'934859',
4079+
'AAAAAAAA-1111-1111-AAAG-111111111111',
4080+
];
4081+
4082+
class MyClass {
4083+
@IsUUID('loose')
4084+
someProperty: string;
4085+
}
4086+
4087+
it('should not fail if validator.validate said that its valid', () => {
4088+
return checkValidValues(new MyClass(), validValues);
4089+
});
4090+
4091+
it('should fail if validator.validate said that its invalid', () => {
4092+
return checkInvalidValues(new MyClass(), invalidValues);
4093+
});
4094+
4095+
it('should not fail if method in validator said that its valid', () => {
4096+
validValues.forEach(value => expect(isUUID(value, 'loose')).toBeTruthy());
4097+
});
4098+
4099+
it('should fail if method in validator said that its invalid', () => {
4100+
invalidValues.forEach(value => expect(isUUID(value, 'loose')).toBeFalsy());
4101+
});
4102+
4103+
it('should return error object with proper data', () => {
4104+
const validationType = 'isUuid';
4105+
const message = 'someProperty must be a UUID';
4106+
return checkReturnedError(new MyClass(), invalidValues, validationType, message);
4107+
});
4108+
});
4109+
40664110
describe('IsFirebasePushId', () => {
40674111
const validValues = [
40684112
'-M-Jh_1KAH5rYJF_7-kY',

0 commit comments

Comments
 (0)