validata
Version:
Type safe data validation and sanitization
171 lines • 9.18 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
const boolean_1 = require("./boolean");
const test_helpers_1 = require("./test-helpers");
const types_1 = require("./types");
describe('isBoolean', () => {
it('incorrect type will cause an issue', () => {
const fut = (0, boolean_1.isBoolean)();
(0, test_helpers_1.runTests)(fut, { input: 'test', issues: [{ reason: 'incorrect-type' }] }, { input: 123, 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, boolean_1.isBoolean)();
(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 boolean', () => {
const fut = (0, boolean_1.isBoolean)();
(0, test_helpers_1.expectSuccess)(fut, true);
(0, test_helpers_1.expectSuccess)(fut, false);
});
it('will check custom validator', () => {
const fut = (0, boolean_1.isBoolean)({ validator: (value) => !value });
(0, test_helpers_1.expectSuccess)(fut, false);
(0, test_helpers_1.expectIssue)(fut, true, 'validator');
});
it('will check custom validator with custom issue returned', () => {
const fut = (0, boolean_1.isBoolean)({ validator: (value) => value ? [types_1.Issue.forPath([], value, 'custom')] : true });
(0, test_helpers_1.expectSuccess)(fut, false);
(0, test_helpers_1.expectIssue)(fut, true, 'custom');
});
});
describe('maybeBoolean', () => {
it('incorrect type will cause issue', () => {
const fut = (0, boolean_1.maybeBoolean)();
(0, test_helpers_1.expectIssue)(fut, 'test', 'incorrect-type');
(0, test_helpers_1.expectIssue)(fut, 123, '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, boolean_1.maybeBoolean)({ incorrectTypeToUndefined: true });
(0, test_helpers_1.expectValue)(fut, 'test', undefined);
(0, test_helpers_1.expectValue)(fut, 123, 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 will be coerced to undefined', () => {
const fut = (0, boolean_1.maybeBoolean)();
(0, test_helpers_1.expectValue)(fut, null, undefined);
(0, test_helpers_1.expectValue)(fut, undefined, undefined);
});
it('will handle boolean', () => {
const fut = (0, boolean_1.maybeBoolean)();
(0, test_helpers_1.expectSuccess)(fut, true);
(0, test_helpers_1.expectSuccess)(fut, false);
});
it('will check custom validator', () => {
const fut = (0, boolean_1.maybeBoolean)({ validator: (value) => !value });
(0, test_helpers_1.expectSuccess)(fut, false);
(0, test_helpers_1.expectIssue)(fut, true, 'validator');
});
});
describe('asBoolean', () => {
it('null or undefined will be an issue', () => {
const fut = (0, boolean_1.asBoolean)();
(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, boolean_1.asBoolean)();
(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');
});
it('Values will be converted', () => {
const fut = (0, boolean_1.asBoolean)();
(0, test_helpers_1.expectValue)(fut, 'true', true);
(0, test_helpers_1.expectValue)(fut, 'false', false);
(0, test_helpers_1.expectValue)(fut, '', false);
(0, test_helpers_1.expectValue)(fut, 0, false);
(0, test_helpers_1.expectValue)(fut, NaN, false);
(0, test_helpers_1.expectValue)(fut, 1, true);
(0, test_helpers_1.expectValue)(fut, 123, true);
(0, test_helpers_1.expectValue)(fut, -123, true);
});
it('Custom values will be converted by custom converter', () => {
const fut = (0, boolean_1.asBoolean)({ converter: (value) => value === 'yes' ? true : value === 'no' ? false : undefined });
(0, test_helpers_1.expectValue)(fut, 'yes', true);
(0, test_helpers_1.expectValue)(fut, 'no', false);
(0, test_helpers_1.expectIssue)(fut, 'maybe', 'no-conversion');
});
it('incorrect type that cannot be converted will have default used', () => {
const fut = (0, boolean_1.asBoolean)({ default: false });
(0, test_helpers_1.expectValue)(fut, true, true);
(0, test_helpers_1.expectValue)(fut, false, false);
(0, test_helpers_1.expectValue)(fut, null, false);
(0, test_helpers_1.expectValue)(fut, undefined, false);
(0, test_helpers_1.expectValue)(fut, 'test', false);
(0, test_helpers_1.expectValue)(fut, '123.4', false);
(0, test_helpers_1.expectValue)(fut, [], false);
(0, test_helpers_1.expectValue)(fut, {}, false);
(0, test_helpers_1.expectValue)(fut, NaN, false);
});
it('will handle boolean', () => {
const fut = (0, boolean_1.asBoolean)();
(0, test_helpers_1.expectSuccess)(fut, true);
(0, test_helpers_1.expectSuccess)(fut, false);
});
it('will check custom validator', () => {
const fut = (0, boolean_1.asBoolean)({ validator: (value) => !value });
(0, test_helpers_1.expectSuccess)(fut, false);
(0, test_helpers_1.expectIssue)(fut, true, 'validator');
});
});
describe('maybeAsBoolean', () => {
it('incorrect type will be converted to undefined', () => {
const fut = (0, boolean_1.maybeAsBoolean)();
(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);
});
it('Values will be converted', () => {
const fut = (0, boolean_1.maybeAsBoolean)();
(0, test_helpers_1.expectValue)(fut, 'true', true);
(0, test_helpers_1.expectValue)(fut, 'false', false);
(0, test_helpers_1.expectValue)(fut, '', false);
(0, test_helpers_1.expectValue)(fut, 0, false);
(0, test_helpers_1.expectValue)(fut, NaN, false);
(0, test_helpers_1.expectValue)(fut, 1, true);
(0, test_helpers_1.expectValue)(fut, 123, true);
(0, test_helpers_1.expectValue)(fut, -123, true);
});
it('Custom values will be converted by custom converter', () => {
const fut = (0, boolean_1.maybeAsBoolean)({ converter: (value) => value === 'yes' ? true : value === 'no' ? false : undefined });
(0, test_helpers_1.expectValue)(fut, 'yes', true);
(0, test_helpers_1.expectValue)(fut, 'no', false);
(0, test_helpers_1.expectValue)(fut, 'maybe', undefined);
});
it('null or undefined will be converted to undefined', () => {
const fut = (0, boolean_1.maybeAsBoolean)();
(0, test_helpers_1.expectValue)(fut, null, undefined);
(0, test_helpers_1.expectValue)(fut, undefined, undefined);
});
it('incorrect type, null, undefined or NaN that cannot be converted will have default used', () => {
const fut = (0, boolean_1.maybeAsBoolean)({ default: false });
(0, test_helpers_1.expectValue)(fut, true, true);
(0, test_helpers_1.expectValue)(fut, false, false);
(0, test_helpers_1.expectValue)(fut, null, false);
(0, test_helpers_1.expectValue)(fut, undefined, false);
(0, test_helpers_1.expectValue)(fut, 'test', false);
(0, test_helpers_1.expectValue)(fut, '123.4', false);
(0, test_helpers_1.expectValue)(fut, [], false);
(0, test_helpers_1.expectValue)(fut, {}, false);
(0, test_helpers_1.expectValue)(fut, NaN, false);
});
it('will handle boolean', () => {
const fut = (0, boolean_1.maybeAsBoolean)();
(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 check custom validator', () => {
const fut = (0, boolean_1.maybeAsBoolean)({ validator: (value) => !value });
(0, test_helpers_1.expectSuccess)(fut, false);
(0, test_helpers_1.expectIssue)(fut, true, 'validator');
});
});
//# sourceMappingURL=boolean.test.js.map
;