Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(cohorts): avoid crash when validating negated criteria with unknown type (#59972)

posthog[bot]8b545144943f
diff --git a/frontend/src/scenes/cohorts/cohortUtils.test.tsx b/frontend/src/scenes/cohorts/cohortUtils.test.tsx
new file mode 100644
index 000000000000..6cce580f9cb1
--- /dev/null
+++ b/frontend/src/scenes/cohorts/cohortUtils.test.tsx
@@ -0,0 +1,89 @@
+import { BehavioralFilterKey } from 'scenes/cohorts/CohortFilters/types'
+import { validateGroup } from 'scenes/cohorts/cohortUtils'
+
+import { AnyCohortCriteriaType, BehavioralEventType, CohortCriteriaGroupFilter, FilterLogicalOperator } from '~/types'
+
+describe('validateGroup', () => {
+ function groupWithNegatedCriteria(criteria: AnyCohortCriteriaType[]): CohortCriteriaGroupFilter {
+ // OR group + any negated criterion triggers the "negation requires AND" error path,
+ // which is the branch that builds the human-readable error from BEHAVIORAL_TYPE_TO_LABEL.
+ return {
+ type: FilterLogicalOperator.Or,
+ values: criteria,
+ }
+ }
+
+ it('falls back to the raw filter type when the label map has no matching entry', () => {
+ const group = groupWithNegatedCriteria([
+ {
+ type: BehavioralFilterKey.Behavioral,
+ // A legacy/unknown value not present in BEHAVIORAL_TYPE_TO_LABEL.
+ value: 'legacy_unknown_type' as BehavioralEventType,
+ negation: true,
+ },
+ ])
+
+ const errors = validateGroup(group)
+
+ expect(typeof errors.id).toBe('string')
+ expect(errors.id).toContain("'legacy_unknown_type'")
+ expect(errors.id).not.toContain('undefined')
+ })
+
+ it('uses the mapped label for a known behavioral filter type', () => {
+ const group = groupWithNegatedCriteria([
+ {
+ type: BehavioralFilterKey.Behavioral,
+ // Negated PerformEvent resolves to NotPerformedEvent ("Did not complete event").
+ value: BehavioralEventType.PerformEvent,
+ negation: true,
+ },
+ ])
+
+ const errors = validateGroup(group)
+
+ expect(errors.id).toContain("'Did not complete event'")
+ })
+
+ it('does not crash when an AND group has one negated unknown-type criterion and one positive criterion', () => {
+ // AND group with partial negation reaches the cancel-out check, which calls cleanCriteria on each
+ // criterion. Before the fix, ROWS[unknownType] was undefined and destructuring it threw.
+ const group: CohortCriteriaGroupFilter = {
+ type: FilterLogicalOperator.And,
+ values: [
+ {
+ type: BehavioralFilterKey.Behavioral,
+ value: 'legacy_unknown_type' as BehavioralEventType,
+ negation: true,
+ },
+ {
+ type: BehavioralFilterKey.Behavioral,
+ value: BehavioralEventType.PerformEvent,
+ negation: false,
+ },
+ ],
+ }
+
+ expect(() => validateGroup(group)).not.toThrow()
+ })
+
+ it('joins multiple negated criteria with a pluralized message', () => {
+ const group = groupWithNegatedCriteria([
+ {
+ type: BehavioralFilterKey.Behavioral,
+ value: 'legacy_unknown_type' as BehavioralEventType,
+ negation: true,
+ },
+ {
+ type: BehavioralFilterKey.Behavioral,
+ value: BehavioralEventType.PerformEvent,
+ negation: true,
+ },
+ ])
+
+ const errors = validateGroup(group)
+
+ // Exercises the `.join(', ')` and the `are` (vs `is a`) pluralization in the changed expression.
+ expect(errors.id).toContain("'legacy_unknown_type', 'Did not complete event' are negative cohort criteria")
+ })
+})
diff --git a/frontend/src/scenes/cohorts/cohortUtils.tsx b/frontend/src/scenes/cohorts/cohortUtils.tsx
index da93fc461a41..883970b54868 100644
--- a/frontend/src/scenes/cohorts/cohortUtils.tsx
+++ b/frontend/src/scenes/cohorts/cohortUtils.tsx
@@ -174,7 +174,13 @@ export function validateGroup(
(group.type === FilterLogicalOperator.And && negatedCriteria.length === criteria.length)
) {
const errorMsg = `${negatedCriteria
- .map((c) => `'${BEHAVIORAL_TYPE_TO_LABEL[criteriaToBehavioralFilterType(c)]!.label}'`)
+ .map((c) => {
+ const behavioralFilterType = criteriaToBehavioralFilterType(c)
+ // Fall back to the raw filter type when the label map has no entry: this surfaces which
+ // BehavioralFilterType is missing from BEHAVIORAL_TYPE_TO_LABEL (e.g. a new enum value that
+ // landed before the map was updated) rather than crashing on an undefined `.label`.
+ return `'${BEHAVIORAL_TYPE_TO_LABEL[behavioralFilterType]?.label ?? behavioralFilterType}'`
+ })
.join(', ')} ${negatedCriteria.length > 1 ? 'are' : 'is a'} negative cohort criteria. ${
CohortClientErrors.NegationCriteriaMissingOther
}`
@@ -545,7 +551,7 @@ function getCriteriaValue(criteria: AnyCohortCriteriaType, key: string): any {
// Populate empty values with default values on changing type, pruning any extra variables
export function cleanCriteria(criteria: AnyCohortCriteriaType, shouldPurge: boolean = false): AnyCohortCriteriaType {
const populatedCriteria: Record<string, any> = {}
- const { fields, ...apiProps } = ROWS[criteriaToBehavioralFilterType(criteria)]
+ const { fields, ...apiProps } = ROWS[criteriaToBehavioralFilterType(criteria)] ?? { fields: [] }
Object.entries(apiProps).forEach(([key, defaultValue]) => {
const nextValue = getCriteriaValue(criteria, key) ?? defaultValue
if (shouldPurge) {