Parse arbitrary data or promise returning data using a schema.
The function throws if a data-promise is passed in and the promise rejects.
E.g.:
const parsedData = await parseDataAs({ test: "1" }, z.object({ test: z.number() )}))console.log(parsedData)// -> output: `1` (as a number, as `z` also deserializes)
Also works with async data, e.g., when fetching from another API or DB:
const fakeDatabaseQuery = async () => { test: "1" }const parsedData = await parseDataAs(fakeDatabaseQuery, z.object({ test: z.number() )}))console.log(parsedData)// -> output: `1` (as a number, as `z` also deserializes)
Input to parse using the passed schema
schema
Error code of error if parsing fails
Optional
Optional error message if parsing fails
Generated using TypeDoc
Parse arbitrary data or promise returning data using a schema.
The function throws if a data-promise is passed in and the promise rejects.
E.g.:
Also works with async data, e.g., when fetching from another API or DB: