validata
Version:
Type safe data validation and sanitization
200 lines • 10 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
const number_1 = require("./number");
const test_helpers_1 = require("./test-helpers");
describe('isNumber', () => {
it('incorrect type will cause an issue', () => {
const fut = (0, number_1.isNumber)();
(0, test_helpers_1.runTests)(fut, { input: 'test', issues: [{ reason: 'incorrect-type' }] }, { input: new Date(), issues: [{ reason: 'incorrect-type' }] }, { input: [], issues: [{ reason: 'incorrect-type' }] }, { input: {}, issues: [{ reason: 'incorrect-type' }] });
});
it('null, undefined or NaN will cause an issue', () => {
const fut = (0, number_1.isNumber)();
(0, test_helpers_1.runTests)(fut, { input: null, issues: [{ reason: 'not-defined' }] }, { input: undefined, issues: [{ reason: 'not-defined' }] }, { input: NaN, issues: [{ reason: 'incorrect-type' }] });
});
it('will accept number', () => {
const fut = (0, number_1.isNumber)();
(0, test_helpers_1.expectSuccess)(fut, 123);
(0, test_helpers_1.expectSuccess)(fut, 0);
(0, test_helpers_1.expectSuccess)(fut, 1.23234);
(0, test_helpers_1.expectSuccess)(fut, -1.23234);
});
it('will validate range', () => {
const fut = (0, number_1.isNumber)({ min: 2, max: 5 });
(0, test_helpers_1.expectSuccess)(fut, 4);
(0, test_helpers_1.expectIssue)(fut, 1, 'min');
(0, test_helpers_1.expectIssue)(fut, 8, 'max');
});
it('will check custom validator', () => {
const fut = (0, number_1.isNumber)({ validator: (value) => value === 7 });
(0, test_helpers_1.expectSuccess)(fut, 7);
(0, test_helpers_1.expectIssue)(fut, 2, 'validator');
});
});
describe('maybeNumber', () => {
it('incorrect type will cause issue', () => {
const fut = (0, number_1.maybeNumber)();
(0, test_helpers_1.expectIssue)(fut, 'test', '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, {}, 'incorrect-type');
});
it('incorrect type will not cause issue', () => {
const fut = (0, number_1.maybeNumber)({ incorrectTypeToUndefined: true });
(0, test_helpers_1.expectValue)(fut, 'test', undefined);
(0, test_helpers_1.expectValue)(fut, new Date(), undefined);
(0, test_helpers_1.expectValue)(fut, [], undefined);
(0, test_helpers_1.expectValue)(fut, {}, undefined);
});
it('null, undefined or NaN will be coerced to undefined', () => {
const fut = (0, number_1.maybeNumber)();
(0, test_helpers_1.expectValue)(fut, null, undefined);
(0, test_helpers_1.expectValue)(fut, undefined, undefined);
(0, test_helpers_1.expectValue)(fut, NaN, undefined);
});
it('will handle number', () => {
const fut = (0, number_1.maybeNumber)();
(0, test_helpers_1.expectSuccess)(fut, 123);
(0, test_helpers_1.expectSuccess)(fut, 0);
(0, test_helpers_1.expectSuccess)(fut, 1.23234);
(0, test_helpers_1.expectSuccess)(fut, -1.23234);
});
it('will validate range', () => {
const fut = (0, number_1.maybeNumber)({ min: 2, max: 5 });
(0, test_helpers_1.expectSuccess)(fut, 4);
(0, test_helpers_1.expectIssue)(fut, 1, 'min');
(0, test_helpers_1.expectIssue)(fut, 8, 'max');
});
it('will check custom validator', () => {
const fut = (0, number_1.maybeNumber)({ validator: (value) => value === 7 });
(0, test_helpers_1.expectSuccess)(fut, 7);
(0, test_helpers_1.expectIssue)(fut, 2, 'validator');
});
});
describe('asNumber', () => {
it('null or undefined will be an issue', () => {
const fut = (0, number_1.asNumber)();
(0, test_helpers_1.expectIssue)(fut, null, 'not-defined');
(0, test_helpers_1.expectIssue)(fut, undefined, 'not-defined');
});
it('incorrect type with no conversion will be an issue', () => {
const fut = (0, number_1.asNumber)();
(0, test_helpers_1.expectIssue)(fut, 'test', 'no-conversion');
(0, test_helpers_1.expectIssue)(fut, [], 'no-conversion');
(0, test_helpers_1.expectIssue)(fut, {}, 'no-conversion');
(0, test_helpers_1.expectIssue)(fut, NaN, 'no-conversion');
});
it('Date will be converted', () => {
const fut = (0, number_1.asNumber)();
(0, test_helpers_1.expectValue)(fut, new Date(1562057445845), 1562057445845);
});
it('Custom values will be converted by custom converter', () => {
const fut = (0, number_1.asNumber)({
converter: (value, options) => options.indexOf(value) + 1 || undefined,
convertOptions: ['one', 'two'],
});
(0, test_helpers_1.expectValue)(fut, 'one', 1);
(0, test_helpers_1.expectValue)(fut, 'two', 2);
(0, test_helpers_1.expectIssue)(fut, 'infinity', 'no-conversion');
});
it('incorrect type that cannot be converted will have default used', () => {
const fut = (0, number_1.asNumber)({ default: 17.54 });
(0, test_helpers_1.expectValue)(fut, 123.4, 123.4);
(0, test_helpers_1.expectValue)(fut, null, 17.54);
(0, test_helpers_1.expectValue)(fut, undefined, 17.54);
(0, test_helpers_1.expectValue)(fut, 'test', 17.54);
(0, test_helpers_1.expectValue)(fut, '123.4', 123.4);
(0, test_helpers_1.expectValue)(fut, [], 17.54);
(0, test_helpers_1.expectValue)(fut, {}, 17.54);
(0, test_helpers_1.expectValue)(fut, NaN, 17.54);
});
it('incorrect type that cannot be converted will have not const default used', () => {
let flag = true;
const fut = (0, number_1.asNumber)({
default: () => {
const result = flag ? 17.54 : 18.65;
flag = !flag;
return result;
},
});
(0, test_helpers_1.expectValue)(fut, undefined, 17.54);
(0, test_helpers_1.expectValue)(fut, undefined, 18.65);
});
it('will handle number', () => {
const fut = (0, number_1.asNumber)();
(0, test_helpers_1.expectSuccess)(fut, 123);
(0, test_helpers_1.expectSuccess)(fut, 0);
(0, test_helpers_1.expectSuccess)(fut, 1.23234);
(0, test_helpers_1.expectSuccess)(fut, -1.23234);
(0, test_helpers_1.expectSuccess)(fut, '-1.23234');
});
it('will coerce range', () => {
const fut = (0, number_1.asNumber)({ coerceMin: 2, coerceMax: 5 });
(0, test_helpers_1.expectSuccess)(fut, 4);
(0, test_helpers_1.expectValue)(fut, 1, 2);
(0, test_helpers_1.expectValue)(fut, 8, 5);
});
it('will check custom validator', () => {
const fut = (0, number_1.asNumber)({ validator: (value) => value === 7 });
(0, test_helpers_1.expectSuccess)(fut, 7);
(0, test_helpers_1.expectIssue)(fut, 2, 'validator');
});
});
describe('maybeAsNumber', () => {
it('incorrect type will be converted to undefined', () => {
const fut = (0, number_1.maybeAsNumber)();
(0, test_helpers_1.expectValue)(fut, 'test', undefined);
(0, test_helpers_1.expectValue)(fut, [], undefined);
(0, test_helpers_1.expectValue)(fut, ['test'], undefined);
(0, test_helpers_1.expectValue)(fut, {}, undefined);
(0, test_helpers_1.expectValue)(fut, NaN, undefined);
});
it('Date will be converted to number', () => {
const fut = (0, number_1.maybeAsNumber)();
(0, test_helpers_1.expectValue)(fut, new Date(1562057445845), 1562057445845);
});
it('Custom values will be converted by custom converter', () => {
const fut = (0, number_1.maybeAsNumber)({
converter: (value, options) => options.indexOf(value) + 1 || undefined,
convertOptions: ['one', 'two'],
});
(0, test_helpers_1.expectValue)(fut, 'one', 1);
(0, test_helpers_1.expectValue)(fut, 'two', 2);
(0, test_helpers_1.expectValue)(fut, 'infinity', undefined);
});
it('null or undefined or empty string will be converted to undefined', () => {
const fut = (0, number_1.maybeAsNumber)();
(0, test_helpers_1.expectValue)(fut, null, undefined);
(0, test_helpers_1.expectValue)(fut, undefined, undefined);
(0, test_helpers_1.expectValue)(fut, '', undefined);
});
it('incorrect type, null, undefined or NaN that cannot be converted will have default used', () => {
const fut = (0, number_1.maybeAsNumber)({ default: 17.54 });
(0, test_helpers_1.expectValue)(fut, 123.4, 123.4);
(0, test_helpers_1.expectValue)(fut, null, 17.54);
(0, test_helpers_1.expectValue)(fut, undefined, 17.54);
(0, test_helpers_1.expectValue)(fut, 'test', 17.54);
(0, test_helpers_1.expectValue)(fut, '123.4', 123.4);
(0, test_helpers_1.expectValue)(fut, [], 17.54);
(0, test_helpers_1.expectValue)(fut, {}, 17.54);
(0, test_helpers_1.expectValue)(fut, NaN, 17.54);
});
it('will handle number', () => {
const fut = (0, number_1.maybeAsNumber)();
(0, test_helpers_1.expectSuccess)(fut, 123);
(0, test_helpers_1.expectSuccess)(fut, 0);
(0, test_helpers_1.expectSuccess)(fut, 1.23234);
(0, test_helpers_1.expectSuccess)(fut, -1.23234);
});
it('will coerce range', () => {
const fut = (0, number_1.maybeAsNumber)({ coerceMin: 2, coerceMax: 5 });
(0, test_helpers_1.expectSuccess)(fut, 4);
(0, test_helpers_1.expectValue)(fut, 1, 2);
(0, test_helpers_1.expectValue)(fut, 8, 5);
});
it('will check custom validator', () => {
const fut = (0, number_1.maybeAsNumber)({ validator: (value) => value === 7 });
(0, test_helpers_1.expectSuccess)(fut, 7);
(0, test_helpers_1.expectIssue)(fut, 2, 'validator');
});
});
//# sourceMappingURL=number.test.js.map
;