validata
Version:
Type safe data validation and sanitization
312 lines • 16.8 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
const change_case_1 = require("change-case");
const luxon_1 = require("luxon");
const validator_1 = require("validator");
const string_1 = require("./string");
const test_helpers_1 = require("./test-helpers");
describe('isString', () => {
it('will handle non-string', () => {
const fut = (0, string_1.isString)();
(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, {}, 'incorrect-type');
});
it('will handle strings', () => {
const fut = (0, string_1.isString)();
(0, test_helpers_1.expectValue)(fut, '', '');
(0, test_helpers_1.expectValue)(fut, 'asd', 'asd');
(0, test_helpers_1.expectValue)(fut, '123', '123');
});
it('will validate string length', () => {
const fut = (0, string_1.isString)({ minLength: 2, maxLength: 5 });
(0, test_helpers_1.expectValue)(fut, 'asdf', 'asdf');
(0, test_helpers_1.expectIssue)(fut, 'a', 'min-length');
(0, test_helpers_1.expectIssue)(fut, 'asdfghjk', 'max-length');
});
it('will check regex', () => {
const fut = (0, string_1.isString)({ regex: /^[0-9]{3}$/ });
(0, test_helpers_1.expectValue)(fut, '123', '123');
(0, test_helpers_1.expectIssue)(fut, 'as', 'regex');
(0, test_helpers_1.expectIssue)(fut, '12', 'regex');
(0, test_helpers_1.expectIssue)(fut, '12345', 'regex');
});
it('will check custom validator', () => {
const fut = (0, string_1.isString)({ validator: (value) => value === 'test' });
(0, test_helpers_1.expectValue)(fut, 'test', 'test');
(0, test_helpers_1.expectIssue)(fut, 'other', 'validator');
});
it('will check custom validator', () => {
const fut = (0, string_1.isString)({ validator: validator_1.default.isEmail });
(0, test_helpers_1.expectSuccess)(fut, 'me@home.com');
(0, test_helpers_1.expectIssue)(fut, 'other', 'validator');
});
it('will check custom validator', () => {
const fut = (0, string_1.isString)({ validator: validator_1.default.isIn, validatorOptions: ['a', 's'] });
(0, test_helpers_1.expectSuccess)(fut, 'a');
(0, test_helpers_1.expectSuccess)(fut, 's');
(0, test_helpers_1.expectIssue)(fut, 'other', 'validator');
});
it('be transformed by single transform', () => {
const fut = (0, string_1.isString)({ transform: change_case_1.dotCase });
(0, test_helpers_1.expectValue)(fut, '', '');
(0, test_helpers_1.expectValue)(fut, 'a', 'a');
(0, test_helpers_1.expectValue)(fut, 'foo bar', 'foo.bar');
(0, test_helpers_1.expectValue)(fut, 'FooBar', 'foo.bar');
});
it('be transformed by single transform', () => {
const transform = (value) => value.replace(/o/g, '*');
const fut = (0, string_1.isString)({ transform: [change_case_1.dotCase, transform] });
(0, test_helpers_1.expectValue)(fut, '', '');
(0, test_helpers_1.expectValue)(fut, 'a', 'a');
(0, test_helpers_1.expectValue)(fut, 'foo bar', 'f**.bar');
(0, test_helpers_1.expectValue)(fut, 'FooBar', 'f**.bar');
});
it('work with trim and min/max length', () => {
const fut = (0, string_1.isString)({ trim: 'both', minLength: 2, maxLength: 6 });
(0, test_helpers_1.expectValue)(fut, 'asdf', 'asdf');
(0, test_helpers_1.expectValue)(fut, ' asdf ', 'asdf');
(0, test_helpers_1.expectIssue)(fut, ' a ', 'min-length');
(0, test_helpers_1.expectValue)(fut, ' asdfgh ', 'asdfgh');
(0, test_helpers_1.expectIssue)(fut, 'asdfghj', 'max-length');
(0, test_helpers_1.expectIssue)(fut, ' asdfghj ', 'max-length');
});
it('rego', () => {
const fut = (0, string_1.isString)({ trim: 'both', minLength: 1, maxLength: 6, regex: /^[0-9a-zA-Z]+$/, transform: (value) => value.toUpperCase() });
(0, test_helpers_1.expectValue)(fut, 'aSd123', 'ASD123');
(0, test_helpers_1.expectValue)(fut, ' aS12 ', 'AS12');
(0, test_helpers_1.expectIssue)(fut, ' ', 'min-length');
(0, test_helpers_1.expectIssue)(fut, 'AdDf123', 'max-length');
(0, test_helpers_1.expectIssue)(fut, ' AdDf123 ', 'max-length');
(0, test_helpers_1.expectIssue)(fut, ' 34#$12 ', 'regex');
});
});
describe('maybeString', () => {
it('will handle non-string', () => {
const fut = (0, string_1.maybeString)();
(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, {}, 'incorrect-type');
});
it('will handle non-string', () => {
const fut = (0, string_1.maybeString)({ 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 strings', () => {
const fut = (0, string_1.maybeString)();
(0, test_helpers_1.expectSuccess)(fut, '');
(0, test_helpers_1.expectSuccess)(fut, 'asd');
(0, test_helpers_1.expectSuccess)(fut, '123');
});
it('will validate string length', () => {
const fut = (0, string_1.maybeString)({ minLength: 2, maxLength: 5 });
(0, test_helpers_1.expectSuccess)(fut, 'asdf');
(0, test_helpers_1.expectIssue)(fut, 'a', 'min-length');
(0, test_helpers_1.expectIssue)(fut, 'asdfghjk', 'max-length');
});
it('will check regex', () => {
const fut = (0, string_1.maybeString)({ regex: /^[0-9]{3}$/ });
(0, test_helpers_1.expectSuccess)(fut, '123');
(0, test_helpers_1.expectIssue)(fut, 'as', 'regex');
(0, test_helpers_1.expectIssue)(fut, '12', 'regex');
(0, test_helpers_1.expectIssue)(fut, '12345', 'regex');
});
it('will check custom validator', () => {
const fut = (0, string_1.maybeString)({ validator: (value) => value === 'test' });
(0, test_helpers_1.expectValue)(fut, null, undefined);
(0, test_helpers_1.expectSuccess)(fut, 'test');
(0, test_helpers_1.expectIssue)(fut, 'other', 'validator');
});
it('will check custom validator', () => {
const fut = (0, string_1.maybeString)({ validator: validator_1.default.isEmail });
(0, test_helpers_1.expectValue)(fut, null, undefined);
(0, test_helpers_1.expectSuccess)(fut, 'me@home.com');
(0, test_helpers_1.expectIssue)(fut, 'other', 'validator');
});
it('will check custom validator', () => {
const fut = (0, string_1.maybeString)({ validator: validator_1.default.isIn, validatorOptions: ['a', 's'] });
(0, test_helpers_1.expectSuccess)(fut, 'a');
(0, test_helpers_1.expectSuccess)(fut, 's');
(0, test_helpers_1.expectIssue)(fut, 'other', 'validator');
});
});
describe('asString', () => {
it('will handle non-string', () => {
const fut = (0, string_1.asString)();
(0, test_helpers_1.expectIssue)(fut, null, 'not-defined');
(0, test_helpers_1.expectIssue)(fut, undefined, 'not-defined');
(0, test_helpers_1.expectValue)(fut, 0, '0');
(0, test_helpers_1.expectValue)(fut, luxon_1.DateTime.fromMillis(1562057445845, { zone: 'Pacific/Auckland' }).toJSDate(), '2019-07-02T08:50:45.845Z');
(0, test_helpers_1.expectValue)(fut, [], '');
(0, test_helpers_1.expectValue)(fut, {}, '[object Object]');
});
it('will handle non-string with default', () => {
const fut = (0, string_1.asString)({ default: 'foo' });
(0, test_helpers_1.expectValue)(fut, null, 'foo');
(0, test_helpers_1.expectValue)(fut, undefined, 'foo');
(0, test_helpers_1.expectValue)(fut, 0, '0');
(0, test_helpers_1.expectValue)(fut, luxon_1.DateTime.fromMillis(1562057445845, { zone: 'Pacific/Auckland' }).toJSDate(), '2019-07-02T08:50:45.845Z');
(0, test_helpers_1.expectValue)(fut, [], '');
(0, test_helpers_1.expectValue)(fut, {}, '[object Object]');
});
it('will handle strings', () => {
const fut = (0, string_1.asString)();
(0, test_helpers_1.expectSuccess)(fut, '');
(0, test_helpers_1.expectSuccess)(fut, 'asd');
(0, test_helpers_1.expectSuccess)(fut, '123');
});
it('will validate string length', () => {
const fut = (0, string_1.asString)({ minLength: 2, maxLength: 5 });
(0, test_helpers_1.expectSuccess)(fut, 'asdf');
(0, test_helpers_1.expectIssue)(fut, 'a', 'min-length');
(0, test_helpers_1.expectIssue)(fut, 'asdfghjk', 'max-length');
});
it('will limit string length', () => {
const fut = (0, string_1.asString)({ limitLength: 5 });
(0, test_helpers_1.expectSuccess)(fut, 'asdf');
(0, test_helpers_1.expectValue)(fut, 'asdfghjk', 'asdfg');
});
it('will check regex', () => {
const fut = (0, string_1.asString)({ regex: /^[0-9]{3}$/ });
(0, test_helpers_1.expectValue)(fut, '123', '123');
(0, test_helpers_1.expectValue)(fut, 654, '654');
(0, test_helpers_1.expectIssue)(fut, 'as', 'regex');
(0, test_helpers_1.expectIssue)(fut, '12', 'regex');
(0, test_helpers_1.expectIssue)(fut, '12345', 'regex');
});
it('will trim', () => {
const fut = (0, string_1.asString)({ trim: 'both' });
(0, test_helpers_1.expectValue)(fut, ' 123 ', '123');
(0, test_helpers_1.expectValue)(fut, '123 ', '123');
(0, test_helpers_1.expectValue)(fut, ' 123', '123');
(0, test_helpers_1.expectValue)(fut, '123', '123');
});
it('will trim start', () => {
const fut = (0, string_1.asString)({ trim: 'start' });
(0, test_helpers_1.expectValue)(fut, ' 123 ', '123 ');
(0, test_helpers_1.expectValue)(fut, '123 ', '123 ');
(0, test_helpers_1.expectValue)(fut, ' 123', '123');
(0, test_helpers_1.expectValue)(fut, '123', '123');
});
it('will trim end', () => {
const fut = (0, string_1.asString)({ trim: 'end' });
(0, test_helpers_1.expectValue)(fut, ' 123 ', ' 123');
(0, test_helpers_1.expectValue)(fut, '123 ', '123');
(0, test_helpers_1.expectValue)(fut, ' 123', ' 123');
(0, test_helpers_1.expectValue)(fut, '123', '123');
});
it('will pad start', () => {
const fut = (0, string_1.asString)({ padStart: { length: 6, padWith: '-' } });
(0, test_helpers_1.expectValue)(fut, '', '------');
(0, test_helpers_1.expectValue)(fut, 'A', '-----A');
(0, test_helpers_1.expectValue)(fut, 'ABCDE', '-ABCDE');
(0, test_helpers_1.expectValue)(fut, 'ABCDEF', 'ABCDEF');
(0, test_helpers_1.expectValue)(fut, 'ABCDEFGHI', 'ABCDEFGHI');
});
it('will pad end', () => {
const fut = (0, string_1.asString)({ padEnd: { length: 6, padWith: '-' } });
(0, test_helpers_1.expectValue)(fut, '', '------');
(0, test_helpers_1.expectValue)(fut, 'A', 'A-----');
(0, test_helpers_1.expectValue)(fut, 'ABCDE', 'ABCDE-');
(0, test_helpers_1.expectValue)(fut, 'ABCDEF', 'ABCDEF');
(0, test_helpers_1.expectValue)(fut, 'ABCDEFGHI', 'ABCDEFGHI');
});
it('will check custom validator', () => {
const fut = (0, string_1.asString)({ validator: (value) => value === 'test' });
(0, test_helpers_1.expectSuccess)(fut, 'test');
(0, test_helpers_1.expectIssue)(fut, 'other', 'validator');
});
it('will check custom validator', () => {
const fut = (0, string_1.asString)({ validator: validator_1.default.isEmail });
(0, test_helpers_1.expectSuccess)(fut, 'me@home.com');
(0, test_helpers_1.expectIssue)(fut, 'other', 'validator');
});
it('will check custom validator', () => {
const fut = (0, string_1.asString)({ validator: validator_1.default.isIn, validatorOptions: ['a', 's'] });
(0, test_helpers_1.expectSuccess)(fut, 'a');
(0, test_helpers_1.expectSuccess)(fut, 's');
(0, test_helpers_1.expectIssue)(fut, 'other', 'validator');
});
it('will use custom converter', () => {
const fut = (0, string_1.asString)({ converter: (value) => value === 1 ? 'one' : undefined });
(0, test_helpers_1.expectValue)(fut, 1, 'one');
(0, test_helpers_1.expectValue)(fut, 2, '2');
});
});
describe('maybeAsString', () => {
it('will handle non-string', () => {
const fut = (0, string_1.maybeAsString)();
(0, test_helpers_1.expectValue)(fut, null, undefined);
(0, test_helpers_1.expectValue)(fut, undefined, undefined);
(0, test_helpers_1.expectValue)(fut, 0, '0');
(0, test_helpers_1.expectValue)(fut, luxon_1.DateTime.fromMillis(1562057445845, { zone: 'Pacific/Auckland' }).toJSDate(), '2019-07-02T08:50:45.845Z');
(0, test_helpers_1.expectValue)(fut, [], '');
(0, test_helpers_1.expectValue)(fut, {}, '[object Object]');
});
it('will handle non-string with default', () => {
const fut = (0, string_1.maybeAsString)({ default: 'foo' });
(0, test_helpers_1.expectValue)(fut, null, 'foo');
(0, test_helpers_1.expectValue)(fut, undefined, 'foo');
(0, test_helpers_1.expectValue)(fut, 0, '0');
(0, test_helpers_1.expectValue)(fut, luxon_1.DateTime.fromMillis(1562057445845, { zone: 'Pacific/Auckland' }).toJSDate(), '2019-07-02T08:50:45.845Z');
(0, test_helpers_1.expectValue)(fut, [], '');
(0, test_helpers_1.expectValue)(fut, {}, '[object Object]');
});
it('will handle strings', () => {
const fut = (0, string_1.maybeAsString)();
(0, test_helpers_1.expectSuccess)(fut, '');
(0, test_helpers_1.expectSuccess)(fut, 'asd');
(0, test_helpers_1.expectSuccess)(fut, '123');
});
it('will validate string length', () => {
const fut = (0, string_1.maybeAsString)({ minLength: 2, maxLength: 5 });
(0, test_helpers_1.expectSuccess)(fut, 'asdf');
(0, test_helpers_1.expectIssue)(fut, 'a', 'min-length');
(0, test_helpers_1.expectIssue)(fut, 'asdfghjk', 'max-length');
});
it('will limit string length', () => {
const fut = (0, string_1.maybeAsString)({ limitLength: 5 });
(0, test_helpers_1.expectSuccess)(fut, 'asdf');
(0, test_helpers_1.expectValue)(fut, 'asdfghjk', 'asdfg');
});
it('will check regex', () => {
const fut = (0, string_1.maybeAsString)({ regex: /^[0-9]{3}$/ });
(0, test_helpers_1.expectSuccess)(fut, '123');
(0, test_helpers_1.expectIssue)(fut, 'as', 'regex');
(0, test_helpers_1.expectIssue)(fut, '12', 'regex');
(0, test_helpers_1.expectIssue)(fut, '12345', 'regex');
});
it('will check custom validator', () => {
const fut = (0, string_1.maybeAsString)({ validator: (value) => value === 'test' });
(0, test_helpers_1.expectSuccess)(fut, 'test');
(0, test_helpers_1.expectIssue)(fut, 'other', 'validator');
});
it('will check custom validator', () => {
const fut = (0, string_1.maybeAsString)({ validator: validator_1.default.isEmail });
(0, test_helpers_1.expectSuccess)(fut, 'me@home.com');
(0, test_helpers_1.expectIssue)(fut, 'other', 'validator');
});
it('will check custom validator', () => {
const fut = (0, string_1.maybeAsString)({ validator: validator_1.default.isIn, validatorOptions: ['a', 's'] });
(0, test_helpers_1.expectSuccess)(fut, 'a');
(0, test_helpers_1.expectSuccess)(fut, 's');
(0, test_helpers_1.expectIssue)(fut, 'other', 'validator');
});
it('will use custom converter', () => {
const fut = (0, string_1.maybeAsString)({ converter: (value) => value === 1 ? 'one' : undefined });
(0, test_helpers_1.expectValue)(fut, 1, 'one');
(0, test_helpers_1.expectValue)(fut, 2, '2');
});
});
//# sourceMappingURL=string.test.js.map
;