Skip to content

Commit

Permalink
Make schemas for compound condition and sequential condition be lazy …
Browse files Browse the repository at this point in the history
…in the correct way.
  • Loading branch information
derekpierre committed Oct 3, 2024
1 parent 63b3f72 commit dbf54b2
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 37 deletions.
68 changes: 35 additions & 33 deletions packages/taco/src/conditions/schemas/compound.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<typeof compoundConditionSchema>;
10 changes: 6 additions & 4 deletions packages/taco/src/conditions/schemas/sequential.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<typeof conditionVariableSchema>;

export const sequentialConditionSchema: z.ZodSchema = baseConditionSchema
Expand Down

0 comments on commit dbf54b2

Please sign in to comment.