Skip to content

Commit ce23db7

Browse files
test: update validation functions test
1 parent fc003f7 commit ce23db7

File tree

1 file changed

+66
-50
lines changed

1 file changed

+66
-50
lines changed

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

Lines changed: 66 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ import {
184184
isUUID,
185185
isUppercase,
186186
isVariableWidth,
187+
length,
187188
matches,
188189
max,
189190
maxDate,
@@ -318,7 +319,7 @@ describe('IsDefined', () => {
318319
it('should return error object with proper data', () => {
319320
const validationType = 'isDefined';
320321
const message = 'someProperty should not be null or undefined';
321-
return checkReturnedError(new MyClass(), invalidValues, validationType, message);
322+
checkReturnedError(new MyClass(), invalidValues, validationType, message);
322323
});
323324
});
324325

@@ -1780,7 +1781,7 @@ describe('IsEthereumAddress', () => {
17801781
it('should return error object with proper data', () => {
17811782
const validationType = 'isEthereumAddress';
17821783
const message = 'someProperty must be an Ethereum address';
1783-
return checkReturnedError(new MyClass(), invalidValues, validationType, message);
1784+
checkReturnedError(new MyClass(), invalidValues, validationType, message);
17841785
});
17851786
});
17861787

@@ -1813,7 +1814,7 @@ describe('IsBtcAddress', () => {
18131814
it('should return error object with proper data', () => {
18141815
const validationType = 'isBtcAddress';
18151816
const message = 'someProperty must be a BTC address';
1816-
return checkReturnedError(new MyClass(), invalidValues, validationType, message);
1817+
checkReturnedError(new MyClass(), invalidValues, validationType, message);
18171818
});
18181819
});
18191820

@@ -1846,7 +1847,7 @@ describe('IsDataURI', () => {
18461847
it('should return error object with proper data', () => {
18471848
const validationType = 'isDataURI';
18481849
const message = 'someProperty must be a data uri format';
1849-
return checkReturnedError(new MyClass(), invalidValues, validationType, message);
1850+
checkReturnedError(new MyClass(), invalidValues, validationType, message);
18501851
});
18511852
});
18521853

@@ -1879,7 +1880,7 @@ describe('IsHSL', () => {
18791880
it('should return error object with proper data', () => {
18801881
const validationType = 'isHSL';
18811882
const message = 'someProperty must be a HSL color';
1882-
return checkReturnedError(new MyClass(), invalidValues, validationType, message);
1883+
checkReturnedError(new MyClass(), invalidValues, validationType, message);
18831884
});
18841885
});
18851886

@@ -1912,7 +1913,7 @@ describe('IsRgbColor', () => {
19121913
it('should return error object with proper data', () => {
19131914
const validationType = 'isRgbColor';
19141915
const message = 'someProperty must be RGB color';
1915-
return checkReturnedError(new MyClass(), invalidValues, validationType, message);
1916+
checkReturnedError(new MyClass(), invalidValues, validationType, message);
19161917
});
19171918
});
19181919

@@ -1945,7 +1946,7 @@ describe('IsIdentityCard', () => {
19451946
it('should return error object with proper data', () => {
19461947
const validationType = 'isIdentityCard';
19471948
const message = 'someProperty must be a identity card number';
1948-
return checkReturnedError(new MyClass(), invalidValues, validationType, message);
1949+
checkReturnedError(new MyClass(), invalidValues, validationType, message);
19491950
});
19501951
});
19511952

@@ -1978,7 +1979,7 @@ describe('IsEAN', () => {
19781979
it('should return error object with proper data', () => {
19791980
const validationType = 'isEAN';
19801981
const message = 'someProperty must be an EAN (European Article Number)';
1981-
return checkReturnedError(new MyClass(), invalidValues, validationType, message);
1982+
checkReturnedError(new MyClass(), invalidValues, validationType, message);
19821983
});
19831984
});
19841985

@@ -2011,7 +2012,7 @@ describe('IsISRC', () => {
20112012
it('should return error object with proper data', () => {
20122013
const validationType = 'isISRC';
20132014
const message = 'someProperty must be an ISRC';
2014-
return checkReturnedError(new MyClass(), invalidValues, validationType, message);
2015+
checkReturnedError(new MyClass(), invalidValues, validationType, message);
20152016
});
20162017
});
20172018

@@ -2044,7 +2045,7 @@ describe('IsRFC3339', () => {
20442045
it('should return error object with proper data', () => {
20452046
const validationType = 'isRFC3339';
20462047
const message = 'someProperty must be RFC 3339 date';
2047-
return checkReturnedError(new MyClass(), invalidValues, validationType, message);
2048+
checkReturnedError(new MyClass(), invalidValues, validationType, message);
20482049
});
20492050
});
20502051

@@ -2077,7 +2078,7 @@ describe('IsLocale', () => {
20772078
it('should return error object with proper data', () => {
20782079
const validationType = 'isLocale';
20792080
const message = 'someProperty must be locale';
2080-
return checkReturnedError(new MyClass(), invalidValues, validationType, message);
2081+
checkReturnedError(new MyClass(), invalidValues, validationType, message);
20812082
});
20822083
});
20832084

@@ -2114,7 +2115,7 @@ describe('IsMagnetURI', () => {
21142115
it('should return error object with proper data', () => {
21152116
const validationType = 'isMagnetURI';
21162117
const message = 'someProperty must be magnet uri format';
2117-
return checkReturnedError(new MyClass(), invalidValues, validationType, message);
2118+
checkReturnedError(new MyClass(), invalidValues, validationType, message);
21182119
});
21192120
});
21202121

@@ -2147,7 +2148,7 @@ describe('IsMimeType', () => {
21472148
it('should return error object with proper data', () => {
21482149
const validationType = 'isMimeType';
21492150
const message = 'someProperty must be MIME type format';
2150-
return checkReturnedError(new MyClass(), invalidValues, validationType, message);
2151+
checkReturnedError(new MyClass(), invalidValues, validationType, message);
21512152
});
21522153
});
21532154

@@ -3195,61 +3196,81 @@ describe('IsNotEmptyObject', () => {
31953196
[],
31963197
[{ key: 'value' }],
31973198
];
3198-
const nullableValidValues = [{ key: 'value' }, { key: 'value' }];
3199-
const nullableInvalidValues = [
3200-
null,
3201-
undefined,
3202-
'{ key: "value" }',
3203-
"{ 'key': 'value' }",
3204-
'string',
3205-
1234,
3206-
false,
3207-
{},
3208-
{ key: undefined },
3209-
{ key: null },
3210-
[],
3211-
[{ key: 'value' }],
3212-
];
32133199

32143200
class MyClass {
32153201
@IsNotEmptyObject()
32163202
someProperty: object;
32173203
}
32183204

3219-
class NullableMyClass {
3220-
@IsNotEmptyObject({ nullable: true })
3221-
someProperty: object;
3222-
}
3223-
3224-
it.each([
3225-
[new MyClass(), validValues],
3226-
[new NullableMyClass(), nullableValidValues],
3227-
])('should not fail if validator.validate said that its valid', (validationObject, values) => {
3228-
return checkValidValues(validationObject, values);
3205+
it('should not fail if validator.validate said that its valid', () => {
3206+
return checkValidValues(new MyClass(), validValues);
32293207
});
32303208

3231-
it.each([
3232-
[new MyClass(), invalidValues],
3233-
[new NullableMyClass(), nullableInvalidValues],
3234-
])('should fail if validator.validate said that its invalid', (validationObject, values) => {
3235-
return checkInvalidValues(validationObject, values);
3209+
it('should fail if validator.validate said that its invalid', () => {
3210+
return checkInvalidValues(new MyClass(), invalidValues);
32363211
});
32373212

32383213
it('should not fail if method in validator said that its valid', () => {
32393214
validValues.forEach(value => expect(isNotEmptyObject(value)).toBeTruthy());
3240-
nullableValidValues.forEach(value => expect(isNotEmptyObject(value, { nullable: true })).toBeTruthy());
32413215
});
32423216

32433217
it('should fail if method in validator said that its invalid', () => {
32443218
invalidValues.forEach(value => expect(isNotEmptyObject(value)).toBeFalsy());
3245-
nullableInvalidValues.forEach(value => expect(isNotEmptyObject(value, { nullable: true })).toBeFalsy());
32463219
});
32473220

32483221
it('should return error object with proper data', () => {
32493222
const validationType = 'isNotEmptyObject';
32503223
const message = 'someProperty must be a non-empty object';
32513224
return checkReturnedError(new MyClass(), invalidValues, validationType, message);
32523225
});
3226+
3227+
describe('with `nullable` option', () => {
3228+
const nullableValidValues = validValues;
3229+
const nullableInvalidValues = invalidValues;
3230+
const nonNullableValidValues = [{ key: 'value' }, { key: 'value' }];
3231+
const nonNullableInvalidValues = [
3232+
null,
3233+
undefined,
3234+
'{ key: "value" }',
3235+
"{ 'key': 'value' }",
3236+
'string',
3237+
1234,
3238+
false,
3239+
{},
3240+
{ key: undefined },
3241+
{ key: null },
3242+
[],
3243+
[{ key: 'value' }],
3244+
];
3245+
class NullableMyClass {
3246+
@IsNotEmptyObject({ nullable: true })
3247+
someProperty: object;
3248+
}
3249+
class NonNullableMyClass {
3250+
@IsNotEmptyObject({ nullable: false })
3251+
someProperty: object;
3252+
}
3253+
3254+
it('should not fail if validator.validate said that its valid', async () => {
3255+
await checkValidValues(new NullableMyClass(), nullableValidValues);
3256+
await checkValidValues(new NonNullableMyClass(), nonNullableValidValues);
3257+
});
3258+
3259+
it('should fail if validator.validate said that its valid', async () => {
3260+
await checkInvalidValues(new NullableMyClass(), nullableInvalidValues);
3261+
await checkInvalidValues(new NonNullableMyClass(), nonNullableInvalidValues);
3262+
});
3263+
3264+
it('should not fail if method in validator said that its valid', () => {
3265+
nullableValidValues.forEach(value => expect(isNotEmptyObject(value, { nullable: true })).toBeTruthy());
3266+
nonNullableValidValues.forEach(value => expect(isNotEmptyObject(value, { nullable: false })).toBeTruthy());
3267+
});
3268+
3269+
it('should fail if method in validator said that its invalid', () => {
3270+
nullableInvalidValues.forEach(value => expect(isNotEmptyObject(value, { nullable: true })).toBeFalsy());
3271+
nonNullableInvalidValues.forEach(value => expect(isNotEmptyObject(value, { nullable: false })).toBeFalsy());
3272+
});
3273+
});
32533274
});
32543275

32553276
describe('IsLowercase', () => {
@@ -3979,10 +4000,6 @@ describe('IsTimeZone', () => {
39794000
describe('isPhoneNumber', () => {
39804001
describe('with region', () => {
39814002
const validValues = [
3982-
'0311111111',
3983-
'031 633 60 01',
3984-
'079 4 666 666',
3985-
'075 416 20 30',
39864003
'+41 311111111',
39874004
'+41 31 633 60 01',
39884005
'+41 79 4 666 666',
@@ -3991,7 +4008,6 @@ describe('isPhoneNumber', () => {
39914008
'+41 (0)31 633 60 01',
39924009
'+41 (0)79 4 666 666',
39934010
'+41 (0)75 416 20 30',
3994-
'+49 9072 1111',
39954011
];
39964012
const invalidValues = [undefined, null, 'asdf', '1'];
39974013

0 commit comments

Comments
 (0)