Combining Built-in Rules
Create a custom validator by combining built-in rules:Security Presets
ast-guard includes pre-built security presets:Writing Custom Rules
Rule Interface
A rule has a name, a description, a default severity and avalidate method. It reports what it finds through context.report; it does not return a list.
JSAstValidator constructor runs in every validation that does not switch it off by name (rules: { 'rule-name': false }). The validator collects at most maxIssues issues (100 by default), so a rule that reports once per node cannot make the result grow with the source.
Example: Custom Rule
Example: Block Specific API Calls
callTool is called directly with a string literal. The AgentScript preset already requires exactly that form (staticCallTarget), and its allowedToolNames option restricts the names; a rule like this one adds to it and does not stand alone.
Extending Presets
A preset function returns an array of rules. Add rules to it:Removing Rules from Presets
Filter out rules you don’t need:Rule Ordering
Rules are executed in array order. For performance, order rules by:- Fast rejections first - Rules that quickly identify invalid code
- Complex analysis last - Rules that traverse the entire AST
Testing Custom Rules
Related
- Security Rules - Built-in rules reference
- AgentScript Preset - Default preset
- Overview - Getting started