-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implementation of
IfThenElseCondition
(#593)
- Loading branch information
Showing
8 changed files
with
410 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { Condition } from './condition'; | ||
import { | ||
IfThenElseConditionProps, | ||
ifThenElseConditionSchema, | ||
IfThenElseConditionType, | ||
} from './schemas/if-then-else'; | ||
import { OmitConditionType } from './shared'; | ||
|
||
export { | ||
IfThenElseConditionProps, | ||
ifThenElseConditionSchema, | ||
IfThenElseConditionType, | ||
} from './schemas/if-then-else'; | ||
|
||
export class IfThenElseCondition extends Condition { | ||
constructor(value: OmitConditionType<IfThenElseConditionProps>) { | ||
super(ifThenElseConditionSchema, { | ||
conditionType: IfThenElseConditionType, | ||
...value, | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { z } from 'zod'; | ||
|
||
import { maxNestedDepth } from '../multi-condition'; | ||
|
||
import { baseConditionSchema } from './common'; | ||
import { anyConditionSchema } from './utils'; | ||
|
||
export const IfThenElseConditionType = 'if-then-else'; | ||
|
||
export const ifThenElseConditionSchema: z.ZodSchema = z.lazy(() => | ||
baseConditionSchema | ||
.extend({ | ||
conditionType: z | ||
.literal(IfThenElseConditionType) | ||
.default(IfThenElseConditionType), | ||
ifCondition: anyConditionSchema, | ||
thenCondition: anyConditionSchema, | ||
elseCondition: z.union([anyConditionSchema, z.boolean()]), | ||
}) | ||
.refine( | ||
// already at 2nd level since checking member condition | ||
(condition) => maxNestedDepth(2)(condition.ifCondition, 2), | ||
{ | ||
message: 'Exceeded max nested depth of 2 for multi-condition type', | ||
path: ['ifCondition'], | ||
}, // Max nested depth of 2 | ||
) | ||
.refine( | ||
// already at 2nd level since checking member condition | ||
(condition) => maxNestedDepth(2)(condition.thenCondition, 2), | ||
{ | ||
message: 'Exceeded max nested depth of 2 for multi-condition type', | ||
path: ['thenCondition'], | ||
}, // Max nested depth of 2 | ||
) | ||
.refine( | ||
(condition) => { | ||
if (typeof condition.elseCondition !== 'boolean') { | ||
// already at 2nd level since checking member condition | ||
return maxNestedDepth(2)(condition.elseCondition, 2); | ||
} | ||
return true; | ||
}, | ||
{ | ||
message: 'Exceeded max nested depth of 2 for multi-condition type', | ||
path: ['elseCondition'], | ||
}, // Max nested depth of 2 | ||
), | ||
); | ||
|
||
export type IfThenElseConditionProps = z.infer< | ||
typeof ifThenElseConditionSchema | ||
>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
176 changes: 176 additions & 0 deletions
176
packages/taco/test/conditions/if-then-else-condition.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,176 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
|
||
import { | ||
IfThenElseCondition, | ||
ifThenElseConditionSchema, | ||
IfThenElseConditionType, | ||
} from '../../src/conditions/if-then-else-condition'; | ||
import { TimeConditionType } from '../../src/conditions/schemas/time'; | ||
import { | ||
testCompoundConditionObj, | ||
testContractConditionObj, | ||
testRpcConditionObj, | ||
testSequentialConditionObj, | ||
testTimeConditionObj, | ||
} from '../test-utils'; | ||
|
||
describe('validation', () => { | ||
it('infers default condition type from constructor', () => { | ||
const condition = new IfThenElseCondition({ | ||
ifCondition: testRpcConditionObj, | ||
thenCondition: testTimeConditionObj, | ||
elseCondition: testContractConditionObj, | ||
}); | ||
expect(condition.value.conditionType).toEqual(IfThenElseConditionType); | ||
}); | ||
|
||
it('validates type', () => { | ||
const result = IfThenElseCondition.validate(ifThenElseConditionSchema, { | ||
conditionType: TimeConditionType, | ||
ifCondition: testRpcConditionObj, | ||
thenCondition: testTimeConditionObj, | ||
elseCondition: testContractConditionObj, | ||
}); | ||
expect(result.error).toBeDefined(); | ||
expect(result.data).toBeUndefined(); | ||
expect(result.error?.format()).toMatchObject({ | ||
conditionType: { | ||
_errors: [ | ||
`Invalid literal value, expected "${IfThenElseConditionType}"`, | ||
], | ||
}, | ||
}); | ||
}); | ||
|
||
it('accepts recursive if-then-else conditions', () => { | ||
const nestedIfThenElseConditionObj = { | ||
conditionType: IfThenElseConditionType, | ||
ifCondition: testRpcConditionObj, | ||
thenCondition: testTimeConditionObj, | ||
elseCondition: testContractConditionObj, | ||
}; | ||
|
||
const conditionObj = { | ||
conditionType: IfThenElseConditionType, | ||
ifCondition: testTimeConditionObj, | ||
thenCondition: nestedIfThenElseConditionObj, | ||
elseCondition: nestedIfThenElseConditionObj, | ||
}; | ||
|
||
const result = IfThenElseCondition.validate( | ||
ifThenElseConditionSchema, | ||
conditionObj, | ||
); | ||
expect(result.error).toBeUndefined(); | ||
expect(result.data).toEqual({ | ||
conditionType: IfThenElseConditionType, | ||
ifCondition: testTimeConditionObj, | ||
thenCondition: nestedIfThenElseConditionObj, | ||
elseCondition: nestedIfThenElseConditionObj, | ||
}); | ||
}); | ||
|
||
it('accepts nested sequential and compound conditions', () => { | ||
const conditionObj = { | ||
conditionType: IfThenElseConditionType, | ||
ifCondition: testRpcConditionObj, | ||
thenCondition: testSequentialConditionObj, | ||
elseCondition: testCompoundConditionObj, | ||
}; | ||
const result = IfThenElseCondition.validate( | ||
ifThenElseConditionSchema, | ||
conditionObj, | ||
); | ||
expect(result.error).toBeUndefined(); | ||
expect(result.data).toEqual({ | ||
conditionType: IfThenElseConditionType, | ||
ifCondition: testRpcConditionObj, | ||
thenCondition: testSequentialConditionObj, | ||
elseCondition: testCompoundConditionObj, | ||
}); | ||
}); | ||
|
||
it.each([true, false])( | ||
'accepts boolean for else condition', | ||
(booleanValue) => { | ||
const conditionObj = { | ||
conditionType: IfThenElseConditionType, | ||
ifCondition: testTimeConditionObj, | ||
thenCondition: testRpcConditionObj, | ||
elseCondition: booleanValue, | ||
}; | ||
|
||
const result = IfThenElseCondition.validate( | ||
ifThenElseConditionSchema, | ||
conditionObj, | ||
); | ||
expect(result.error).toBeUndefined(); | ||
expect(result.data).toEqual({ | ||
conditionType: IfThenElseConditionType, | ||
ifCondition: testTimeConditionObj, | ||
thenCondition: testRpcConditionObj, | ||
elseCondition: booleanValue, | ||
}); | ||
}, | ||
); | ||
|
||
it('limits max depth of nested if condition', () => { | ||
const result = IfThenElseCondition.validate(ifThenElseConditionSchema, { | ||
ifCondition: { | ||
conditionType: IfThenElseConditionType, | ||
ifCondition: testRpcConditionObj, | ||
thenCondition: testCompoundConditionObj, | ||
elseCondition: testTimeConditionObj, | ||
}, | ||
thenCondition: testRpcConditionObj, | ||
elseCondition: testTimeConditionObj, | ||
}); | ||
expect(result.error).toBeDefined(); | ||
expect(result.data).toBeUndefined(); | ||
expect(result.error?.format()).toMatchObject({ | ||
ifCondition: { | ||
_errors: [`Exceeded max nested depth of 2 for multi-condition type`], | ||
}, | ||
}); | ||
}); | ||
|
||
it('limits max depth of nested then condition', () => { | ||
const result = IfThenElseCondition.validate(ifThenElseConditionSchema, { | ||
ifCondition: testRpcConditionObj, | ||
thenCondition: { | ||
conditionType: IfThenElseConditionType, | ||
ifCondition: testRpcConditionObj, | ||
thenCondition: testCompoundConditionObj, | ||
elseCondition: true, | ||
}, | ||
elseCondition: testTimeConditionObj, | ||
}); | ||
expect(result.error).toBeDefined(); | ||
expect(result.data).toBeUndefined(); | ||
expect(result.error?.format()).toMatchObject({ | ||
thenCondition: { | ||
_errors: [`Exceeded max nested depth of 2 for multi-condition type`], | ||
}, | ||
}); | ||
}); | ||
|
||
it('limits max depth of nested else condition', () => { | ||
const result = IfThenElseCondition.validate(ifThenElseConditionSchema, { | ||
ifCondition: testRpcConditionObj, | ||
thenCondition: testTimeConditionObj, | ||
elseCondition: { | ||
conditionType: IfThenElseConditionType, | ||
ifCondition: testRpcConditionObj, | ||
thenCondition: testSequentialConditionObj, | ||
elseCondition: testTimeConditionObj, | ||
}, | ||
}); | ||
expect(result.error).toBeDefined(); | ||
expect(result.data).toBeUndefined(); | ||
expect(result.error?.format()).toMatchObject({ | ||
elseCondition: { | ||
_errors: [`Exceeded max nested depth of 2 for multi-condition type`], | ||
}, | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.