|
| 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