validata
Version:
Type safe data validation and sanitization
182 lines (155 loc) • 5.96 kB
text/typescript
import { DateTime } from 'luxon';
import { asArray, isArray, maybeArray, maybeAsArray } from './array';
import { asBoolean, isBoolean, maybeAsBoolean, maybeBoolean } from './boolean';
import { checkAsync } from './check';
import { asNullable, isNullable } from './common';
import { asDate, isDate, maybeAsDate, maybeDate } from './date';
import { asDateTime, isDateTime, maybeAsDateTime, maybeDateTime } from './date-time';
import { asNumber, isNumber, maybeAsNumber, maybeNumber } from './number';
import { asObject, isObject, maybeAsObject, maybeObject } from './object';
import { isRecord, maybeRecord } from './record';
import { asString, isString, maybeAsString, maybeString } from './string';
import { expectValue } from './test-helpers';
import { isTuple, maybeTuple } from './tuple';
import { AsyncValueProcessor, ValueProcessor } from './types';
import { asUrl, isUrl, maybeAsUrl, maybeUrl } from './url';
describe('common tests', () => {
const dateTime = DateTime.local();
const date = new Date();
const url = new URL('http://localhost');
const testCases = [
[ ]],
[ ],
[ ],
[ ],
[ ],
[ ],
[ ],
[ ],
[ ],
[ ],
[ ],
[ ],
[ ],
[ ],
[ ],
[ ],
[ ],
[ ],
[ ],
[ ],
[ ],
[ ],
[ ],
[ ],
[ ],
[ ],
[ ],
[ ],
[ ],
[ ],
[ ], [isNumber()]],
[ ],
[ ],
[ ],
[ ],
[ ],
] as const;
describe('createNullableCheck', () => {
it.each(testCases)('createIsNullableCheck %#', (
check: (...args: any[]) => ValueProcessor<unknown>,
value: unknown,
contract: unknown = undefined
) => {
const fut = check(contract);
const futNull = isNullable(check(contract));
expectValue(futNull, null, null);
expect(fut.process(value)).toEqual(futNull.process(value));
});
it.each([
[ ],
[ ]],
[ ], null, [1, 2, 3]],
[ ],
[ ]],
[ ], null, [1, 2, 3]],
[ ],
[ ],
[ ],
[ ],
[ ],
[ ],
[ ],
[ ],
[ ],
[ ],
[ ],
[ ],
[ ],
[ ],
[ ],
[ ],
[ ],
[ ],
[ ],
[ ],
[ ],
[ ],
[ ],
[ ],
[ ],
[ ],
[ ],
[ ],
[ ],
[ ],
[ ],
[ ],
[ ],
[ ],
[ ],
[ ],
[ ],
[ ],
[ ],
[ ],
[ ],
[ ], [1, 'text'], [isNumber(), isString()]],
[ ], null, [1, 'text'], [isNumber(), isString()]],
[ ],
[ ],
[ ],
[ ],
[ ],
[ ],
])('createAsNullableCheck %#', (
check: (...args: any[]) => ValueProcessor<unknown>,
value: unknown = undefined,
defaultValue: unknown = null,
expectedValue: unknown = defaultValue,
contract: unknown = undefined) => {
const futNull = asNullable(check(contract), { default: defaultValue });
expect(futNull.process(value)).toEqual({ value: expectedValue });
});
it('result is intact during an issue detection', () => {
const fut = isObject({ key: isBoolean() });
const futNull = isObject({ key: asNullable(isBoolean()) });
const result = fut.process({ key: [] });
const resultNull = futNull.process({ key: [] });
expect(result).toEqual(resultNull);
});
});
describe('checkAsync', () => {
it.each(testCases)('async check run %#', async (
check: (...args: any[]) => ValueProcessor<unknown>,
value: unknown,
contract: unknown = undefined
) => {
const futAsync: AsyncValueProcessor<unknown> = {
process: (value) => Promise.resolve(check(contract).process(value)),
};
const result = checkAsync(futAsync, () => Promise.resolve(value));
await expect(result).resolves.not.toThrow();
});
});
});