Skip to content

Commit

Permalink
fix(web-analytics): validate web analytics filters from url (#19967)
Browse files Browse the repository at this point in the history
* fix(web-analytics): validate web analytics filters from url

* Hide warning

* Import order
  • Loading branch information
robbie-c authored Jan 26, 2024
1 parent 52e81c7 commit e84c0ce
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
17 changes: 17 additions & 0 deletions frontend/src/queries/schema-guards.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import Ajv from 'ajv'

import { WebAnalyticsPropertyFilters } from '~/queries/schema'

import schema from './schema.json'
const ajv = new Ajv({
allowUnionTypes: true,
})
ajv.addSchema(schema)

export const isWebAnalyticsPropertyFilters = (data: unknown): data is WebAnalyticsPropertyFilters => {
const validator = ajv.getSchema('#/definitions/WebAnalyticsPropertyFilters')
if (!validator) {
throw new Error('Could not find validator for WebAnalyticsPropertyFilters')
}
return validator(data) as boolean
}
5 changes: 4 additions & 1 deletion frontend/src/scenes/web-analytics/webAnalyticsLogic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
WebAnalyticsPropertyFilters,
WebStatsBreakdown,
} from '~/queries/schema'
import { isWebAnalyticsPropertyFilters } from '~/queries/schema-guards'
import {
BaseMathType,
ChartDisplayType,
Expand Down Expand Up @@ -997,8 +998,10 @@ export const webAnalyticsLogic = kea<webAnalyticsLogicType>([
_,
{ filters, date_from, date_to, interval, device_tab, source_tab, graphs_tab, path_tab, geography_tab }
) => {
const parsedFilters = isWebAnalyticsPropertyFilters(filters) ? filters : initialWebAnalyticsFilter

actions.setStateFromUrl({
filters: filters || initialWebAnalyticsFilter,
filters: parsedFilters,
dateFrom: date_from || null,
dateTo: date_to || null,
interval: interval || null,
Expand Down

0 comments on commit e84c0ce

Please sign in to comment.