defense-in-depth

Validate data at four layers to prevent invalid-data bugs.

6|1|Updated Oct 23, 2025
One-click install
npx skills add https://github.com/alexsandrocruz/ZenPowers --skill defense-in-depth-alexsandrocruz
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: defense-in-depth
Source: https://github.com/alexsandrocruz/ZenPowers/tree/main/skills/defense-in-depth
Command: npx skills add https://github.com/alexsandrocruz/ZenPowers --skill defense-in-depth-alexsandrocruz

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This Skill prevents recurring bugs caused by invalid data by mandating validation at every layer data passes through. It ensures that even if one check is bypassed, subsequent layers will catch the issue, making bugs structurally impossible.

Core Features & Use Cases

  • Four-Layer Validation: Implements checks at Entry Point, Business Logic, Environment Guards, and Debug Instrumentation.
  • Structural Bug Prevention: Ensures comprehensive validation across the entire data flow, not just at a single point.
  • Forensic Context Capture: Includes debug logging to capture context for deeper analysis when issues do arise.
  • Use Case: When invalid data causes failures deep in execution, use this Skill to add robust validation at multiple system layers, making the bug impossible to reproduce.

Quick Start

Layer 1: Entry Point Validation

public static async Task<Project> CreateProjectAsync(string name, string workingDirectory)

{

if (string.IsNullOrWhiteSpace(workingDirectory)) { throw new ArgumentException("..."); }

if (!Directory.Exists(workingDirectory)) { throw new DirectoryNotFoundException("..."); }

// ... proceed

}

Layer 2: Business Logic Validation

public static async Task<Workspace> InitializeWorkspaceAsync(string projectDir, string sessionId)

{

if (string.IsNullOrEmpty(projectDir)) { throw new InvalidOperationException("..."); }

// ... proceed

}

Layer 3: Environment Guards

public static async Task GitInitAsync(string directory)

{

if (Environment.GetEnvironmentVariable("DOTNET_ENVIRONMENT") == "Test") { /* refuse outside temp dir */ }

// ... proceed

}

Layer 4: Debug Instrumentation

_logger.LogDebug("About to git init: {Directory}, CWD: {CurrentDirectory}, Stack: {StackTrace}", directory, Environment.CurrentDirectory, stackTrace);

Frequently Asked Questions about defense-in-depth

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I validate data at multiple layers to prevent bugs from invalid input?

Multi-layer validation catches invalid data across entry points, business logic, environment checks, and debug instrumentation. Each layer acts as a backup, ensuring that even if one validation is bypassed, subsequent checks prevent the bug from propagating deeper into execution.

What's the best way to implement defensive programming across an entire application?

Defensive programming applies explicit validation checks at four distinct layers: API entry validation, business logic checks, environment guards, and debug instrumentation. This approach makes bugs structurally impossible by enforcing data integrity at every transition point in your code.

How do I catch data integrity errors before they cause deep failures?

Add validation checks at the entry point, business logic layer, environment guards, and debug instrumentation with context-rich logging. Capturing data-flow traces at each layer reveals where invalid data entered and prevents cascading failures downstream.

When should I add validation beyond the API entry point?

Add validation at every layer because entry-point checks alone miss invalid data introduced by internal code paths, environment state changes, or runtime mutations. Four-layer validation ensures comprehensive coverage and catches bugs that single-point validation cannot prevent.

Can defensive programming reduce the time spent debugging invalid data issues?

Yes. Multi-layer validation with forensic logging captures context at each validation point, making it structurally impossible for invalid data to propagate silently. When issues do occur, the captured context reveals the exact layer and data state where validation failed.

What error handling pattern works best for preventing bugs in business logic?

Explicit per-layer validation with clear error boundaries at entry points, business logic checks, and environment guards prevents invalid state. Combined with debug instrumentation and context-rich logging, this pattern makes invalid data failures immediate and traceable rather than hidden.