From b0bf677ee7b7c322dbe2c9feb13257787edc4fb8 Mon Sep 17 00:00:00 2001 From: Pierre Cavin Date: Wed, 25 Oct 2023 23:22:11 +0200 Subject: [PATCH] fix: detect multiple zeros as an invalid step (#743) [skip ci] --- src/time.ts | 4 ++-- tests/crontime.test.ts | 8 ++++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/time.ts b/src/time.ts index 17b6a0eb..2a35b38a 100644 --- a/src/time.ts +++ b/src/time.ts @@ -774,10 +774,10 @@ export class CronTime { let upper = mUpper !== undefined ? parseInt(mUpper, 10) : undefined; const wasStepDefined = mStep !== undefined; - if (mStep === '0') { + const step = parseInt(mStep ?? '1', 10); + if (step === 0) { throw new Error(`Field (${unit}) has a step of zero`); } - const step = parseInt(mStep ?? '1', 10); if (upper !== undefined && lower > upper) { throw new Error(`Field (${unit}) has an invalid range`); diff --git a/tests/crontime.test.ts b/tests/crontime.test.ts index 5954a215..3827268b 100644 --- a/tests/crontime.test.ts +++ b/tests/crontime.test.ts @@ -210,9 +210,17 @@ describe('crontime', () => { }); it('should test invalid step', () => { + expect(() => { + new CronTime('* * * 1/ *'); + }).toThrow(); + expect(() => { new CronTime('* * * 1/0 *'); }).toThrow(); + + expect(() => { + new CronTime('* * * 1/00 *'); + }).toThrow(); }); it('should test invalid range', () => {