fix: validate revenue payload (must be int)
This commit is contained in:
@@ -5,13 +5,25 @@ import { RESERVED_EVENT_NAMES } from '@openpanel/constants';
|
|||||||
export const zTrackPayload = z
|
export const zTrackPayload = z
|
||||||
.object({
|
.object({
|
||||||
name: z.string().min(1),
|
name: z.string().min(1),
|
||||||
properties: z.record(z.unknown()).optional(),
|
properties: z.record(z.string(), z.unknown()).optional(),
|
||||||
profileId: z.string().or(z.number()).optional(),
|
profileId: z.string().or(z.number()).optional(),
|
||||||
})
|
})
|
||||||
.refine((data) => !RESERVED_EVENT_NAMES.includes(data.name as any), {
|
.refine((data) => !RESERVED_EVENT_NAMES.includes(data.name as any), {
|
||||||
message: `Event name cannot be one of the reserved names: ${RESERVED_EVENT_NAMES.join(', ')}`,
|
message: `Event name cannot be one of the reserved names: ${RESERVED_EVENT_NAMES.join(', ')}`,
|
||||||
path: ['name'],
|
path: ['name'],
|
||||||
});
|
})
|
||||||
|
.refine(
|
||||||
|
(data) => {
|
||||||
|
if (data.name !== 'revenue') return true;
|
||||||
|
const revenue = data.properties?.__revenue;
|
||||||
|
if (revenue === undefined) return true;
|
||||||
|
return Number.isInteger(revenue);
|
||||||
|
},
|
||||||
|
{
|
||||||
|
message: '__revenue must be an integer (no floats or strings)',
|
||||||
|
path: ['properties', '__revenue'],
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
export const zIdentifyPayload = z.object({
|
export const zIdentifyPayload = z.object({
|
||||||
profileId: z.string().min(1),
|
profileId: z.string().min(1),
|
||||||
|
|||||||
Reference in New Issue
Block a user