Skip to content

Commit da1c66f

Browse files
authored
fix(schedules): name the range for an out-of-range schedule value instead of calling it required (#10638)
An invalid minutes interval, hourly minute, HH:MM time or monthly day now reports its allowed range or format, and source apply only adds the cron and timezone hint when the timing fields themselves pass. Accept and refuse decisions are unchanged.
1 parent e1c92e5 commit da1c66f

4 files changed

Lines changed: 72 additions & 7 deletions

File tree

‎apps/sim/lib/workflows/code/apply/schedules.test.ts‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,33 @@ describe('findInvalidSchedules', () => {
4343
expect(refusal?.blockKey).toBe('nightly')
4444
})
4545

46+
it.each([
47+
['an interval over 1440', { scheduleType: 'minutes', minutesInterval: '5000' }],
48+
['a time without HH:MM', { scheduleType: 'daily', dailyTime: '9am' }],
49+
[
50+
'a day of the month over 31',
51+
{ scheduleType: 'monthly', monthlyDay: '40', monthlyTime: '09:00' },
52+
],
53+
])(
54+
'names the bad value in %s instead of calling it missing or pointing to cron',
55+
(_label, values) => {
56+
const [refusal] = refusals(schedule('nightly', values))
57+
expect(refusal?.message).not.toMatch(/required/)
58+
expect(refusal?.hint ?? '').not.toMatch(/cron/i)
59+
}
60+
)
61+
62+
it('keeps the cron and timezone hint for a timezone deploy cannot schedule in', () => {
63+
const [refusal] = refusals(
64+
schedule('nightly', {
65+
scheduleType: 'daily',
66+
dailyTime: '09:00',
67+
timezone: 'Mars/Olympus_Mons',
68+
})
69+
)
70+
expect(refusal?.hint).toMatch(/timezone/)
71+
})
72+
4673
it.each([
4774
[
4875
'a valid cron in a timezone',

‎apps/sim/lib/workflows/code/apply/schedules.ts‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import type { Diagnostic } from '@sim/workflow-code'
22
import type { GraphDiff, SourceGraph } from '@/lib/workflows/code/apply/graph-diff'
3-
import { findScheduleBlocks, validateScheduleBlock } from '@/lib/workflows/schedules/validation'
3+
import {
4+
findScheduleBlocks,
5+
hasValidScheduleConfig,
6+
validateScheduleBlock,
7+
} from '@/lib/workflows/schedules/validation'
48

59
export type InvalidScheduleDiagnostic = Diagnostic<'E_APPLY_INVALID_SCHEDULE'>
610

@@ -9,7 +13,9 @@ export type InvalidScheduleDiagnostic = Diagnostic<'E_APPLY_INVALID_SCHEDULE'>
913
* deploy runs (`validateScheduleBlock`), so a dry run fails where deploy would instead of passing
1014
* and leaving deploy to refuse. A schedule whose type is still missing a field is a draft not
1115
* filled in yet; deploy refuses it, but the required-field gates own that, so it is not refused
12-
* here. `keyOf` names a block's source key by id, to locate each refusal.
16+
* here. Only a refusal whose timing fields pass gets the cron and timezone hint, since an
17+
* out-of-range field's message already names its range. `keyOf` names a block's source key by
18+
* id, to locate each refusal.
1319
*/
1420
export function findInvalidSchedules(
1521
target: SourceGraph,
@@ -29,7 +35,11 @@ export function findInvalidSchedules(
2935
message: `Schedule "${block.name}" cannot run: ${result.error ?? 'its timing is invalid'}.`,
3036
blockId: block.id,
3137
...(blockKey !== undefined ? { blockKey } : {}),
32-
hint: 'Write a five-field cron expression (minute hour day-of-month month day-of-week) and an IANA timezone such as America/New_York; deploy refuses this schedule as written.',
38+
...(hasValidScheduleConfig(result.scheduleType, block)
39+
? {
40+
hint: 'Write a five-field cron expression (minute hour day-of-month month day-of-week) and an IANA timezone such as America/New_York; deploy refuses this schedule as written.',
41+
}
42+
: {}),
3343
})
3444
}
3545
return refusals

‎apps/sim/lib/workflows/schedules/deploy.test.ts‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,9 @@ describe('Schedule Deploy Utilities', () => {
155155
const result = validateScheduleBlock(block)
156156

157157
expect(result.isValid).toBe(false)
158-
expect(result.error).toBe('Minutes interval is required for minute-based schedules')
158+
expect(result.error).toBe(
159+
'Minutes interval must be between 1 and 1440 for minute-based schedules'
160+
)
159161
})
160162
})
161163

@@ -193,7 +195,9 @@ describe('Schedule Deploy Utilities', () => {
193195
const result = validateScheduleBlock(block)
194196

195197
expect(result.isValid).toBe(false)
196-
expect(result.error).toBe('Day and time are required for monthly schedules')
198+
expect(result.error).toBe(
199+
'Day must be between 1 and 31 and time must be HH:MM for monthly schedules'
200+
)
197201
})
198202
})
199203

‎apps/sim/lib/workflows/schedules/validation.ts‎

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,10 @@ function isValidTimeValue(value: string | null | undefined): boolean {
4141
* Check if a schedule type has valid configuration
4242
* Uses raw block values to avoid false positives from default parsing
4343
*/
44-
function hasValidScheduleConfig(scheduleType: string | undefined, block: BlockState): boolean {
44+
export function hasValidScheduleConfig(
45+
scheduleType: string | undefined,
46+
block: BlockState
47+
): boolean {
4548
switch (scheduleType) {
4649
case 'minutes': {
4750
const rawValue = getSubBlockValue(block, 'minutesInterval')
@@ -113,6 +116,27 @@ function getMissingConfigError(scheduleType: string): string {
113116
}
114117
}
115118

119+
/**
120+
* Get human-readable error message for schedule configuration that is filled in but cannot
121+
* schedule (an out-of-range number or a time that is not HH:MM)
122+
*/
123+
function getInvalidConfigError(scheduleType: string): string {
124+
switch (scheduleType) {
125+
case 'minutes':
126+
return 'Minutes interval must be between 1 and 1440 for minute-based schedules'
127+
case 'hourly':
128+
return 'Minute value must be between 0 and 59 for hourly schedules'
129+
case 'daily':
130+
return 'Time must be HH:MM for daily schedules'
131+
case 'weekly':
132+
return 'Time must be HH:MM for weekly schedules'
133+
case 'monthly':
134+
return 'Day must be between 1 and 31 and time must be HH:MM for monthly schedules'
135+
default:
136+
return getMissingConfigError(scheduleType)
137+
}
138+
}
139+
116140
/**
117141
* Find schedule blocks in a workflow's blocks
118142
* Only returns enabled schedule blocks (disabled blocks are skipped)
@@ -150,7 +174,7 @@ export function validateScheduleBlock(block: BlockState): ScheduleValidationResu
150174
})
151175
return {
152176
isValid: false,
153-
error: getMissingConfigError(scheduleType),
177+
error: missing ? getMissingConfigError(scheduleType) : getInvalidConfigError(scheduleType),
154178
reason: missing ? 'missing' : 'invalid',
155179
scheduleType,
156180
}

0 commit comments

Comments
 (0)