fix: counter types
This commit is contained in:
parent
82d720d308
commit
5a5be89573
1 changed files with 34 additions and 23 deletions
|
@ -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,13 +20,27 @@ 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();
|
||||
|
||||
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();
|
||||
if (typeof val === "string") {
|
||||
const parsedCondition = parseCounterConditionString(val);
|
||||
if (!parsedCondition) {
|
||||
ctx.addIssue({
|
||||
|
@ -41,13 +55,10 @@ const zTriggerInput = z.union([zBoundedCharacters(0, 100), zTrigger])
|
|||
condition: buildCounterConditionString(parsedCondition[0], parsedCondition[1]),
|
||||
reverse_condition: buildCounterConditionString(getReverseCounterComparisonOp(parsedCondition[0]), parsedCondition[1]),
|
||||
};
|
||||
}
|
||||
return {
|
||||
...val,
|
||||
name: ruleName,
|
||||
};
|
||||
});
|
||||
|
||||
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({
|
||||
|
|
Loading…
Add table
Reference in a new issue