-
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.
Merge
epic-sequential-condition
(#590)
- Loading branch information
Showing
9 changed files
with
565 additions
and
7 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
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,30 @@ | ||
import { CompoundConditionType } from './compound-condition'; | ||
import { ConditionProps } from './condition'; | ||
import { ConditionVariableProps, SequentialConditionType } from './sequential'; | ||
|
||
export const maxNestedDepth = | ||
(maxDepth: number) => | ||
(condition: ConditionProps, currentDepth = 1) => { | ||
if ( | ||
condition.conditionType === CompoundConditionType || | ||
condition.conditionType === SequentialConditionType | ||
) { | ||
if (currentDepth > maxDepth) { | ||
// no more multi-condition types allowed at this level | ||
return false; | ||
} | ||
|
||
if (condition.conditionType === CompoundConditionType) { | ||
return condition.operands.every((child: ConditionProps) => | ||
maxNestedDepth(maxDepth)(child, currentDepth + 1), | ||
); | ||
} else { | ||
return condition.conditionVariables.every( | ||
(child: ConditionVariableProps) => | ||
maxNestedDepth(maxDepth)(child.condition, currentDepth + 1), | ||
); | ||
} | ||
} | ||
|
||
return true; | ||
}; |
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,73 @@ | ||
import { z } from 'zod'; | ||
|
||
import { contractConditionSchema } from './base/contract'; | ||
import { rpcConditionSchema } from './base/rpc'; | ||
import { timeConditionSchema } from './base/time'; | ||
import { compoundConditionSchema } from './compound-condition'; | ||
import { baseConditionSchema, Condition } from './condition'; | ||
import { maxNestedDepth } from './multi-condition'; | ||
import { OmitConditionType, plainStringSchema } from './shared'; | ||
|
||
export const SequentialConditionType = 'sequential'; | ||
|
||
export const conditionVariableSchema: z.ZodSchema = z.object({ | ||
varName: plainStringSchema, | ||
condition: z.lazy(() => | ||
z.union([ | ||
rpcConditionSchema, | ||
timeConditionSchema, | ||
contractConditionSchema, | ||
compoundConditionSchema, | ||
sequentialConditionSchema, | ||
]), | ||
), | ||
}); | ||
|
||
export const sequentialConditionSchema: z.ZodSchema = baseConditionSchema | ||
.extend({ | ||
conditionType: z | ||
.literal(SequentialConditionType) | ||
.default(SequentialConditionType), | ||
conditionVariables: z.array(conditionVariableSchema).min(2).max(5), | ||
}) | ||
.refine( | ||
(condition) => maxNestedDepth(2)(condition), | ||
{ | ||
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>; | ||
|
||
export type SequentialConditionProps = z.infer< | ||
typeof sequentialConditionSchema | ||
>; | ||
|
||
export class SequentialCondition extends Condition { | ||
constructor(value: OmitConditionType<SequentialConditionProps>) { | ||
super(sequentialConditionSchema, { | ||
conditionType: SequentialConditionType, | ||
...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
Oops, something went wrong.