# Conditional Validation Support in Rudder CLI

{{< youtube bwxpUz9Q4r8 >}}<br/>

RudderStack's [Tracking Plans]({{< ref "data-governance/tracking-plans/_index.md" >}}) enable data teams to apply type-safe governance to event schemas. While our governance features ensure consistency and predictability, real-world applications often require schemas to adapt based on business context.

With the release of [Conditional Validation]({{< ref "dev-tools/rudder-cli/data-catalog-and-tracking-plans/conditional-validation/" >}}), you can now define validation rules that dynamically adjust based on discriminator properties. This powerful capability addresses a common challenge — maintaining strict data quality standards while accommodating legitimate variations in your event data structure.

With conditional validation, you can:

- Define context-aware validation rules that adapt based on specific property values
- Prevent event proliferation by consolidating similar events with varied requirements
- Enforce granular validation on custom types that need to behave differently in different scenarios
- Maintain data quality without sacrificing flexibility for legitimate business variations

You can leverage conditional validation both on the event schema level and within custom types, and manage everything via [workspace configuration files]({{< ref "dev-tools/rudder-cli/data-catalog-and-tracking-plans/" >}}) using [Rudder CLI]({{< ref "dev-tools/rudder-cli/" >}}).

## Use cases

This section provides some examples of using the conditional validation feature.

### Event-level conditional validation

- **Adaptive page tracking**: Single `Page Viewed` event with different required properties based on page type — for example, search term for search pages, product details for product pages, checkout step for checkout pages
- **Context-aware form submissions**: Different required fields based on form type or user journey stage
- **Dynamic feature usage tracking**: Varying property requirements based on feature context or user role

See [Event Rule Variants]({{< ref "dev-tools/rudder-cli/data-catalog-and-tracking-plans/conditional-validation/event-rules.md" >}}) for more details on creating and managing event rule variants in your Tracking Plans using Rudder CLI.

### Custom type conditional validation

- **Regional address formats**: Different required fields for US addresses (state + ZIP) vs. UK addresses (postcode) vs. Japanese addresses (prefecture + district)
- **Manufacturer vs. marketplace products**: Require MSRP and Manufacturer ID for direct products, and require Seller ID for marketplace items
- **Account tier-specific profiles**: Different required profile fields for individual vs. business vs. enterprise accounts

See [Custom Type Variants]({{< ref "dev-tools/rudder-cli/data-catalog-and-tracking-plans/conditional-validation/custom-type-rules.md" >}}) for more details on creating and managing custom type variants using Rudder CLI.

## How it works

Conditional validation uses discriminator properties to determine which validation rules apply. A discriminator is a required property whose value triggers specific validation cases.

For example, in an ecommerce tracking scenario:

1. **Discriminator property**: `page_type` (enumerated list defined in Data Catalog)
2. **Variant cases**:

   - When `page_type` = `search_page`: Require the `search_term` property
   - When `page_type` = `product`: Require the `product` object property

**Result**: Same event handles both scenarios with appropriate validation. 

## Implementation

Conditional validation works at two levels:

#### Event rule variants

Define variants within your `tracking-plans.yaml` to create context-aware event validation:

```yaml
version: "rudder/v0.1"
kind: "tp"
metadata:
  name: "ecommerce_tracking_plan"
spec:
  id: "ecommerce_tracking_plan"
  display_name: "Ecommerce Store - Tracking Plan"
  description: "Comprehensive tracking plan for the casual apparel e-commerce demo site built with React TypeScript. Captures user behavior across product discovery, cart interactions, and purchase completion for analytics and optimization."
  rules:
    - type: "event_rule"
      id: "page_viewed_rule"
      event:
        $ref: "#/events/ecommerce_events/page_viewed"
        allow_unplanned: false
      properties: 
        # Base properties required for all page views
        - $ref: "#/properties/ecommerce_properties/page_type"
          required: true
        - $ref: "#/properties/ecommerce_properties/page_name"
          required: false
        - $ref: "#/properties/ecommerce_properties/page_url"
          required: false
      
      # Conditional Validation Variants
      variants:
        - type: discriminator
          discriminator: "#/properties/ecommerce_properties/page_type"
          cases:
            - display_name: "Product Detail Page"
              match: 
                - "product"
              description: "When user is viewing a specific product page"
              properties:
                - $ref: "#/properties/ecommerce_properties/product"
                  required: true
                  
            - display_name: "Search Results Page"
              match: 
                - "search"
              description: "When user is on search or category pages"
              properties:
                - $ref: "#/properties/ecommerce_properties/search_term"
                  required: true 
          
          # Default case for home and other pages (only page_url required)
          default:
            - $ref: "#/properties/ecommerce_properties/page_url"
              required: true%
```

#### Custom type variants

Define variants within your `custom-types.yaml` to create reusable, context-aware object types:

```yaml
version: "rudder/v0.1"
kind: "custom-types"
metadata:
  name: "ecommerce_types"
spec:
  types:
    # Product Type - Consolidates all product-related properties with conditional MSRP validation
    - id: Product_Object
      name: "Product_Object"
      description: "Custom type for product information with conditional manufacturer pricing validation"
      type: "object"
      properties:
        - $ref: "#/properties/ecommerce_properties/product_id"
          required: true
        - $ref: "#/properties/ecommerce_properties/product_sku"
          required: true
        - $ref: "#/properties/ecommerce_properties/product_name"
          required: true
        - $ref: "#/properties/ecommerce_properties/product_category"
          required: true
        - $ref: "#/properties/ecommerce_properties/product_price"
          required: true
        - $ref: "#/properties/ecommerce_properties/manufacturer_pricing"
          required: false      
      # Conditional Validation Variants for Product Object
      variants:
        - type: discriminator
          discriminator: "#/properties/ecommerce_properties/manufacturer_pricing"
          cases:
            - display_name: "Product with Manufacturer Pricing"
              match:
                - true
              description: "Products that have manufacturer suggested retail price (MSRP)"
              properties:
                - $ref: "#/properties/ecommerce_properties/product_msrp"
                  required: true

```

#### Deploy the changes to your workspace

```bash
rudder-cli apply -l ~/path_to_tracking_plan_dir
```

See the [Conditional Validation]({{< ref "dev-tools/rudder-cli/data-catalog-and-tracking-plans/conditional-validation/" >}}) documentation for more details.

<br />
