mirror of
https://github.com/umami-software/umami.git
synced 2026-02-04 04:37:11 +01:00
tighten zod validation for segment parameters
Some checks are pending
Node.js CI / build (push) Waiting to run
Some checks are pending
Node.js CI / build (push) Waiting to run
This commit is contained in:
parent
6fd428683d
commit
d4ff7c8e3f
2 changed files with 38 additions and 2 deletions
|
|
@ -2,7 +2,7 @@ import { z } from 'zod';
|
||||||
import { uuid } from '@/lib/crypto';
|
import { uuid } from '@/lib/crypto';
|
||||||
import { getQueryFilters, parseRequest } from '@/lib/request';
|
import { getQueryFilters, parseRequest } from '@/lib/request';
|
||||||
import { json, unauthorized } from '@/lib/response';
|
import { json, unauthorized } from '@/lib/response';
|
||||||
import { anyObjectParam, searchParams, segmentTypeParam } from '@/lib/schema';
|
import { searchParams, segmentParametersSchema, segmentTypeParam } from '@/lib/schema';
|
||||||
import { canUpdateWebsite, canViewWebsite } from '@/permissions';
|
import { canUpdateWebsite, canViewWebsite } from '@/permissions';
|
||||||
import { createSegment, getWebsiteSegments } from '@/queries/prisma';
|
import { createSegment, getWebsiteSegments } from '@/queries/prisma';
|
||||||
|
|
||||||
|
|
@ -42,7 +42,7 @@ export async function POST(
|
||||||
const schema = z.object({
|
const schema = z.object({
|
||||||
type: segmentTypeParam,
|
type: segmentTypeParam,
|
||||||
name: z.string().max(200),
|
name: z.string().max(200),
|
||||||
parameters: anyObjectParam,
|
parameters: segmentParametersSchema,
|
||||||
});
|
});
|
||||||
|
|
||||||
const { auth, body, error } = await parseRequest(request, schema);
|
const { auth, body, error } = await parseRequest(request, schema);
|
||||||
|
|
|
||||||
|
|
@ -104,6 +104,23 @@ export const reportTypeParam = z.enum([
|
||||||
'utm',
|
'utm',
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
export const operatorParam = z.enum([
|
||||||
|
'eq',
|
||||||
|
'neq',
|
||||||
|
's',
|
||||||
|
'ns',
|
||||||
|
'c',
|
||||||
|
'dnc',
|
||||||
|
't',
|
||||||
|
'f',
|
||||||
|
'gt',
|
||||||
|
'lt',
|
||||||
|
'gte',
|
||||||
|
'lte',
|
||||||
|
'bf',
|
||||||
|
'af',
|
||||||
|
]);
|
||||||
|
|
||||||
export const goalReportSchema = z.object({
|
export const goalReportSchema = z.object({
|
||||||
type: z.literal('goal'),
|
type: z.literal('goal'),
|
||||||
parameters: z
|
parameters: z
|
||||||
|
|
@ -231,3 +248,22 @@ export const reportResultSchema = z.intersection(
|
||||||
);
|
);
|
||||||
|
|
||||||
export const segmentTypeParam = z.enum(['segment', 'cohort']);
|
export const segmentTypeParam = z.enum(['segment', 'cohort']);
|
||||||
|
|
||||||
|
export const segmentParametersSchema = z.object({
|
||||||
|
filters: z
|
||||||
|
.array(
|
||||||
|
z.object({
|
||||||
|
name: z.string(),
|
||||||
|
operator: operatorParam,
|
||||||
|
value: z.string(),
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
.optional(),
|
||||||
|
dateRange: z.string().optional(),
|
||||||
|
action: z
|
||||||
|
.object({
|
||||||
|
type: z.string(),
|
||||||
|
value: z.string(),
|
||||||
|
})
|
||||||
|
.optional(),
|
||||||
|
});
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue