Define tasks with a runtime payload schema and validate the payload before running the task.
schemaTask
function allows you to define a task with a runtime payload schema. This schema is used to validate the payload before running the task or when triggering a task directly. If the payload does not match the schema, the task will not execute.
schemaTask
takes all the same options as task, with the addition of a schema
field. The schema
field is a schema parser function from a schema library or or a custom parser function.
task
and schemaTask
into a single function, but because
that would be a breaking change, we are keeping them separate for now.ZodError
.
We will also validate the payload every time before the task is run, so you can be sure that the payload is always valid. In the example above, the task would fail with a TaskPayloadParsedError
error and skip retrying if the payload does not match the schema.
{ name?: string, age: number; dob: string }
, but the run payload type is { name: string, age: number; dob: Date }
. So you can trigger the task with a payload like this:
parse
function.