validata
Version:
Type safe data validation and sanitization
118 lines • 6.29 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
const any_1 = require("./any");
const number_1 = require("./number");
const object_1 = require("./object");
const record_1 = require("./record");
const string_1 = require("./string");
const test_helpers_1 = require("./test-helpers");
describe('isRecord', () => {
it('will fail non-object', () => {
const fut = (0, record_1.isRecord)((0, number_1.isNumber)());
(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 record', () => {
const fut = (0, record_1.isRecord)((0, number_1.isNumber)());
(0, test_helpers_1.expectSuccess)(fut, {});
(0, test_helpers_1.expectSuccess)(fut, { a: 47 });
(0, test_helpers_1.expectValue)(fut, { a: 47 }, { a: 47 });
(0, test_helpers_1.expectIssue)(fut, { a: 'foo' }, 'incorrect-type', ['a']);
});
it('will respect min and max number of keys', () => {
const fut = (0, record_1.isRecord)((0, number_1.isNumber)(), { maxKeys: 4, minKeys: 2 });
(0, test_helpers_1.expectIssue)(fut, {}, 'min-keys', []);
(0, test_helpers_1.expectIssue)(fut, { a: 47 }, 'min-keys', []);
(0, test_helpers_1.expectSuccess)(fut, { a: 47, b: 4, c: 65 });
(0, test_helpers_1.expectIssue)(fut, { a: 47, b: 4, c: 65, d: 65, e: 76 }, 'max-keys', []);
});
it('will check key names', () => {
const fut = (0, record_1.isRecord)((0, any_1.isAny)(), { keyRegex: /^test/ });
(0, test_helpers_1.expectIssue)(fut, { a: 324 }, 'key-regex', []);
(0, test_helpers_1.expectIssue)(fut, { test: 47, Test: 'q34' }, 'key-regex', []);
(0, test_helpers_1.expectSuccess)(fut, { test: 47, tester: 4, testing: 65 });
});
it('will check key names - exclude chars', () => {
const fut = (0, record_1.isRecord)((0, any_1.isAny)(), { keyRegex: /^[^.$]+$/ });
(0, test_helpers_1.expectIssue)(fut, { 'hello.world': 324 }, 'key-regex', []);
(0, test_helpers_1.expectIssue)(fut, { test: 47, $id: 'q34' }, 'key-regex', []);
(0, test_helpers_1.expectSuccess)(fut, { foo: 47, bar: 4, testing: 65 });
});
it('will process children', () => {
const fut = (0, record_1.isRecord)((0, object_1.isObject)({
a: (0, number_1.asNumber)({ coerceMin: 25 }),
b: (0, string_1.asString)(),
}));
(0, test_helpers_1.expectValue)(fut, { foo: { a: 47, b: 'asd' } }, { foo: { a: 47, b: 'asd' } });
(0, test_helpers_1.expectValue)(fut, { foo: { a: '47', b: 12 } }, { foo: { a: 47, b: '12' } });
(0, test_helpers_1.expectValue)(fut, { foo: { a: '7', b: 12 } }, { foo: { a: 25, b: '12' } });
});
});
describe('maybeRecord', () => {
it('will coerce null and undefined', () => {
const fut = (0, record_1.maybeRecord)((0, number_1.isNumber)());
(0, test_helpers_1.expectValue)(fut, null, undefined);
(0, test_helpers_1.expectValue)(fut, undefined, undefined);
});
it('will fail non-object', () => {
const fut = (0, record_1.maybeRecord)((0, number_1.isNumber)());
(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 be graceful with non-object', () => {
const fut = (0, record_1.maybeRecord)((0, number_1.isNumber)(), { 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 record', () => {
const fut = (0, record_1.maybeRecord)((0, number_1.isNumber)());
(0, test_helpers_1.expectSuccess)(fut, {});
(0, test_helpers_1.expectSuccess)(fut, { a: 47 });
});
});
describe('asRecord', () => {
it('will fail null or undefined', () => {
const fut = (0, record_1.asRecord)((0, number_1.isNumber)());
(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, record_1.asRecord)((0, number_1.isNumber)(), { default: { a: 47 } });
(0, test_helpers_1.expectValue)(fut, null, { a: 47 });
(0, test_helpers_1.expectValue)(fut, undefined, { a: 47 });
});
it('will accept string values in record', () => {
const fut = (0, record_1.asRecord)((0, string_1.isString)());
(0, test_helpers_1.expectValue)(fut, '{ "fruit": "apple" }', { fruit: 'apple' });
});
});
describe('maybeAsRecord', () => {
it('will coerce null and undefined', () => {
const fut = (0, record_1.maybeAsRecord)();
(0, test_helpers_1.expectValue)(fut, null, undefined);
(0, test_helpers_1.expectValue)(fut, undefined, undefined);
});
it('will use default', () => {
const fut = (0, record_1.maybeAsRecord)((0, number_1.asNumber)(), { default: { a: 47 } });
(0, test_helpers_1.expectValue)(fut, null, { a: 47 });
(0, test_helpers_1.expectValue)(fut, undefined, { a: 47 });
});
it('will convert non-object to undefined', () => {
const fut = (0, record_1.maybeAsRecord)();
(0, test_helpers_1.expectValue)(fut, '', undefined);
(0, test_helpers_1.expectValue)(fut, false, 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, 'test', undefined);
});
});
//# sourceMappingURL=record.test.js.map
;