manifold/web/pages/api/v0/_validate.ts
Ben Congdon c3bc25a4b9
Add API route for listing a bets by user (#567)
* Add API route for getting a user's bets

* Refactor bets API to use /bets

* Update /markets to use zod validation

* Update docs
2022-07-07 15:36:02 -07:00

18 lines
452 B
TypeScript

import { z } from 'zod'
import { ValidationError } from './_types'
export const validate = <T extends z.ZodTypeAny>(schema: T, val: unknown) => {
const result = schema.safeParse(val)
if (!result.success) {
const issues = result.error.issues.map((i) => {
return {
field: i.path.join('.') || null,
error: i.message,
}
})
throw new ValidationError(issues)
} else {
return result.data as z.infer<T>
}
}