Function Signatures
Type signatures for rule and hook functions
Function Signatures
Type signatures for rule evaluation functions and hooks.
RuleEvaluateFnAsync
Type for rule evaluation functions.
type RuleEvaluateFnAsync<
T extends z.ZodRawShape,
TTools extends Record<string, unknown> = {}
> = (
input: z.infer<z.ZodObject<T>>,
context: {
tools: TTools;
}
) => Promise<RuleResult>Parameters:
input: Input data matching the context schemacontext: Context object containing toolstools: Tools available to the rule
Returns: Promise<RuleResult>
Example:
const evaluate: RuleEvaluateFnAsync<
{ userId: z.ZodString; age: z.ZodNumber },
{ logger: Console }
> = async (input, { tools }) => {
tools.logger.log(`Checking user ${input.userId}`);
if (input.age >= 18) {
return allow({
reason: 'User is of legal age',
meta: { verifiedAge: input.age, userId: input.userId }
});
}
return deny({
reason: 'User must be 18 or older',
meta: { providedAge: input.age, requiredAge: 18 }
});
};RuleHookFnAsync
Type for rule hook functions.
type RuleHookFnAsync<
T extends z.ZodRawShape,
TTools extends Record<string, unknown> = {}
> = (
input: z.infer<z.ZodObject<T>>,
context: {
tools: TTools;
}
) => Promise<void>Parameters:
input: Input data matching the context schemacontext: Context object containing toolstools: Tools available to the hook
Returns: Promise<void>
Example:
const onAllow: RuleHookFnAsync<
{ userId: z.ZodString; age: z.ZodNumber },
{ logger: Console }
> = async (input, { tools }) => {
tools.logger.log(`Access granted to user ${input.userId}`);
// Perform side effects
};