From 468af786edd2605c6331eafb90075a2f35addc27 Mon Sep 17 00:00:00 2001 From: Christian Fehmer Date: Tue, 27 Aug 2024 14:05:48 +0300 Subject: [PATCH] refactor: remove ResultOmittableDefaultPropertiesSchema (@fehmer) --- packages/contracts/src/schemas/results.ts | 51 +++++++++++++---------- 1 file changed, 29 insertions(+), 22 deletions(-) diff --git a/packages/contracts/src/schemas/results.ts b/packages/contracts/src/schemas/results.ts index c784f5da9..41891b53c 100644 --- a/packages/contracts/src/schemas/results.ts +++ b/packages/contracts/src/schemas/results.ts @@ -63,27 +63,23 @@ const ResultBaseSchema = z.object({ keyConsistency: PercentageSchema, chartData: ChartDataSchema.or(z.literal("toolong")), uid: IdSchema, + + //required on POST but optional in the database and might be removed to save space + restartCount: z.number().int().nonnegative().optional(), + incompleteTestSeconds: z.number().nonnegative().optional(), + afkDuration: z.number().nonnegative().optional(), + tags: z.array(IdSchema).optional(), + bailedOut: z.boolean().optional(), + blindMode: z.boolean().optional(), + lazyMode: z.boolean().optional(), + funbox: FunboxSchema.optional(), + language: LanguageSchema.optional(), + difficulty: DifficultySchema.optional(), + numbers: z.boolean().optional(), + punctuation: z.boolean().optional(), }); -//required on POST but optional in the database and might be removed to save space -const ResultOmittableDefaultPropertiesSchema = z.object({ - restartCount: z.number().int().nonnegative(), - incompleteTestSeconds: z.number().nonnegative(), - afkDuration: z.number().nonnegative(), - tags: z.array(IdSchema), - bailedOut: z.boolean(), - blindMode: z.boolean(), - lazyMode: z.boolean(), - funbox: FunboxSchema, - language: LanguageSchema, - difficulty: DifficultySchema, - numbers: z.boolean(), - punctuation: z.boolean(), -}); - -export const ResultSchema = ResultBaseSchema.merge( - ResultOmittableDefaultPropertiesSchema.partial() //missing on GET if the values are the default values -).extend({ +export const ResultSchema = ResultBaseSchema.extend({ _id: IdSchema, keySpacingStats: KeyStatsSchema.optional(), keyDurationStats: KeyStatsSchema.optional(), @@ -99,9 +95,20 @@ export type Result = Omit< mode2: Mode2; }; -export const CompletedEventSchema = ResultBaseSchema.merge( - ResultOmittableDefaultPropertiesSchema //mandatory on POST -) +export const CompletedEventSchema = ResultBaseSchema.required({ + restartCount: true, + incompleteTestSeconds: true, + afkDuration: true, + tags: true, + bailedOut: true, + blindMode: true, + lazyMode: true, + funbox: true, + language: true, + difficulty: true, + numbers: true, + punctuation: true, +}) .extend({ charTotal: z.number().int().nonnegative(), challenge: token().max(100).optional(),