{"record":{"id":"bf1995a865a9c1fc","repo":"rohitg00/ai-engineering-from-scratch","slug":"rule-r-get-name-missing-substring-or-regex","errorCode":null,"errorMessage":"rule {r.get('name')} missing substring or regex","messagePattern":"rule (.+?) missing substring or regex","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"phases/19-capstone-projects/83-prompt-injection-detector/code/main.py","lineNumber":135,"sourceCode":"    score = sum(1 for w in words if w in _COMMON_WORDS)\n    if score >= 2:\n        return text + \" \" + candidate\n    return text\n\n\nclass Detector:\n    def __init__(self, rules: Iterable[dict[str, object]] | None = None) -> None:\n        rules_list = list(rules) if rules is not None else all_rules()\n        self.substring_rules: list[dict[str, object]] = []\n        self.regex_rules: list[dict[str, object]] = []\n        for r in rules_list:\n            if \"substring\" in r:\n                self.substring_rules.append(r)\n            elif \"regex\" in r:\n                compiled = re.compile(str(r[\"regex\"]), re.IGNORECASE | re.DOTALL)\n                self.regex_rules.append({**r, \"_compiled\": compiled})\n            else:\n                raise ValueError(f\"rule {r.get('name')} missing substring or regex\")\n\n    def analyze(self, prompt: str) -> Verdict:\n        normalized = normalize(prompt)\n        haystacks = (prompt.lower(), normalized)\n        scores_by_category: dict[str, float] = {}\n        fired: list[str] = []\n        for r in self.substring_rules:\n            needle = str(r[\"substring\"]).lower()\n            if any(needle in h for h in haystacks):\n                cat = str(r[\"category\"])\n                score = float(r[\"score\"])\n                scores_by_category[cat] = max(scores_by_category.get(cat, 0.0), score)\n                fired.append(str(r[\"name\"]))\n        for r in self.regex_rules:\n            compiled: re.Pattern = r[\"_compiled\"]\n            if any(compiled.search(h) for h in haystacks):\n                cat = str(r[\"category\"])\n                score = float(r[\"score\"])","sourceCodeStart":117,"sourceCodeEnd":153,"githubUrl":"https://github.com/rohitg00/ai-engineering-from-scratch/blob/39ea8a1c6d0b61f071226eff7ede4d4105fed820/phases/19-capstone-projects/83-prompt-injection-detector/code/main.py#L117-L153","documentation":"Error \"rule {r.get('name')} missing substring or regex\" thrown in rohitg00/ai-engineering-from-scratch.","triggerScenarios":"Thrown at phases/19-capstone-projects/83-prompt-injection-detector/code/main.py:135 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":[],"exampleFix":null,"handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"39ea8a1c6d0b61f071226eff7ede4d4105fed820","analyzedAt":"2026-08-26T03:13:46.626Z","schemaVersion":2},"datasetVersion":"2026-08-26T07:17:17.940Z"}