hiling-opengraph/util.ts

27 lines
472 B
TypeScript
Raw Normal View History

2023-10-11 12:08:57 +07:00
import { z } from './deps.ts'
interface ValidationError {
field: string | number,
message: string
}
function validateBody(schema: z.AnyZodObject, body: object) {
try {
schema.parse(body)
} catch (err) {
const error = err as z.ZodError;
const errArr: Array<ValidationError> = [];
error.issues.forEach(x => {
errArr.push({
field: x.path[0],
message: x.message
})
})
throw errArr
}
}
export {
validateBody
}