Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 47 additions & 7 deletions src/components/functions/ConfigForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,37 @@ export const ConfigForm = ({ middlewares, data }: ConfigFormProps) => {
<Select
onValueChange={value => {
field.onChange(value);
form.setValue('options.functionType', value);
if (value === 'cron') {
form.setValue('options', {
functionType: 'cron',
cronString: form.getValues('options.cronString') || '',
timezone: form.getValues('options.timezone') || 'UTC',
});
} else if (value === 'event') {
form.setValue('options', {
functionType: 'event',
eventName: form.getValues('options.eventName') || '',
});
} else if (value === 'request' || value === 'webhook') {
const verb =
form.getValues('options.verb') ||
form.getValues('options.http.verb') ||
'GET';
form.setValue('options', {
functionType: value,
verb,
middlewares: form.getValues('options.middlewares') || [],
http: {
verb,
queryParams: form.getValues('options.http.queryParams'),
urlParams: form.getValues('options.http.urlParams'),
bodyParams: form.getValues('options.http.bodyParams'),
returnTypes: form.getValues('options.http.returnTypes'),
},
});
} else {
form.setValue('options', { functionType: value });
}
}}
value={field.value}
>
Expand Down Expand Up @@ -132,12 +162,22 @@ export const ConfigForm = ({ middlewares, data }: ConfigFormProps) => {
<InputField fieldName={'options.eventName'} label={'Event Name'} />
)}
{form.watch('functionType') === 'cron' && (
<InputField
fieldName={'options.cronString'}
label={'Cron schedule'}
placeholder={'*/5 * * * *'}
description={'minute hour day month weekday (UTC)'}
/>
<>
<InputField
fieldName={'options.cronString'}
label={'Cron schedule'}
placeholder={'*/5 * * * *'}
description={'minute hour day month weekday'}
/>
<InputField
fieldName={'options.timezone'}
label={'Timezone'}
placeholder={'UTC'}
description={
'IANA timezone for the schedule (e.g. UTC, Europe/Athens)'
}
/>
</>
)}
{(form.watch('functionType') === 'webhook' ||
form.watch('functionType') === 'request') && (
Expand Down
2 changes: 1 addition & 1 deletion src/components/functions/CreateFunctionForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ export const CreateFunctionForm = ({
timeout: data.timeout,
inputs: {
cronPattern: options.cronString,
event: options.cronString,
timezone: options.timezone?.trim() || 'UTC',
},
})
.then(() => {
Expand Down
3 changes: 2 additions & 1 deletion src/components/functions/EditFunctionForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ export const EditFunctionForm = ({
functionData.inputs?.cronPattern ??
functionData.inputs?.event ??
'',
timezone: functionData.inputs?.timezone?.trim() || 'UTC',
},
}
: {}),
Expand Down Expand Up @@ -190,7 +191,7 @@ export const EditFunctionForm = ({
timeout: data.timeout,
inputs: {
cronPattern: options.cronString,
event: options.cronString,
timezone: options.timezone?.trim() || 'UTC',
},
})
.then(() => {
Expand Down
45 changes: 33 additions & 12 deletions src/components/functions/zod.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
import { z } from 'zod';
import { parseExpression } from 'cron-parser';

function validateCronString(value: string): boolean {
function validateIanaTimezone(value: string): boolean {
try {
parseExpression(value.trim(), { tz: 'UTC' });
Intl.DateTimeFormat('en-US', { timeZone: value.trim() });
return true;
} catch {
return false;
}
}

function validateCronString(value: string, timezone = 'UTC'): boolean {
try {
parseExpression(value.trim(), { tz: timezone.trim() || 'UTC' });
return true;
} catch {
return false;
Expand Down Expand Up @@ -134,16 +143,28 @@ export const EventOptions = z.object({
functionType: z.literal('event'),
eventName: z.string(),
});
export const CronOptions = z.object({
functionType: z.literal('cron'),
cronString: z
.string()
.min(1, 'Cron schedule is required')
.refine(validateCronString, {
message:
'Invalid cron pattern. Use 5 fields: minute hour day month weekday (UTC). Example: */5 * * * *',
}),
});
export const CronOptions = z
.object({
functionType: z.literal('cron'),
cronString: z.string().min(1, 'Cron schedule is required'),
timezone: z
.string()
.trim()
.default('UTC')
.refine(v => v.length > 0 && validateIanaTimezone(v), {
message:
'Invalid timezone. Use an IANA name (e.g. UTC, Europe/Athens).',
}),
})
.superRefine((data, ctx) => {
if (!validateCronString(data.cronString, data.timezone)) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
path: ['cronString'],
message: `Invalid cron pattern. Use 5 fields: minute hour day month weekday (${data.timezone}). Example: */5 * * * *`,
});
}
});
export const SocketOptions = z.object({
functionType: z.literal('socket'),
});
Expand Down
1 change: 1 addition & 0 deletions src/lib/models/functions/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ interface IWebInputsInterface {
method?: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH';
event?: string;
cronPattern?: string;
timezone?: string;
bodyParams?: { [key: string]: any }[];
urlParams?: { [key: string]: any }[];
queryParams?: { [key: string]: any }[];
Expand Down
Loading