parseAsync
The async version of parse: returns the data or rejects with ValidationError. Required if the schema contains checkAsync.
Signature
ts
function parseAsync<S extends Schema>(schema: S, value: unknown, options?: Options): Promise<Infer<S>>Parameters
| Parameter | Type | Description |
|---|---|---|
schema | Schema | The schema to validate against |
value | unknown | Any data |
options | Options | { all?: boolean } — as in validate |
Returns
A Promise with the cleaned data. Checks run in the same order as in validateAsync.
Example
Handler with a database check
ts
app.post('/signup', async (req, res, next) => {
try {
const data = await parseAsync(Signup, req.body);
await db.users.create(data);
res.status(201).end();
} catch (e) {
next(e); // ValidationError is handled by the error handler
}
});See also
- parse
- ValidationError — a shared error handler
- validateAsync