Skip to content

Commit 7d4871c

Browse files
committed
Add documentation and tests for validateAllowingStrings()
1 parent e8ee072 commit 7d4871c

2 files changed

Lines changed: 67 additions & 6 deletions

File tree

src/validation/index.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,30 @@
1-
import { validateSync } from 'class-validator';
1+
import { validateSync, ValidationError } from 'class-validator';
22

3+
/**
4+
* Check if a string is an integer (signed/unsigned)
5+
*
6+
* @param str String to check
7+
* @returns Returns true if the string is int
8+
*/
39
export function isIntString(str: string): boolean {
410
return (
511
typeof str === 'string' &&
612
Number.isInteger(Number(str))
713
);
814
}
915

10-
export function validateAllowingStrings(obj: object) {
11-
let validationErrors = validateSync(obj, {
16+
/**
17+
* Run validation on a model; allowing ints as strings
18+
*
19+
* @param obj Object to check
20+
* @returns Returns true if the string is int
21+
*/
22+
export function validateAllowingStrings(obj: object): ValidationError[] {
23+
const validationErrors = validateSync(obj, {
1224
skipMissingProperties: true
1325
});
1426

15-
validationErrors = validationErrors.filter(error => {
27+
return validationErrors.filter(error => {
1628
const constraints = error.constraints;
1729

1830
if (
@@ -24,6 +36,4 @@ export function validateAllowingStrings(obj: object) {
2436

2537
return Object.keys(error.constraints).length;
2638
});
27-
28-
return validationErrors;
2939
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { IsInt } from 'class-validator';
2+
import 'jasmine';
3+
import { BaseModel } from 'models';
4+
import { isIntString, validateAllowingStrings } from '../../../../src/validation';
5+
6+
/**
7+
* isIntString()
8+
* pointyapi/validation
9+
*/
10+
describe('[Validation] isIntString()', () => {
11+
it('returns true if string is int', () => {
12+
for (const test of [ '1', '0', '-1', '123', '-123', '99999999999999']) {
13+
expect(isIntString(test)).withContext(test).toBeTrue();
14+
}
15+
});
16+
17+
it('returns false if string is not int', () => {
18+
for (const test of [ '1a', '0x', '-', '%', 'hello', 'a']) {
19+
expect(isIntString(test)).withContext(test).toBeFalse();
20+
}
21+
});
22+
});
23+
24+
/**
25+
* validateAllowingStrings()
26+
* pointyapi/validation
27+
*/
28+
class ValidationTestModel {
29+
@IsInt()
30+
public intprop: number | string = undefined;
31+
}
32+
33+
describe('[Validation] validateAllowingStrings()', () => {
34+
it('can allow strings that are ints', () => {
35+
const obj = new ValidationTestModel();
36+
obj.intprop = '123';
37+
const result = validateAllowingStrings(obj);
38+
39+
expect(Array.isArray(result)).withContext('returns array').toBeTrue();
40+
expect(result.length).withContext('number of errors').toBe(0);
41+
});
42+
43+
it('does not allow strings that are not ints', () => {
44+
const obj = new ValidationTestModel();
45+
obj.intprop = 'abc';
46+
const result = validateAllowingStrings(obj);
47+
48+
expect(Array.isArray(result)).withContext('returns array').toBeTrue();
49+
expect(result.length).withContext('number of errors').toBe(1);
50+
});
51+
});

0 commit comments

Comments
 (0)