// Example custom step for data validation
const ValidationStep: Step<
BaseReturnValues<{
isValid: boolean;
errors?: string[];
}>,
{ type: 'validation.checking'; field: string },
{ data: Record<string, unknown>; rules: ValidationRule[] },
undefined
> = {
name: 'validation',
beforeExecute: async (context) => {
return {
data: context.state.formData,
rules: context.state.validationRules,
};
},
execute: async function* (params, context) {
const { data, rules } = params;
const errors: string[] = [];
for (const rule of rules) {
yield { type: 'validation.checking', field: rule.field };
if (!rule.validate(data[rule.field])) {
errors.push(`${rule.field}: ${rule.message}`);
}
}
return {
next: errors.length ? 'validation-failed' : 'validation-passed',
state: {
isValid: errors.length === 0,
errors: errors.length ? errors : undefined,
},
};
},
afterExecute: async (result) => {
// Perform any cleanup or logging
return result;
},
};