validata
Version:
Type safe data validation and sanitization
186 lines • 9.8 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
const luxon_1 = require("luxon");
const date_1 = require("./date");
const test_helpers_1 = require("./test-helpers");
describe('isDate', () => {
it('incorrect type will cause an issue', () => {
const fut = (0, date_1.isDate)();
(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_1.isDate)();
(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_1.isDate)();
(0, test_helpers_1.expectSuccess)(fut, new Date());
});
it('will validate future limit', () => {
const fut = (0, date_1.isDate)({ maxFuture: luxon_1.Duration.fromISO('PT10M') });
(0, test_helpers_1.expectSuccess)(fut, new Date());
(0, test_helpers_1.expectIssue)(fut, new Date(Date.now() + 60 * 60 * 1000), 'max-future');
});
it('will validate past limit', () => {
const fut = (0, date_1.isDate)({ maxPast: luxon_1.Duration.fromISO('PT10M') });
(0, test_helpers_1.expectSuccess)(fut, new Date());
(0, test_helpers_1.expectIssue)(fut, new Date(Date.now() - 60 * 60 * 1000), 'max-past');
});
});
describe('maybeDate', () => {
it('incorrect type will cause issue', () => {
const fut = (0, date_1.maybeDate)();
(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_1.maybeDate)({ 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_1.maybeDate)();
(0, test_helpers_1.expectValue)(fut, null, undefined);
(0, test_helpers_1.expectValue)(fut, undefined, undefined);
});
it('will handle date', () => {
const fut = (0, date_1.maybeDate)();
(0, test_helpers_1.expectSuccess)(fut, new Date());
});
it('will validate future limit', () => {
const fut = (0, date_1.maybeDate)({ maxFuture: luxon_1.Duration.fromISO('PT10M') });
(0, test_helpers_1.expectSuccess)(fut, new Date());
(0, test_helpers_1.expectIssue)(fut, new Date(Date.now() + 60 * 60 * 1000), 'max-future');
});
it('will validate past limit', () => {
const fut = (0, date_1.maybeDate)({ maxPast: luxon_1.Duration.fromISO('PT10M') });
(0, test_helpers_1.expectSuccess)(fut, new Date());
(0, test_helpers_1.expectIssue)(fut, new Date(Date.now() - 60 * 60 * 1000), 'max-past');
});
});
describe('asDate', () => {
it('null or undefined will be an issue', () => {
const fut = (0, date_1.asDate)();
(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_1.asDate)();
(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_1.asDate)();
const date = new Date();
(0, test_helpers_1.expectValue)(fut, date.getTime(), date);
});
it('will handle date', () => {
const fut = (0, date_1.asDate)();
(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 use format for convert', () => {
const fut = (0, date_1.asDate)({ format: 'dd/MM/yyyy HH:mm:ss' });
(0, test_helpers_1.expectSuccess)(fut, '27/05/2020 14:06:39');
(0, test_helpers_1.expectSuccess)(fut, new Date());
});
it('will run custom converter', () => {
const fut = (0, date_1.asDate)({
converter: (value, options) => luxon_1.DateTime.fromFormat(value, options.format).toJSDate(),
convertOptions: { format: 'dd/MM/yyyy HH:mm:ss' },
});
(0, test_helpers_1.expectSuccess)(fut, '27/05/2020 14:06:39');
(0, test_helpers_1.expectSuccess)(fut, new Date());
});
it('will fall back to built-in converter', () => {
const fut = (0, date_1.asDate)({
converter: (value, options) => luxon_1.DateTime.fromFormat(value, options.format).toJSDate(),
convertOptions: { format: 'dd/MM/yyyy HH:mm:ss' },
});
(0, test_helpers_1.expectSuccess)(fut, '2013-02-12T08:34:32.120Z');
});
it('will check custom validator', () => {
const date = new Date();
const fut = (0, date_1.asDate)({ validator: (value) => value.getTime() === date.getTime() });
(0, test_helpers_1.expectSuccess)(fut, date);
(0, test_helpers_1.expectIssue)(fut, new Date().setFullYear(2000), 'validator');
});
});
describe('maybeAsDate', () => {
it('incorrect type will be converted to undefined', () => {
const fut = (0, date_1.maybeAsDate)();
(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);
(0, test_helpers_1.expectValue)(fut, NaN, undefined);
});
it('incorrect type will be converted to undefined', () => {
const fut = (0, date_1.maybeAsDate)({ strictParsing: true });
(0, test_helpers_1.expectIssue)(fut, 'test', '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_1.maybeAsDate)();
(0, test_helpers_1.expectValue)(fut, 1562057445845, new Date(1562057445845));
(0, test_helpers_1.expectValue)(fut, '2013-02-12T08:34:32.120Z', new Date(Date.parse('2013-02-12T08:34:32.120Z')));
});
it('null or undefined will be converted to undefined', () => {
const fut = (0, date_1.maybeAsDate)();
(0, test_helpers_1.expectValue)(fut, null, undefined);
(0, test_helpers_1.expectValue)(fut, undefined, undefined);
});
it('will handle date', () => {
const fut = (0, date_1.maybeAsDate)();
(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 use format for convert', () => {
const fut = (0, date_1.maybeAsDate)({ format: 'dd/MM/yyyy HH:mm:ss' });
(0, test_helpers_1.expectSuccess)(fut, '27/05/2020 14:06:39');
(0, test_helpers_1.expectSuccess)(fut, new Date());
});
it('will run custom converter', () => {
const fut = (0, date_1.maybeAsDate)({
converter: (value, options) => luxon_1.DateTime.fromFormat(value, options.format).toJSDate(),
convertOptions: { format: 'dd/MM/yyyy HH:mm:ss' },
});
(0, test_helpers_1.expectSuccess)(fut, '27/05/2020 14:06:39');
(0, test_helpers_1.expectSuccess)(fut, new Date());
});
it('will fall back to built-in converter', () => {
const fut = (0, date_1.maybeAsDate)({
converter: (value, options) => luxon_1.DateTime.fromFormat(value, options.format).toJSDate(),
convertOptions: { format: 'dd/MM/yyyy HH:mm:ss' },
});
(0, test_helpers_1.expectSuccess)(fut, '2013-02-12T08:34:32.120Z');
});
it('will validate future limit', () => {
const fut = (0, date_1.maybeAsDate)({ maxFuture: luxon_1.Duration.fromISO('PT10M') });
(0, test_helpers_1.expectSuccess)(fut, new Date());
(0, test_helpers_1.expectIssue)(fut, new Date(Date.now() + 60 * 60 * 1000), 'max-future');
});
it('will validate past limit', () => {
const fut = (0, date_1.maybeAsDate)({ maxPast: luxon_1.Duration.fromISO('PT10M') });
(0, test_helpers_1.expectSuccess)(fut, new Date());
(0, test_helpers_1.expectIssue)(fut, new Date(Date.now() - 60 * 60 * 1000), 'max-past');
});
it('will check custom validator', () => {
const date = new Date();
const fut = (0, date_1.maybeAsDate)({ validator: (value) => value.getTime() === date.getTime() });
(0, test_helpers_1.expectSuccess)(fut, date);
(0, test_helpers_1.expectIssue)(fut, new Date().setFullYear(2000), 'validator');
});
});
//# sourceMappingURL=date.test.js.map
;