validata
Version:
Type safe data validation and sanitization
112 lines • 5.88 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
const test_helpers_1 = require("./test-helpers");
const url_1 = require("./url");
describe('isUrl', () => {
it('will handle non-url', () => {
const fut = (0, url_1.isUrl)();
(0, test_helpers_1.expectIssue)(fut, null, 'not-defined');
(0, test_helpers_1.expectIssue)(fut, undefined, 'not-defined');
(0, test_helpers_1.expectIssue)(fut, 'https://google.com', 'incorrect-type');
(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, {}, 'incorrect-type');
});
it('will handle urls', () => {
const fut = (0, url_1.isUrl)();
(0, test_helpers_1.expectSuccess)(fut, new URL('https://google.com'));
});
it('will check protocol', () => {
const fut = (0, url_1.isUrl)({ protocol: 'https' });
(0, test_helpers_1.expectSuccess)(fut, new URL('https://google.com'));
(0, test_helpers_1.expectIssue)(fut, new URL('http://google.com'), 'invalid-protocol');
});
it('will check custom validator', () => {
const fut = (0, url_1.isUrl)({ validator: (value) => value.pathname === '/test' });
(0, test_helpers_1.expectSuccess)(fut, new URL('https://google.com/test'));
(0, test_helpers_1.expectIssue)(fut, new URL('https://google.com'), 'validator');
});
});
describe('maybeUrl', () => {
it('will handle non-url', () => {
const fut = (0, url_1.maybeUrl)();
(0, test_helpers_1.expectValue)(fut, null, undefined);
(0, test_helpers_1.expectValue)(fut, undefined, undefined);
(0, test_helpers_1.expectIssue)(fut, 'https://google.com', 'incorrect-type');
(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, {}, 'incorrect-type');
});
it('will handle non-url', () => {
const fut = (0, url_1.maybeUrl)({ incorrectTypeToUndefined: true });
(0, test_helpers_1.expectValue)(fut, null, undefined);
(0, test_helpers_1.expectValue)(fut, undefined, undefined);
(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, {}, undefined);
});
it('will handle urls', () => {
const fut = (0, url_1.maybeUrl)();
(0, test_helpers_1.expectSuccess)(fut, new URL('https://google.com'));
});
it('will check protocol', () => {
const fut = (0, url_1.maybeUrl)({ protocol: 'https' });
(0, test_helpers_1.expectSuccess)(fut, new URL('https://google.com'));
(0, test_helpers_1.expectIssue)(fut, new URL('http://google.com'), 'invalid-protocol');
});
it('will check custom validator', () => {
const fut = (0, url_1.maybeUrl)({ validator: (value) => value.pathname === '/test' });
(0, test_helpers_1.expectSuccess)(fut, new URL('https://google.com/test'));
(0, test_helpers_1.expectIssue)(fut, new URL('https://google.com'), 'validator');
});
});
describe('asUrl', () => {
it('will handle non-url', () => {
const fut = (0, url_1.asUrl)();
(0, test_helpers_1.expectIssue)(fut, null, 'not-defined');
(0, test_helpers_1.expectIssue)(fut, undefined, 'not-defined');
(0, test_helpers_1.expectSuccess)(fut, 'https://google.com');
(0, test_helpers_1.expectIssue)(fut, 'hello world', 'no-conversion');
(0, test_helpers_1.expectIssue)(fut, 0, 'no-conversion');
(0, test_helpers_1.expectIssue)(fut, new Date(), 'no-conversion');
(0, test_helpers_1.expectIssue)(fut, [], 'no-conversion');
(0, test_helpers_1.expectIssue)(fut, {}, 'no-conversion');
});
it('will custom convert urls', () => {
const fut = (0, url_1.asUrl)({ converter: (value) => value === 'http://127.0.0.1' ? new URL('http://localhost') : undefined });
(0, test_helpers_1.expectValue)(fut, 'http://127.0.0.1', new URL('http://localhost'));
(0, test_helpers_1.expectIssue)(fut, 'hello world', 'no-conversion');
});
it('will handle urls', () => {
const fut = (0, url_1.asUrl)();
(0, test_helpers_1.expectSuccess)(fut, new URL('https://google.com'));
});
});
describe('maybeAsUrl', () => {
it('will handle non-url', () => {
const fut = (0, url_1.maybeAsUrl)();
(0, test_helpers_1.expectValue)(fut, null, undefined);
(0, test_helpers_1.expectValue)(fut, undefined, undefined);
(0, test_helpers_1.expectValue)(fut, 'hello world', undefined);
(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, {}, undefined);
});
it('will accept non-url as undefined', () => {
const fut = (0, url_1.maybeAsUrl)({ incorrectTypeToUndefined: true });
(0, test_helpers_1.expectValue)(fut, 'hello world', undefined);
(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, {}, undefined);
});
it('will handle urls', () => {
const fut = (0, url_1.maybeAsUrl)();
(0, test_helpers_1.expectSuccess)(fut, new URL('https://google.com'));
});
});
//# sourceMappingURL=url.test.js.map
;