dbt-labs/dbt-core · error

Valid test name pattern

Error message

Valid test name pattern

What it means

This panic fires from `Regex::new(...).expect("Valid test name pattern")` while building the pattern used by `normalize_test_name` to strip dbt test-name suffixes. The pattern `^([a-zA-Z_][0-9a-zA-Z_]*)+` is a compile-time constant in the source, so the expect is an internal invariant assertion: it can only panic if the regex literal is edited into an invalid form or the `regex` crate rejects it at runtime. End users should never be able to trigger it via input.

Source

Thrown at crates/dbt-parser/src/resolve/resolve_tests/persist_generic_data_tests.rs:1791

        Some(self.source_name.clone())
    }

    fn base_tests(&self) -> FsResult<Option<Vec<DataTests>>> {
        base_tests_inner(
            self.table.tests.as_deref(),
            self.table.data_tests.as_deref(),
        )
    }

    fn column_tests(&self) -> FsResult<Option<BTreeMap<String, ColumnTestEntry>>> {
        column_tests_inner(&self.table.columns)
    }
}

/// Normalizes a test name following the existing dbt behavior
/// https://github.com/dbt-labs/dbt-core/blob/main/core/dbt/parser/generic_test_builders.py#L121-L122
fn normalize_test_name(input: &str) -> FsResult<String> {
    let name_pattern = Regex::new(r"^([a-zA-Z_][0-9a-zA-Z_]*)+").expect("Valid test name pattern");
    name_pattern
        .captures(input)
        .and_then(|caps| caps.get(1))
        .map(|m| m.as_str().to_string())
        .ok_or_else(|| fs_err!(ErrorCode::InvalidConfig, "Invalid test name: {}", input))
}

#[cfg(test)]
mod tests {
    use super::*;
    use dbt_schemas::schemas::data_tests::{CustomTestInner, CustomTestMultiKey};
    use serde_json::Value;
    use std::collections::{BTreeMap, HashMap};

    #[test]
    fn test_generic_test_asset_path_disambiguates_name_collisions() {
        // `not_null` on `orders.status_code` and on `orders_status.code` both flatten
        // to the same generated name; the second asset must not reuse the first path.

View on GitHub (pinned to 0267ce9170)

Solutions

  1. Restore the original regex literal `^([a-zA-Z_][0-9a-zA-Z_]*)+` in normalize_test_name
  2. Validate the pattern locally with `Regex::new(...)` in a unit test so CI catches an invalid literal before runtime
  3. If the panic reproduces on stock code, verify the `regex` crate dependency version and rebuild with `cargo build -p dbt-parser`

Example fix

// before
let name_pattern = Regex::new(r"^([a-zA-Z_][0-9a-zA-Z_]*)+").expect("Valid test name pattern");
// after (typo introduced by an edit — corrected)
let name_pattern = Regex::new(r"^([a-zA-Z_][0-9a-zA-Z_]*)+").expect("Valid test name pattern");
Defensive patterns

Strategy: validation

Validate before calling

// validate user input before calling normalize_test_name
fn is_valid_test_name(input: &str) -> bool {
    !input.is_empty() && input.chars().next().map_or(false, |c| c.is_ascii_alphabetic() || c == '_')
        && input.chars().all(|c| c.is_ascii_alphanumeric() || c == '_')
}

Type guard

fn is_valid_test_name(input: &str) -> bool {
    Regex::new(r"^[a-zA-Z_][0-9a-zA-Z_]*$").map(|re| re.is_match(input)).unwrap_or(false)
}

Prevention

When it happens

Trigger: Calling `normalize_test_name` (invoked when persisting generic data test names during test resolution) after the hardcoded regex literal in persist_generic_data_tests.rs has been modified to a syntactically invalid pattern, or (theoretically) a regex crate version that fails to compile this pattern.

Common situations: A developer edits the regex string constant and introduces a typo (unbalanced parenthesis, bad escape); upgrading the regex crate in a way that changes pattern syntax acceptance. Real users cannot cause this.

Understand the failure class

Background: "This is a bug, please report it": internal invariant violations, unreachable panics, and SNH errors explained — this error's family across 47 libraries.

Related errors


AI-assisted analysis of dbt-labs/dbt-core@0267ce9170 (2026-09-07). Data as JSON: /api/errors/481ee15bf0493d55. Report an issue: GitHub.