18 lines
1.2 KiB
TypeScript
18 lines
1.2 KiB
TypeScript
export type ControlType = 'text' | 'textArea' | 'select' | 'dateTime';
|
|
export type Field = { id: string; key: string; label: string; control: ControlType; required: boolean; placeholder?: string; options?: string[] };
|
|
|
|
export function buildSchemas(title: string, fields: Field[]) {
|
|
const properties: Record<string, unknown> = {};
|
|
for (const field of fields) {
|
|
properties[field.key] = field.control === 'select'
|
|
? { type: 'string', enum: field.options?.filter(Boolean) ?? [] }
|
|
: field.control === 'dateTime'
|
|
? { type: 'string', format: 'date-time' }
|
|
: { type: 'string', minLength: field.required ? 1 : 0, maxLength: field.control === 'textArea' ? 2000 : 255 };
|
|
}
|
|
return {
|
|
dataSchema: { $id: `${title.toLowerCase().replace(/\s+/g, '-')}-draft`, title, type: 'object', required: fields.filter(f => f.required).map(f => f.key), properties },
|
|
uiSchema: { description: `${title} · 由 AIOA 表单设计器生成`, sections: [{ title: '基本信息', controls: fields.map(f => ({ field: f.key, label: f.label, control: f.control, placeholder: f.placeholder, ...(f.control === 'select' ? { optionLabels: Object.fromEntries((f.options ?? []).map(v => [v, v])) } : {}) })) }] },
|
|
};
|
|
}
|