@mastra/core
Version:
77 lines • 3.2 kB
TypeScript
import { z } from 'zod/v4';
/**
* The predicate Zod schema. We define it via `z.lazy` so the recursive
* `and` / `or` / `not` branches can reference the top-level type.
*/
export declare const predicateSchema: z.ZodType<Predicate>;
export type PathOrLiteral = {
path: string;
} | {
literal: string | number | boolean | null;
};
export type Predicate = {
op: 'eq' | 'ne' | 'lt' | 'lte' | 'gt' | 'gte';
left: PathOrLiteral;
right: PathOrLiteral;
} | {
op: 'in' | 'notIn';
value: PathOrLiteral;
set: Array<string | number | boolean | null>;
} | {
op: 'exists' | 'notExists';
path: string;
} | {
op: 'truthy' | 'falsy';
value: PathOrLiteral;
} | {
op: 'and' | 'or';
args: Predicate[];
} | {
op: 'not';
arg: Predicate;
};
/** Sentinel returned by path resolvers when a path can't be resolved. */
export declare const MISSING: unique symbol;
export type Missing = typeof MISSING;
/**
* Normalize a raw predicate path. Accepts both plain dotted paths (`foo.bar`)
* and template-style paths (`${foo.bar}`). Returns `MISSING` for an empty path.
*/
export declare function normalizePredicatePath(rawPath: string): string | Missing;
/**
* Non-throwing dot-path traversal of an arbitrary object graph. Returns
* `MISSING` if any segment can't be resolved, so callers can distinguish
* "no value" from `null`.
*/
export declare function walk(root: unknown, path: string): unknown | Missing;
/** A domain-supplied path resolver over that domain's context shape. */
export type PredicatePathResolver<TCtx> = (rawPath: string, ctx: TCtx) => unknown | Missing;
/**
* Compose a typed predicate evaluator from a domain path resolver.
*
* The returned evaluator never throws for path resolution failures — missing
* paths propagate to `false` on comparison ops and `in`, to `true` on the
* negated ops `notIn` and `notExists` (a missing value is trivially "not in"
* any set), and to `false` on `exists`. It throws only if the
* predicate shape itself is malformed (which `predicateSchema.parse` catches
* at load time).
*/
export declare function createPredicateEvaluator<TCtx>(resolvePath: PredicatePathResolver<TCtx>): (pred: Predicate, ctx: TCtx) => boolean;
/**
* Validate that every path referenced by a predicate starts with one of the
* domain's known roots. Domains call this at definition time so typo'd roots
* fail loud where they're cheap — evaluation stays fail-quiet by design.
* Returns the list of offending paths (empty when valid).
*/
export declare function collectInvalidPredicatePaths(pred: Predicate, roots: readonly string[]): string[];
/**
* Produce a short human-readable label for a predicate, suitable for
* rendering as a condition-node label in a graph UI.
* Bounded output length; no user-controlled text is passed through
* unescaped — string literals go through JSON.stringify, and paths that
* contain anything beyond plain identifier/dot characters are rendered as
* JSON strings too, so a malicious value can't break out of the
* surrounding rendering.
*/
export declare function derivePredicateLabel(pred: Predicate, maxLength?: number): string;
//# sourceMappingURL=index.d.ts.map