mirror of
https://gitea.toothfairyai.com/ToothFairyAI/tf_code.git
synced 2026-04-01 06:42:26 +00:00
22 lines
574 B
TypeScript
22 lines
574 B
TypeScript
import { z } from "zod"
|
|
|
|
export function fn<T extends z.ZodType, Result>(schema: T, cb: (input: z.infer<T>) => Result) {
|
|
const result = (input: z.infer<T>) => {
|
|
let parsed
|
|
try {
|
|
parsed = schema.parse(input)
|
|
} catch (e) {
|
|
console.trace("schema validation failure stack trace:")
|
|
if (e instanceof z.ZodError) {
|
|
console.error("schema validation issues:", JSON.stringify(e.issues, null, 2))
|
|
}
|
|
throw e
|
|
}
|
|
|
|
return cb(parsed)
|
|
}
|
|
result.force = (input: z.infer<T>) => cb(input)
|
|
result.schema = schema
|
|
return result
|
|
}
|