Skip to content

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 ​

ParameterTypeDescription
schemaSchemaThe schema to validate against
valueunknownAny data
optionsOptions{ 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 ​