Skip to content

Commit

Permalink
Use baseConditionSchema that all sub-types of conditions extend their…
Browse files Browse the repository at this point in the history
… schema from.
  • Loading branch information
derekpierre committed Sep 19, 2024
1 parent dd236ea commit d3ddf13
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 14 deletions.
4 changes: 2 additions & 2 deletions packages/taco/src/conditions/base/rpc.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { z } from 'zod';

import { Condition } from '../condition';
import { baseConditionSchema, Condition } from '../condition';
import { SUPPORTED_CHAIN_IDS } from '../const';
import {
EthAddressOrUserAddressSchema,
Expand All @@ -12,7 +12,7 @@ import createUnionSchema from '../zod';

export const RpcConditionType = 'rpc';

export const rpcConditionSchema = z.object({
export const rpcConditionSchema = baseConditionSchema.extend({
conditionType: z.literal(RpcConditionType).default(RpcConditionType),
chain: createUnionSchema(SUPPORTED_CHAIN_IDS),
method: z.enum(['eth_getBalance']),
Expand Down
8 changes: 4 additions & 4 deletions packages/taco/src/conditions/compound-condition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ import { z } from 'zod';
import { contractConditionSchema } from './base/contract';
import { rpcConditionSchema } from './base/rpc';
import { timeConditionSchema } from './base/time';
import { Condition, ConditionProps } from './condition';
import { baseConditionSchema, Condition, ConditionProps } from './condition';
import { sequentialConditionSchema } from './sequential';
import { OmitConditionType } from './shared';

export const CompoundConditionType = 'compound';

export const compoundConditionSchema: z.ZodSchema = z
.object({
export const compoundConditionSchema: z.ZodSchema = baseConditionSchema
.extend({
conditionType: z
.literal(CompoundConditionType)
.default(CompoundConditionType),
Expand All @@ -29,7 +29,7 @@ export const compoundConditionSchema: z.ZodSchema = z
)
.min(1)
.max(5),
// TODO nesting validation
// TODO nesting validation
})
.refine(
(condition) => {
Expand Down
4 changes: 4 additions & 0 deletions packages/taco/src/conditions/condition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import { z } from 'zod';

import { USER_ADDRESS_PARAMS } from './const';

export const baseConditionSchema = z.object({
conditionType: z.string(),
});

type ConditionSchema = z.ZodSchema;
export type ConditionProps = z.infer<ConditionSchema>;

Expand Down
17 changes: 9 additions & 8 deletions packages/taco/src/conditions/sequential.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { contractConditionSchema } from './base/contract';
import { rpcConditionSchema } from './base/rpc';
import { timeConditionSchema } from './base/time';
import { compoundConditionSchema } from './compound-condition';
import { Condition } from './condition';
import { baseConditionSchema, Condition } from './condition';
import { OmitConditionType, plainStringSchema } from './shared';

export const SequentialConditionType = 'sequential';
Expand All @@ -22,13 +22,14 @@ export const conditionVariableSchema: z.ZodSchema = z.object({
),
});

export const sequentialConditionSchema: z.ZodSchema = z.object({
conditionType: z
.literal(SequentialConditionType)
.default(SequentialConditionType),
conditionVariables: z.array(conditionVariableSchema).min(2).max(5),
// TODO nesting validation
});
export const sequentialConditionSchema: z.ZodSchema =
baseConditionSchema.extend({
conditionType: z
.literal(SequentialConditionType)
.default(SequentialConditionType),
conditionVariables: z.array(conditionVariableSchema).min(2).max(5),
// TODO nesting validation
});

export type SequentialConditionProps = z.infer<
typeof sequentialConditionSchema
Expand Down

0 comments on commit d3ddf13

Please sign in to comment.