36 lines
1.1 KiB
TypeScript
36 lines
1.1 KiB
TypeScript
import { CronPattern } from 'croner';
|
|
import { z } from 'zod';
|
|
|
|
export const EntityIdSchema = z
|
|
.string()
|
|
.min(1, 'ID must not be empty')
|
|
.max(64, 'ID must be at most 64 characters')
|
|
.regex(/^[a-zA-Z0-9][a-zA-Z0-9._-]{0,63}$/, 'ID must start with alphanumeric and contain only [a-zA-Z0-9._-]');
|
|
|
|
export const PortSchema = z.number().int().min(1).max(65535);
|
|
|
|
export const CronExpressionSchema = z
|
|
.string()
|
|
.min(1)
|
|
.superRefine((expr, ctx) => {
|
|
try {
|
|
new CronPattern(expr);
|
|
} catch (err) {
|
|
ctx.addIssue({
|
|
code: z.ZodIssueCode.custom,
|
|
message: `Invalid cron expression "${expr}": ${err instanceof Error ? err.message : String(err)}`,
|
|
});
|
|
}
|
|
});
|
|
|
|
export const InputDefSchema = z.object({
|
|
name: EntityIdSchema,
|
|
type: z.enum(['string', 'number', 'boolean', 'server', 'select']).default('string'),
|
|
label: z.string().min(1).optional(),
|
|
description: z.string().optional(),
|
|
required: z.boolean().default(false),
|
|
default: z.union([z.string(), z.number(), z.boolean()]).optional(),
|
|
options: z.array(z.string()).optional(),
|
|
});
|
|
|
|
export type InputDef = z.infer<typeof InputDefSchema>;
|