Types
Type definitions for contexts, rules, policies, and results
Types
Type definitions for contexts, rules, policies, and results in Bantai.
ContextDefinition
Type representing a context definition.
type ContextDefinition<
T extends z.ZodRawShape,
TTools extends Record<string, unknown> = {}
> = {
schema: z.ZodObject<T>;
defaultValues: Partial<z.infer<z.ZodObject<T>>>;
tools: TTools;
}Properties:
schema: Zod object schemadefaultValues: Partial default values for context fieldstools: Tools object available to rules
RuleDefinition
Type representing a rule definition.
type RuleDefinition<
TContext extends ContextDefinition<z.ZodRawShape, Record<string, unknown>>,
TName extends string = string
> = {
name: TName;
evaluate: (input: z.infer<z.ZodObject<ExtractShape<TContext['schema']>>>) => Promise<RuleResult>;
hooks: {
onAllow?: RuleHookFnAsync<ExtractShape<TContext['schema']>, ExtractTools<TContext>>;
onDeny?: RuleHookFnAsync<ExtractShape<TContext['schema']>, ExtractTools<TContext>>;
};
}Properties:
name: Rule identifierevaluate: Async function that evaluates the rulehooks: Optional hooks for side effects
PolicyDefinition
Type representing a policy definition.
type PolicyDefinition<
TContext extends ContextDefinition<z.ZodRawShape, Record<string, unknown>>,
TName extends string,
TRules extends readonly RuleDefinition<TContext, string>[]
> = {
name: TName;
rules: Map<string, TRules[number]> & {
get<K extends ExtractRuleName<TRules[number]>>(key: K): RulesMap<TRules>[K] | undefined;
has<K extends ExtractRuleName<TRules[number]>>(key: K): boolean;
};
context: TContext;
options: {
defaultStrategy: PolicyStrategy;
};
}Properties:
name: Policy identifierrules: Map of rules with type-safe accessorscontext: Context used by the policyoptions: Policy configuration including default strategy
RuleResult
Type representing the result of a rule evaluation.
type RuleResult = {
allowed: boolean;
reason: string | null;
skipped: boolean;
meta?: Record<string, unknown>;
}Properties:
allowed:trueif rule allows,falseif rule deniesreason: Optional reason messageskipped:trueif rule was skipped,falseotherwisemeta?: Optional metadata record for additional context
PolicyResult
Type representing the result of a policy evaluation.
type PolicyResult = {
decision: 'allow' | 'deny';
isAllowed: boolean;
reason: 'policy_violated' | 'policy_enforced';
violatedRules: ViolatedRule[];
evaluatedRules: EvaluatedRule[];
strategy: 'preemptive' | 'exhaustive';
}Properties:
decision: Final decision ('allow'or'deny')isAllowed: Boolean convenience property (truewhendecision === 'allow')reason: Reason for the decision'policy_enforced': All rules passed'policy_violated': One or more rules failed
violatedRules: Array of violated rulesevaluatedRules: Array of all evaluated rules (both passed and failed)strategy: Strategy used for evaluation
ViolatedRule
Type representing a violated rule.
type ViolatedRule = {
name: string;
result: RuleResult;
}Properties:
name: Name of the violated ruleresult: Rule result that caused the violation
EvaluatedRule
Type representing an evaluated rule (both passed and failed).
type EvaluatedRule = {
rule: RuleDefinition;
result: RuleResult;
}Properties:
rule: The rule definition that was evaluatedresult: The result of the rule evaluation
PolicyStrategy
Type representing evaluation strategy.
type PolicyStrategy = 'preemptive' | 'exhaustive'Values:
'preemptive': Stops at first violation (fail-fast)'exhaustive': Collects all violations
Related Documentation
- Functions - Functions that use these types
- Utility Types - Helper types for working with contexts