import * as CronParser from 'cron-parser'; import * as x from 'x-value'; export const cronExpressionSymbol = Symbol(); export const CronExpression = x.atomic(cronExpressionSymbol, value => { if (typeof value !== 'string') { throw new TypeError('Expected a cron expression string.'); } getCronExpressionValue(value); }); export type CronExpression = x.Nominal<'cron expression', string>; export function getCronExpressionValue(value: string): string { const groups = /^cron\((.+)\)$/.exec(value); if (!groups) { throw new TypeError( 'Expected a cron expression string like `cron(0 0 * * *)`.', ); } const [, pattern] = groups; CronParser.parseExpression(pattern); return pattern; }