validata
Version:
Type safe data validation and sanitization
153 lines • 8.85 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const luxon_1 = require("luxon");
const date_time_1 = require("./date-time");
const test_helpers_1 = require("./test-helpers");
describe('isDateTime', () => {
it('incorrect type will cause an issue', () => {
const fut = (0, date_time_1.isDateTime)();
(0, test_helpers_1.runTests)(fut, { input: 'test', issues: [{ reason: 'incorrect-type' }] }, { input: 1234, 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, date_time_1.isDateTime)();
(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 date', () => {
const fut = (0, date_time_1.isDateTime)();
(0, test_helpers_1.expectSuccess)(fut, luxon_1.DateTime.local());
});
it('will validate future limit', () => {
const fut = (0, date_time_1.isDateTime)({ maxFuture: luxon_1.Duration.fromISO('PT10M') });
(0, test_helpers_1.expectSuccess)(fut, luxon_1.DateTime.local());
(0, test_helpers_1.expectIssue)(fut, luxon_1.DateTime.local().plus(luxon_1.Duration.fromISO('PT20M')), 'max-future');
});
it('will validate past limit', () => {
const fut = (0, date_time_1.isDateTime)({ maxPast: luxon_1.Duration.fromISO('PT10M') });
(0, test_helpers_1.expectSuccess)(fut, luxon_1.DateTime.local());
(0, test_helpers_1.expectIssue)(fut, luxon_1.DateTime.local().minus(luxon_1.Duration.fromISO('PT20M')), 'max-past');
});
});
describe('maybeDateTime', () => {
it('incorrect type will cause issue', () => {
const fut = (0, date_time_1.maybeDateTime)();
(0, test_helpers_1.expectIssue)(fut, 'test', 'incorrect-type');
(0, test_helpers_1.expectIssue)(fut, 123, '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 if muted', () => {
const fut = (0, date_time_1.maybeDateTime)({ incorrectTypeToUndefined: true });
(0, test_helpers_1.expectValue)(fut, 'test', undefined);
(0, test_helpers_1.expectValue)(fut, 123, 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, date_time_1.maybeDateTime)();
(0, test_helpers_1.expectValue)(fut, null, undefined);
(0, test_helpers_1.expectValue)(fut, undefined, undefined);
});
it('will handle date', () => {
const fut = (0, date_time_1.maybeDateTime)();
(0, test_helpers_1.expectSuccess)(fut, luxon_1.DateTime.local());
});
it('will validate future limit', () => {
const fut = (0, date_time_1.maybeDateTime)({ maxFuture: luxon_1.Duration.fromISO('PT10M') });
(0, test_helpers_1.expectSuccess)(fut, luxon_1.DateTime.local());
(0, test_helpers_1.expectIssue)(fut, luxon_1.DateTime.local().plus(luxon_1.Duration.fromISO('PT20M')), 'max-future');
});
it('will validate past limit', () => {
const fut = (0, date_time_1.maybeDateTime)({ maxPast: luxon_1.Duration.fromISO('PT10M') });
(0, test_helpers_1.expectSuccess)(fut, luxon_1.DateTime.local());
(0, test_helpers_1.expectIssue)(fut, luxon_1.DateTime.local().minus(luxon_1.Duration.fromISO('PT20M')), 'max-past');
});
});
describe('asDateTime', () => {
it('null or undefined will be an issue', () => {
const fut = (0, date_time_1.asDateTime)();
(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, date_time_1.asDateTime)();
(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, date_time_1.asDateTime)();
const date = luxon_1.DateTime.utc();
(0, test_helpers_1.expectValue)(fut, date.toMillis(), date);
});
it('Date will be converted with custom converter', () => {
const fut = (0, date_time_1.asDateTime)({ converter: (value) => value === 'now' ? luxon_1.DateTime.utc() : undefined });
(0, test_helpers_1.expectSuccess)(fut, 'now');
(0, test_helpers_1.expectIssue)(fut, 'test', 'no-conversion');
});
it('will handle date', () => {
const fut = (0, date_time_1.asDateTime)();
(0, test_helpers_1.expectSuccess)(fut, 123);
(0, test_helpers_1.expectSuccess)(fut, '2013-02-12T08:34:32.120Z');
(0, test_helpers_1.expectSuccess)(fut, new Date());
});
it('will check custom validator', () => {
const date = luxon_1.DateTime.utc();
const fut = (0, date_time_1.asDateTime)({ validator: (value) => value.toMillis() === date.toMillis() });
(0, test_helpers_1.expectSuccess)(fut, date);
(0, test_helpers_1.expectIssue)(fut, new Date().setFullYear(2000), 'validator');
});
});
describe('maybeAsDateTime', () => {
it('incorrect type will be converted to undefined', () => {
const fut = (0, date_time_1.maybeAsDateTime)();
(0, test_helpers_1.expectValue)(fut, 'test', undefined);
(0, test_helpers_1.expectValue)(fut, '2013-99-12T08:34:32.120Z', 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('incorrect type will result in issue when parsing is strict', () => {
const fut = (0, date_time_1.maybeAsDateTime)({ strictParsing: true });
(0, test_helpers_1.expectIssue)(fut, 'test', 'no-conversion');
(0, test_helpers_1.expectIssue)(fut, '2013-99-12T08:34:32.120Z', 'no-conversion');
(0, test_helpers_1.expectIssue)(fut, [], 'no-conversion');
(0, test_helpers_1.expectIssue)(fut, ['test'], 'no-conversion');
(0, test_helpers_1.expectIssue)(fut, {}, 'no-conversion');
(0, test_helpers_1.expectIssue)(fut, NaN, 'no-conversion');
});
it('Date will be converted to date', () => {
const fut = (0, date_time_1.maybeAsDateTime)();
(0, test_helpers_1.expectValue)(fut, 1562057445845, luxon_1.DateTime.fromMillis(1562057445845).toUTC());
(0, test_helpers_1.expectValue)(fut, '2013-02-12T08:34:32.120Z', luxon_1.DateTime.fromISO('2013-02-12T08:34:32.120Z').toUTC());
});
it('null or undefined will be converted to undefined', () => {
const fut = (0, date_time_1.maybeAsDateTime)();
(0, test_helpers_1.expectValue)(fut, null, undefined);
(0, test_helpers_1.expectValue)(fut, undefined, undefined);
});
it('will handle date', () => {
const fut = (0, date_time_1.maybeAsDateTime)();
(0, test_helpers_1.expectSuccess)(fut, 123);
(0, test_helpers_1.expectSuccess)(fut, '2013-02-12T08:34:32.120Z');
(0, test_helpers_1.expectSuccess)(fut, new Date());
});
it('will validate future limit', () => {
const fut = (0, date_time_1.maybeAsDateTime)({ maxFuture: luxon_1.Duration.fromISO('PT10M') });
(0, test_helpers_1.expectSuccess)(fut, luxon_1.DateTime.local());
(0, test_helpers_1.expectIssue)(fut, luxon_1.DateTime.local().plus(luxon_1.Duration.fromISO('PT20M')), 'max-future');
});
it('will validate past limit', () => {
const fut = (0, date_time_1.maybeAsDateTime)({ maxPast: luxon_1.Duration.fromISO('PT10M') });
(0, test_helpers_1.expectSuccess)(fut, luxon_1.DateTime.local());
(0, test_helpers_1.expectIssue)(fut, luxon_1.DateTime.local().minus(luxon_1.Duration.fromISO('PT20M')), 'max-past');
});
it('will check custom validator', () => {
const date = luxon_1.DateTime.utc();
const fut = (0, date_time_1.maybeAsDateTime)({ validator: (value) => value.toMillis() === date.toMillis() });
(0, test_helpers_1.expectSuccess)(fut, date);
(0, test_helpers_1.expectIssue)(fut, new Date().setFullYear(2000), 'validator');
});
});
//# sourceMappingURL=date-time.test.js.map