Validation Scripts

Validation scripts allow you to execute custom logic at specific points during the validation process. These global-level scripts provide hooks for setup, cleanup, reporting, and integration with external systems during validation workflows.

Validation Pipeline Execution

Configuration

{% @mermaid/diagram content="flowchart TD A[Validation Started] --> B[Fetch/Create Target Org] B --> C[Compute Impacted Packages] C --> D[Build Impacted Packages] D --> E[🔧 PRE-VALIDATION SCRIPT] E --> F[Deploy Packages to Target Org] F --> G[Run Apex Tests] G --> H[Validate Coverage] H --> I{Validation Result} I -->|Success| J[🏁 POST-VALIDATION SCRIPT] I -->|Failure| K[🏁 POST-VALIDATION SCRIPT] J --> L[Generate Reports] K --> L L --> M[Cleanup Resources]

style E fill:#e1f5fe,stroke:#0277bd,stroke-width:2px
style J fill:#e8f5e8,stroke:#2e7d32,stroke-width:2px
style K fill:#ffebee,stroke:#c62828,stroke-width:2px" %}

Add script paths to your sfdx-project.json file:

{
  "plugins": {
    "sfp": {
      "validateScripts": {
        "preValidation": "./scripts/pre-validate.sh",
        "postValidation": "./scripts/post-validate.sh"
      }
    }
  }
}

Script Arguments

Scripts receive three arguments in this order:

  1. Context File Path - Absolute path to temporary JSON file containing validation context

  2. Target Org - Username of the target organization for validation

  3. Hub Org - Username of the hub organization (empty string "" if not available)

Context Data Structure

Pre-Validation Context

The context file contains information about packages ready for validation:

Post-Validation Context

The context file includes all pre-validation data plus validation results:

Example Scripts

Pre-Validation Script

Post-Validation Script

Error Handling & Behavior

Script Type
Failure Behavior
Timeout
Use Cases

Pre-validation

Halts validation process

30 minutes

Setup test data, configure environments, validate prerequisites

Post-validation

Logged as warning, validation continues

30 minutes

Cleanup resources, send notifications, generate reports

Best Practices

  • Make scripts executable: chmod +x scripts/pre-validate.sh

  • Use set -e: Exit on errors to ensure proper failure handling

  • Parse JSON safely: Use jq for reliable JSON parsing

  • Handle missing data: Check if fields exist before using them

  • Log clearly: Scripts appear in validation logs with CI/CD folding

  • Keep scripts fast: Remember the 30-minute timeout limit

  • Test locally: Validate script behavior before committing

Common Use Cases

Pre-Validation Scripts

  • Set up test data specific to validation scenarios

  • Configure external API endpoints for testing

  • Validate prerequisites (licenses, feature flags, etc.)

  • Initialize monitoring or logging for the validation process

Post-Validation Scripts

  • Clean up test data created during validation

  • Send notifications to Slack, Teams, or other systems

  • Generate custom reports or metrics

  • Update external tracking systems with validation results

  • Archive validation artifacts or logs

Last updated