Skip to content

OpenPanel is an open-source web and product analytics platform, an open-source alternative to Mixpanel with optional self-hosting.

fix: validate revenue to not be negative

Carl-Gerhard Lindesvärdc69ff7053a0d
diff --git a/packages/validation/src/track.validation.ts b/packages/validation/src/track.validation.ts
index 4ae685fbc..0a60b1c1b 100644
--- a/packages/validation/src/track.validation.ts
+++ b/packages/validation/src/track.validation.ts
@@ -1,7 +1,5 @@
-import { z } from 'zod';
-
import { RESERVED_EVENT_NAMES } from '@openpanel/constants';
-
+import { z } from 'zod';
import { isBlockedEventName } from './event-blocklist';
export const zTrackPayload = z
@@ -20,15 +18,23 @@ export const zTrackPayload = z
})
.refine(
(data) => {
- if (data.name !== 'revenue') return true;
+ if (data.name !== 'revenue') {
+ return true;
+ }
const revenue = data.properties?.__revenue;
- if (revenue === undefined) return true;
- return Number.isInteger(revenue);
+ if (revenue === undefined || revenue === null) {
+ return true;
+ }
+ const isInt = Number.isInteger(revenue);
+ if (isInt && Number(revenue) < 0) {
+ return false;
+ }
+ return isInt;
},
{
message: '__revenue must be an integer (no floats or strings)',
path: ['properties', '__revenue'],
- },
+ }
);
export const zIdentifyPayload = z.object({