diff --git a/packages/taco/src/conditions/schemas/compound.ts b/packages/taco/src/conditions/schemas/compound.ts index d3b6a297..df8dd40c 100644 --- a/packages/taco/src/conditions/schemas/compound.ts +++ b/packages/taco/src/conditions/schemas/compound.ts @@ -7,40 +7,42 @@ import { anyConditionSchema } from './utils'; export const CompoundConditionType = 'compound'; -export const compoundConditionSchema: z.ZodSchema = baseConditionSchema - .extend({ - conditionType: z - .literal(CompoundConditionType) - .default(CompoundConditionType), - operator: z.enum(['and', 'or', 'not']), - operands: z.array(anyConditionSchema).min(1).max(5), - }) - .refine( - (condition) => { - // 'and' and 'or' operators must have at least 2 operands - if (['and', 'or'].includes(condition.operator)) { - return condition.operands.length >= 2; - } +export const compoundConditionSchema: z.ZodSchema = z.lazy(() => + baseConditionSchema + .extend({ + conditionType: z + .literal(CompoundConditionType) + .default(CompoundConditionType), + operator: z.enum(['and', 'or', 'not']), + operands: z.array(anyConditionSchema).min(1).max(5), + }) + .refine( + (condition) => { + // 'and' and 'or' operators must have at least 2 operands + if (['and', 'or'].includes(condition.operator)) { + return condition.operands.length >= 2; + } - // 'not' operator must have exactly 1 operand - if (condition.operator === 'not') { - return condition.operands.length === 1; - } + // 'not' operator must have exactly 1 operand + if (condition.operator === 'not') { + return condition.operands.length === 1; + } - // We test positive cases exhaustively, so we return false here: - return false; - }, - ({ operands, operator }) => ({ - message: `Invalid number of operands ${operands.length} for operator "${operator}"`, - path: ['operands'], - }), - ) - .refine( - (condition) => maxNestedDepth(2)(condition), - { - message: 'Exceeded max nested depth of 2 for multi-condition type', - path: ['operands'], - }, // Max nested depth of 2 - ); + // We test positive cases exhaustively, so we return false here: + return false; + }, + ({ operands, operator }) => ({ + message: `Invalid number of operands ${operands.length} for operator "${operator}"`, + path: ['operands'], + }), + ) + .refine( + (condition) => maxNestedDepth(2)(condition), + { + message: 'Exceeded max nested depth of 2 for multi-condition type', + path: ['operands'], + }, // Max nested depth of 2 + ), +); export type CompoundConditionProps = z.infer; diff --git a/packages/taco/src/conditions/schemas/sequential.ts b/packages/taco/src/conditions/schemas/sequential.ts index 5b11de9b..60942d04 100644 --- a/packages/taco/src/conditions/schemas/sequential.ts +++ b/packages/taco/src/conditions/schemas/sequential.ts @@ -7,10 +7,12 @@ import { anyConditionSchema } from './utils'; export const SequentialConditionType = 'sequential'; -export const conditionVariableSchema: z.ZodSchema = z.object({ - varName: plainStringSchema, - condition: anyConditionSchema, -}); +export const conditionVariableSchema: z.ZodSchema = z.lazy(() => + z.object({ + varName: plainStringSchema, + condition: anyConditionSchema, + }), +); export type ConditionVariableProps = z.infer; export const sequentialConditionSchema: z.ZodSchema = baseConditionSchema