Skip to content
Back to skills

Runtime Validation

ASecurity

Provides runtime validation patterns for data integrity and type safety at system boundaries.

  • 5 stars
  • 0 votes
  • 0 copies
  • 2 views
  • Added June 6, 2026
ai-agentspythonbashnodedockertestingdebugginggitapidatabaseci/cd

Works with

  • cli
  • api

Security analysis

A96/100
  • mediumInstalls packages at runtime which could introduce malicious dependencies

Pro shows the line behind each finding and how to fix it

Scanned June 6, 2026

npx -y skills add nxtg-ai/forge-plugin --skill runtime-validation --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Runtime Validation?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Runtime Validation
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/nxtg-ai-runtime-validation/badge)](https://www.skillsdirectory.com/skills/nxtg-ai-runtime-validation)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: Runtime Validation
description: Provides runtime validation patterns for data integrity and type safety at system boundaries.
---

# NXTG-Forge Runtime Validation Layer

## Overview

The Runtime Validation Layer is a new testing protocol that monitors application behavior during execution to catch errors that traditional unit tests miss. It bridges the gap between static testing and production monitoring.

## Motivation

**Problem**: Unit tests can achieve 100% pass rates while production code fails with runtime errors (e.g., Pydantic validation errors, data integrity violations).

**Solution**: Monitor application logs and runtime behavior during test execution to detect:
- Pydantic validation errors
- Data integrity violations
- Mathematical invariant violations
- Contract mismatches between layers

## Architecture

### Components

1. **Log Monitor Agent**
   - Continuously scans application logs during test runs
   - Detects patterns indicating validation errors
   - Reports violations in real-time

2. **Contract Validator**
   - Validates API responses against Pydantic models
   - Ensures data flows correctly between architectural layers
   - Detects schema drift

3. **Data Integrity Checker**
   - Validates mathematical invariants (e.g., density ≤ 1.0, percentages ≤ 100%)
   - Checks referential integrity across databases
   - Ensures consistency of derived values

4. **Anomaly Detector**
   - Flags unusual patterns in runtime data
   - Detects performance regressions
   - Identifies memory leaks or resource exhaustion

### Integration Points

```python
# Runtime validation during tests
@pytest.fixture(scope="session")
def runtime_validator():
    """Start runtime validation for test session."""
    validator = RuntimeValidator(
        log_path="logs/app.log",
        error_patterns=[
            r"validation error for \w+",
            r"Input should be .* than",
            r"type=\w+_error",
        ]
    )
    validator.start()
    yield validator
    validator.stop()

    # Fail tests if runtime errors detected
    if validator.errors_found:
        pytest.fail(f"Runtime validation errors: {validator.get_summary()}")
```

### Test Pyramid Integration

```
Traditional Testing          Runtime Validation
===================         ===================

Unit Tests (70%)     -->    Monitor logs during unit tests
                           Validate data contracts

Integration (20%)    -->    Monitor cross-system flows
                           Validate API contracts

E2E Tests (10%)      -->    Monitor full workflows
                           Validate user journeys
```

## Implementation Strategy

### Phase 1: Log Monitoring (Week 1)
- Implement LogMonitor class
- Add to existing test fixtures
- Report Pydantic validation errors

### Phase 2: Contract Validation (Week 2)
- Implement ContractValidator
- Hook into API response paths
- Validate against Pydantic models

### Phase 3: Data Integrity (Week 3)
- Implement DataIntegrityChecker
- Define invariants for each domain
- Add to integration tests

### Phase 4: Anomaly Detection (Week 4)
- Implement AnomalyDetector
- Baseline normal behavior
- Flag deviations

## Usage Examples

### Basic Log Monitoring

```python
# tests/runtime/test_log_monitor.py
import pytest
from src.runtime_validation import LogMonitor

@pytest.fixture
def log_monitor():
    monitor = LogMonitor("logs/app.log")
    monitor.add_error_pattern(r"validation error")
    monitor.start()
    yield monitor
    monitor.stop()

def test_api_endpoint_with_monitoring(log_monitor, client):
    """Test API endpoint while monitoring logs."""
    response = client.post("/api/graph/overview")

    # Traditional assertion
    assert response.status_code == 200

    # Runtime validation
    errors = log_monitor.get_errors()
    assert len(errors) == 0, f"Runtime errors detected: {errors}"
```

### Contract Validation

```python
# tests/runtime/test_contract_validator.py
from src.runtime_validation import ContractValidator
from api.models.graph import GraphOverviewResponse

def test_graph_overview_contract(client):
    """Validate API response matches Pydantic contract."""
    validator = ContractValidator()

    response = client.get("/api/graph/overview")
    data = response.json()

    # Validate against Pydantic model
    validation_result = validator.validate(
        data=data,
        model=GraphOverviewResponse
    )

    assert validation_result.is_valid, validation_result.errors
```

### Data Integrity Checking

```python
# tests/runtime/test_data_integrity.py
from src.runtime_validation import DataIntegrityChecker

def test_cluster_density_invariant(graph_service):
    """Validate mathematical invariants in graph clustering."""
    checker = DataIntegrityChecker()

    # Define invariants
    checker.add_invariant(
        name="cluster_density",
        check=lambda d: 0.0 <= d['density'] <= 1.0,
        error="Cluster density must be between 0 and 1"
    )

    # Generate clusters
    clusters = graph_service.compute_clusters()

    # Check invariants
    for cluster in clusters:
        violations = checker.check(cluster.cluster.dict())
        assert len(violations) == 0, f"Invariant violations: {violations}"
```

## CI/CD Integration

### GitHub Actions Workflow

```yaml
# .github/workflows/runtime-validation.yml
name: Runtime Validation

on: [push, pull_request]

jobs:
  runtime-validation:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v2

      - name: Start services
        run: docker-compose up -d

      - name: Run tests with runtime validation
        run: |
          pytest tests/runtime/ \
            --runtime-validation \
            --log-monitor \
            --contract-validation \
            --fail-on-runtime-errors

      - name: Upload runtime validation report
        if: failure()
        uses: actions/upload-artifact@v2
        with:
          name: runtime-validation-report
          path: reports/runtime-validation.html
```

## Metrics and Reporting

### Key Metrics
- **Runtime Error Rate**: Errors per 1000 test executions
- **Contract Violation Rate**: Schema mismatches per endpoint
- **Invariant Violation Rate**: Data integrity failures
- **Detection Latency**: Time to detect runtime errors

### Dashboard Example

```
╔══════════════════════════════════════════════════════╗
║          Runtime Validation Dashboard                ║
╠══════════════════════════════════════════════════════╣
║                                                      ║
║  Runtime Errors:        3 🔴                         ║
║  Contract Violations:   1 🟡                         ║
║  Invariant Violations:  0 🟢                         ║
║                                                      ║
║  Recent Errors:                                     ║
║  • ClusterMetadata.density > 1.0 (18:00:21)        ║
║  • Invalid enum value for NodeType (17:45:12)      ║
║  • Missing required field 'label' (17:30:05)       ║
║                                                      ║
║  Coverage:                                          ║
║  • Log Monitoring:     ████████░░ 85%              ║
║  • Contract Validation: ███████░░░ 72%              ║
║  • Data Integrity:     █████░░░░░ 56%              ║
║                                                      ║
╚══════════════════════════════════════════════════════╝
```

## Best Practices

### DO
- ✅ Monitor logs during ALL test runs
- ✅ Define clear invariants for your domain
- ✅ Fail fast on runtime errors
- ✅ Include runtime validation in CI/CD
- ✅ Track metrics over time
- ✅ Use runtime validation to find test gaps

### DON'T
- ❌ Ignore runtime errors in logs
- ❌ Assume unit test pass = production ready
- ❌ Skip runtime validation for "simple" changes
- ❌ Suppress validation errors without fixing root cause
- ❌ Run runtime validation only in production

## Example: Finding the Graph Density Bug

This real example shows how runtime validation catches bugs unit tests miss:

```python
# Unit test that passed (but missed the bug)
def test_cluster_density_calculation():
    clustering = GraphClusteringService()

    # Mock data
    nodes = ['A', 'B', 'C']
    edges = [('A', 'B'), ('B', 'C')]

    density = clustering.calculate_density(nodes, edges)
    assert density > 0  # ✅ Passes but doesn't check upper bound!

# Runtime validation that caught the bug
@runtime_validation
def test_graph_overview_endpoint():
    response = client.get("/api/graph/overview")

    # Runtime validator detected in logs:
    # "validation error for ClusterMetadata
    #  density Input should be less than or equal to 1
    #  [input_value=4.5]"

    # This revealed the density calculation was wrong!
```

## Quick Start

1. **Install runtime validation**:
   ```bash
   pip install forge-runtime-validation
   ```

2. **Add to pytest.ini**:
   ```ini
   [tool:pytest]
   addopts = --runtime-validation
   runtime_validation_log = logs/app.log
   runtime_validation_fail_on_errors = true
   ```

3. **Run tests**:
   ```bash
   pytest --runtime-validation
   ```

## ROI Justification

### Time Investment
- Initial setup: 4-8 hours
- Per-test overhead: ~5%
- Maintenance: 2 hours/month

### Value Delivered
- **Catches 15-30% more bugs** than unit tests alone
- **Reduces production incidents** by 40-60%
- **Improves data quality** through invariant checking
- **Accelerates debugging** with detailed error context

### Case Study: 3db Platform
- Unit tests: 306/306 passing (100%)
- Runtime validation: Found 5 critical bugs in first run
- Production impact: Prevented 3 data corruption issues
- Time saved: 20+ hours of production debugging

---

**Last Updated**: 2026-01-23
**Version**: 1.0.0
**Author**: NXTG-Forge Team
**Status**: Proposed Standard

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.