mirror of
https://github.com/kunkunsh/kunkun.git
synced 2025-04-11 17:29:44 +00:00

the worker package initialize comlink API's and cause trouble for regular website when ui package is imported
48 lines
1.6 KiB
TypeScript
48 lines
1.6 KiB
TypeScript
import { FormNodeNameEnum, type FormSchema } from "@kksh/api/models"
|
|
import type { BaseIssue, BaseSchema } from "valibot"
|
|
import * as v from "valibot"
|
|
|
|
function addDefaultToSchema(
|
|
schema: BaseSchema<unknown, unknown, BaseIssue<unknown>>,
|
|
field: FormSchema.BaseField
|
|
) {
|
|
if (field.default) {
|
|
schema = v.optional(schema, field.default)
|
|
}
|
|
return schema
|
|
}
|
|
|
|
export function buildFormSchema(form: FormSchema.Form): v.ObjectSchema<any, undefined> {
|
|
let schema = v.object({})
|
|
for (const field of form.fields) {
|
|
let fieldSchema: any = undefined
|
|
if (field.nodeName === FormNodeNameEnum.Input) {
|
|
fieldSchema = v.string()
|
|
} else if (field.nodeName === FormNodeNameEnum.Number) {
|
|
fieldSchema = v.number()
|
|
} else if (field.nodeName === FormNodeNameEnum.Select) {
|
|
fieldSchema = v.string()
|
|
// fieldSchema = v.picklist((field as FormSchema.SelectField).options)
|
|
// schema = v.object({ ...schema.entries, [field.key]: fieldSchema })
|
|
// continue
|
|
} else if (field.nodeName === FormNodeNameEnum.Boolean) {
|
|
fieldSchema = v.boolean()
|
|
} else if (field.nodeName === FormNodeNameEnum.Date) {
|
|
fieldSchema = v.date()
|
|
} else {
|
|
console.warn(`Unknown field type: ${field.nodeName}`)
|
|
}
|
|
fieldSchema = addDefaultToSchema(fieldSchema, field)
|
|
if ((field as FormSchema.BaseField).optional) {
|
|
fieldSchema = v.nullable(v.optional(fieldSchema))
|
|
}
|
|
if ((field as FormSchema.BaseField).description) {
|
|
fieldSchema = v.pipe(fieldSchema, v.description((field as FormSchema.BaseField).description!))
|
|
}
|
|
if (fieldSchema) {
|
|
schema = v.object({ ...schema.entries, [field.key]: fieldSchema })
|
|
}
|
|
}
|
|
return schema
|
|
}
|