From 5a5be89573337f701e1115a2e7c8497608f917ad Mon Sep 17 00:00:00 2001 From: Dragory <2606411+Dragory@users.noreply.github.com> Date: Mon, 15 Jan 2024 22:38:48 +0000 Subject: [PATCH] fix: counter types --- backend/src/plugins/Counters/types.ts | 57 ++++++++++++++++----------- 1 file changed, 34 insertions(+), 23 deletions(-) diff --git a/backend/src/plugins/Counters/types.ts b/backend/src/plugins/Counters/types.ts index 2b8cce16..783ec666 100644 --- a/backend/src/plugins/Counters/types.ts +++ b/backend/src/plugins/Counters/types.ts @@ -10,7 +10,7 @@ const MAX_COUNTERS = 5; const MAX_TRIGGERS_PER_COUNTER = 5; export const zTrigger = z.strictObject({ - // Dummy type because name gets replaced by the property key in zTriggerInput + // Dummy type because name gets replaced by the property key in transform() name: z.never().optional().transform(() => ""), pretty_name: zBoundedCharacters(0, 100).nullable().default(null), condition: zBoundedCharacters(1, 64).refine( @@ -20,34 +20,45 @@ export const zTrigger = z.strictObject({ reverse_condition: zBoundedCharacters(1, 64).refine( (str) => parseCounterConditionString(str) !== null, { message: "Invalid counter trigger reverse condition" }, - ), -}); - -const zTriggerInput = z.union([zBoundedCharacters(0, 100), zTrigger]) + ).optional(), +}) .transform((val, ctx) => { const ruleName = String(ctx.path[ctx.path.length - 2]).trim(); - if (typeof val === "string") { - const parsedCondition = parseCounterConditionString(val); - if (!parsedCondition) { - ctx.addIssue({ - code: z.ZodIssueCode.custom, - message: "Invalid counter trigger condition", - }); - return z.NEVER; - } - return { - name: ruleName, - pretty_name: null, - condition: buildCounterConditionString(parsedCondition[0], parsedCondition[1]), - reverse_condition: buildCounterConditionString(getReverseCounterComparisonOp(parsedCondition[0]), parsedCondition[1]), - }; + + let reverseCondition = val.reverse_condition; + if (! reverseCondition) { + const parsedCondition = parseCounterConditionString(val.condition)!; + reverseCondition = buildCounterConditionString(getReverseCounterComparisonOp(parsedCondition[0]), parsedCondition[1]); } + return { ...val, name: ruleName, + reverse_condition: reverseCondition, }; }); +const zTriggerFromString = zBoundedCharacters(0, 100) + .transform((val, ctx) => { + const ruleName = String(ctx.path[ctx.path.length - 2]).trim(); + const parsedCondition = parseCounterConditionString(val); + if (!parsedCondition) { + ctx.addIssue({ + code: z.ZodIssueCode.custom, + message: "Invalid counter trigger condition", + }); + return z.NEVER; + } + return { + name: ruleName, + pretty_name: null, + condition: buildCounterConditionString(parsedCondition[0], parsedCondition[1]), + reverse_condition: buildCounterConditionString(getReverseCounterComparisonOp(parsedCondition[0]), parsedCondition[1]), + }; + }); + +const zTriggerInput = z.union([zTrigger, zTriggerFromString]); + export const zCounter = z.strictObject({ // Typed as "never" because you are not expected to supply this directly. // The transform instead picks it up from the property key and the output type is a string. @@ -78,9 +89,9 @@ export const zCounter = z.strictObject({ amount: z.number(), every: zDelayString, }).nullable().default(null), - can_view: z.boolean(), - can_edit: z.boolean(), - can_reset_all: z.boolean(), + can_view: z.boolean().default(false), + can_edit: z.boolean().default(false), + can_reset_all: z.boolean().default(false), }); export const zCountersConfig = z.strictObject({