Skip to content

Commit

Permalink
Add sequential condition validation to ensure duplicate varNames are …
Browse files Browse the repository at this point in the history
…not provided for conditionVariables.
  • Loading branch information
derekpierre committed Sep 20, 2024
1 parent ef53f6c commit 3bd8816
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
19 changes: 19 additions & 0 deletions packages/taco/src/conditions/sequential.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,25 @@ export const sequentialConditionSchema: z.ZodSchema = baseConditionSchema
message: 'Exceeded max nested depth of 2 for multi-condition type',
path: ['conditionVariables'],
}, // Max nested depth of 2
)
.refine(
// check for duplicate var names
(condition) => {
const seen = new Set();
return condition.conditionVariables.every(
(child: ConditionVariableProps) => {
if (seen.has(child.varName)) {
return false;
}
seen.add(child.varName);
return true;
},
);
},
{
message: 'Duplicate variable names are not allowed',
path: ['conditionVariables'],
},
);

export type ConditionVariableProps = z.infer<typeof conditionVariableSchema>;
Expand Down
29 changes: 28 additions & 1 deletion packages/taco/test/conditions/sequential.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,33 @@ describe('validation', () => {
});
});

it('rejects duplication variable names', () => {
const result = SequentialCondition.validate(sequentialConditionSchema, {
conditionVariables: [
{
varName: 'var1',
condition: testRpcConditionObj,
},
{
varName: 'var2',
condition: testTimeConditionObj,
},
{
varName: 'var1',
condition: testContractConditionObj,
},
],
});

expect(result.error).toBeDefined();
expect(result.data).toBeUndefined();
expect(result.error?.format()).toMatchObject({
conditionVariables: {
_errors: ['Duplicate variable names are not allowed'],
},
});
});

it('rejects > max number of condition variables', () => {
const tooManyConditionVariables = new Array(6);
for (let i = 0; i < tooManyConditionVariables.length; i++) {
Expand All @@ -78,7 +105,7 @@ describe('validation', () => {
expect(result.data).toBeUndefined();
expect(result.error?.format()).toMatchObject({
conditionVariables: {
_errors: [`Array must contain at most 5 element(s)`],
_errors: ['Array must contain at most 5 element(s)'],
},
});
});
Expand Down

0 comments on commit 3bd8816

Please sign in to comment.