UNPKG

validata

Version:

Type safe data validation and sanitization

158 lines 8.27 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const any_1 = require("./any"); const array_1 = require("./array"); const number_1 = require("./number"); const string_1 = require("./string"); const test_helpers_1 = require("./test-helpers"); describe('isArray', () => { it('will fail non-array', () => { const fut = (0, array_1.isArray)(); (0, test_helpers_1.expectIssue)(fut, null, 'not-defined'); (0, test_helpers_1.expectIssue)(fut, undefined, 'not-defined'); (0, test_helpers_1.expectIssue)(fut, 0, 'incorrect-type'); (0, test_helpers_1.expectIssue)(fut, new Date(), 'incorrect-type'); (0, test_helpers_1.expectIssue)(fut, {}, 'incorrect-type'); (0, test_helpers_1.expectIssue)(fut, 'test', 'incorrect-type'); }); it('will accept array', () => { const fut = (0, array_1.isArray)(); (0, test_helpers_1.expectSuccess)(fut, []); (0, test_helpers_1.expectSuccess)(fut, [{ a: 47 }]); (0, test_helpers_1.expectSuccess)(fut, [12]); (0, test_helpers_1.expectSuccess)(fut, [1, 2, 3, 4, 5, 'a', 'c']); }); it('will give issue if range outside expected', () => { const fut = (0, array_1.isArray)(undefined, { minLength: 1, maxLength: 2 }); (0, test_helpers_1.expectIssue)(fut, [], 'min-length'); (0, test_helpers_1.expectSuccess)(fut, [{ a: 47 }]); (0, test_helpers_1.expectSuccess)(fut, [12]); (0, test_helpers_1.expectSuccess)(fut, [12, 34]); (0, test_helpers_1.expectIssue)(fut, [1, 2, 3, 4, 5, 'a', 'c'], 'max-length'); }); it('will process items', () => { const fut = (0, array_1.isArray)((0, number_1.isNumber)({ coerceMax: 500, min: 25 })); (0, test_helpers_1.expectSuccess)(fut, []); (0, test_helpers_1.expectSuccess)(fut, [87]); (0, test_helpers_1.expectValue)(fut, [87, 223, 543, 56], [87, 223, 500, 56]); (0, test_helpers_1.expectIssue)(fut, [87, 2, 45], 'min', [1]); (0, test_helpers_1.expectIssue)(fut, [87, test, 45], 'incorrect-type', [1]); }); }); describe('maybeArray', () => { it('will fail non-array', () => { const fut = (0, array_1.maybeArray)(); (0, test_helpers_1.expectValue)(fut, null, undefined); (0, test_helpers_1.expectValue)(fut, undefined, undefined); (0, test_helpers_1.expectIssue)(fut, 0, 'incorrect-type'); (0, test_helpers_1.expectIssue)(fut, new Date(), 'incorrect-type'); (0, test_helpers_1.expectIssue)(fut, {}, 'incorrect-type'); (0, test_helpers_1.expectIssue)(fut, 'test', 'incorrect-type'); }); it('will accept non-array with undefined if requested', () => { const fut = (0, array_1.maybeArray)(undefined, { incorrectTypeToUndefined: true }); (0, test_helpers_1.expectValue)(fut, 0, undefined); (0, test_helpers_1.expectValue)(fut, new Date(), undefined); (0, test_helpers_1.expectValue)(fut, {}, undefined); (0, test_helpers_1.expectValue)(fut, 'test', undefined); }); it('will accept array', () => { const fut = (0, array_1.maybeArray)(); (0, test_helpers_1.expectSuccess)(fut, []); (0, test_helpers_1.expectSuccess)(fut, [{ a: 47 }]); (0, test_helpers_1.expectSuccess)(fut, [12]); (0, test_helpers_1.expectSuccess)(fut, [1, 2, 3, 4, 5, 'a', 'c']); }); it('will give issue if range outside expected', () => { const fut = (0, array_1.maybeArray)(undefined, { minLength: 1, maxLength: 2 }); (0, test_helpers_1.expectIssue)(fut, [], 'min-length'); (0, test_helpers_1.expectSuccess)(fut, [{ a: 47 }]); (0, test_helpers_1.expectSuccess)(fut, [12]); (0, test_helpers_1.expectSuccess)(fut, [12, 34]); (0, test_helpers_1.expectIssue)(fut, [1, 2, 3, 4, 5, 'a', 'c'], 'max-length'); }); }); describe('asArray', () => { it('will fail null or undefined', () => { const fut = (0, array_1.asArray)(); (0, test_helpers_1.expectIssue)(fut, null, 'not-defined'); (0, test_helpers_1.expectIssue)(fut, undefined, 'not-defined'); }); it('will use default', () => { const fut = (0, array_1.asArray)((0, any_1.isAny)(), { default: ['default'] }); (0, test_helpers_1.expectValue)(fut, null, ['default']); (0, test_helpers_1.expectValue)(fut, undefined, ['default']); }); it('will convert non-array', () => { const fut = (0, array_1.asArray)(); const date = new Date(); (0, test_helpers_1.expectValue)(fut, 0, [0]); (0, test_helpers_1.expectValue)(fut, date, [date]); (0, test_helpers_1.expectValue)(fut, {}, [{}]); (0, test_helpers_1.expectValue)(fut, 'test', ['test']); }); it('will accept array', () => { const fut = (0, array_1.asArray)(); (0, test_helpers_1.expectValue)(fut, [], []); (0, test_helpers_1.expectValue)(fut, [{ a: 47 }], [{ a: 47 }]); (0, test_helpers_1.expectValue)(fut, [12], [12]); (0, test_helpers_1.expectValue)(fut, [1, 2, 3, 4, 5, 'a', 'c'], [1, 2, 3, 4, 5, 'a', 'c']); }); it('will give issue if range outside expected', () => { const fut = (0, array_1.asArray)(undefined, { minLength: 1, maxLength: 2 }); (0, test_helpers_1.expectIssue)(fut, [], 'min-length'); (0, test_helpers_1.expectSuccess)(fut, [{ a: 47 }]); (0, test_helpers_1.expectSuccess)(fut, [12]); (0, test_helpers_1.expectSuccess)(fut, [12, 34]); (0, test_helpers_1.expectIssue)(fut, [1, 2, 3, 4, 5, 'a', 'c'], 'max-length'); }); it('will process items', () => { const fut = (0, array_1.asArray)((0, number_1.isNumber)({ coerceMax: 500, min: 25 })); (0, test_helpers_1.expectSuccess)(fut, []); (0, test_helpers_1.expectSuccess)(fut, [87]); (0, test_helpers_1.expectValue)(fut, [87, 223, 543, 56], [87, 223, 500, 56]); (0, test_helpers_1.expectIssue)(fut, [87, 2, 45], 'min', [1]); (0, test_helpers_1.expectIssue)(fut, [87, test, 45], 'incorrect-type', [1]); }); }); describe('maybeAsArray', () => { it('will not convert null or undefined', () => { const fut = (0, array_1.maybeAsArray)(); (0, test_helpers_1.expectValue)(fut, null, undefined); (0, test_helpers_1.expectValue)(fut, undefined, undefined); }); it('will use default', () => { const fut = (0, array_1.maybeAsArray)((0, any_1.isAny)(), { default: ['default'] }); (0, test_helpers_1.expectValue)(fut, null, ['default']); (0, test_helpers_1.expectValue)(fut, undefined, ['default']); }); it('will convert non-array', () => { const fut = (0, array_1.maybeAsArray)(); const date = new Date(); (0, test_helpers_1.expectValue)(fut, 0, [0]); (0, test_helpers_1.expectValue)(fut, date, [date]); (0, test_helpers_1.expectValue)(fut, {}, [{}]); (0, test_helpers_1.expectValue)(fut, 'test', ['test']); }); it('will use custom converter', () => { const fut = (0, array_1.maybeAsArray)((0, string_1.isString)(), { converter: (value) => value === 'one and two' ? ['1', '2'] : undefined }); (0, test_helpers_1.expectValue)(fut, 'one and two', ['1', '2']); (0, test_helpers_1.expectValue)(fut, 'three', ['three']); }); it('will accept array', () => { const fut = (0, array_1.maybeAsArray)(); (0, test_helpers_1.expectValue)(fut, [], []); (0, test_helpers_1.expectValue)(fut, [{ a: 47 }], [{ a: 47 }]); (0, test_helpers_1.expectValue)(fut, [12], [12]); (0, test_helpers_1.expectValue)(fut, [1, 2, 3, 4, 5, 'a', 'c'], [1, 2, 3, 4, 5, 'a', 'c']); }); it('will give issue if range outside expected', () => { const fut = (0, array_1.maybeAsArray)(undefined, { minLength: 1, maxLength: 2 }); (0, test_helpers_1.expectIssue)(fut, [], 'min-length'); (0, test_helpers_1.expectSuccess)(fut, [{ a: 47 }]); (0, test_helpers_1.expectSuccess)(fut, [12]); (0, test_helpers_1.expectSuccess)(fut, [12, 34]); (0, test_helpers_1.expectIssue)(fut, [1, 2, 3, 4, 5, 'a', 'c'], 'max-length'); }); }); //# sourceMappingURL=array.test.js.map