2
0

♻️ Replace schemas with merge and discriminated unions

Closes #374
This commit is contained in:
Baptiste Arnaud
2023-03-14 16:42:12 +01:00
parent ff09814ead
commit d154c4e2f2
52 changed files with 3217 additions and 4328 deletions

View File

@ -30,14 +30,14 @@ const conditionContentSchema = z.object({
comparisons: z.array(comparisonSchema),
})
export const conditionItemSchema = itemBaseSchema.and(
export const conditionItemSchema = itemBaseSchema.merge(
z.object({
type: z.literal(ItemType.CONDITION),
content: conditionContentSchema,
})
)
export const conditionBlockSchema = blockBaseSchema.and(
export const conditionBlockSchema = blockBaseSchema.merge(
z.object({
type: z.enum([LogicBlockType.CONDITION]),
items: z.array(conditionItemSchema),

View File

@ -1,7 +1,6 @@
export * from './script'
export * from './condition'
export * from './enums'
export * from './logicBlock'
export * from './redirect'
export * from './setVariable'
export * from './typebotLink'

View File

@ -7,7 +7,7 @@ export const jumpOptionsSchema = z.object({
blockId: z.string().optional(),
})
export const jumpBlockSchema = blockBaseSchema.and(
export const jumpBlockSchema = blockBaseSchema.merge(
z.object({
type: z.enum([LogicBlockType.JUMP]),
options: jumpOptionsSchema,

View File

@ -1,26 +0,0 @@
import { z } from 'zod'
import { scriptOptionsSchema, scriptBlockSchema } from './script'
import { conditionBlockSchema } from './condition'
import { redirectOptionsSchema, redirectBlockSchema } from './redirect'
import { setVariableOptionsSchema, setVariableBlockSchema } from './setVariable'
import { typebotLinkOptionsSchema, typebotLinkBlockSchema } from './typebotLink'
import { waitBlockSchema, waitOptionsSchema } from './wait'
import { jumpBlockSchema, jumpOptionsSchema } from './jump'
const logicBlockOptionsSchema = scriptOptionsSchema
.or(redirectOptionsSchema)
.or(setVariableOptionsSchema)
.or(typebotLinkOptionsSchema)
.or(waitOptionsSchema)
.or(jumpOptionsSchema)
export const logicBlockSchema = scriptBlockSchema
.or(conditionBlockSchema)
.or(redirectBlockSchema)
.or(typebotLinkBlockSchema)
.or(setVariableBlockSchema)
.or(waitBlockSchema)
.or(jumpBlockSchema)
export type LogicBlock = z.infer<typeof logicBlockSchema>
export type LogicBlockOptions = z.infer<typeof logicBlockOptionsSchema>

View File

@ -7,7 +7,7 @@ export const redirectOptionsSchema = z.object({
isNewTab: z.boolean(),
})
export const redirectBlockSchema = blockBaseSchema.and(
export const redirectBlockSchema = blockBaseSchema.merge(
z.object({
type: z.enum([LogicBlockType.REDIRECT]),
options: redirectOptionsSchema,

View File

@ -8,7 +8,7 @@ export const scriptOptionsSchema = z.object({
shouldExecuteInParentContext: z.boolean().optional(),
})
export const scriptBlockSchema = blockBaseSchema.and(
export const scriptBlockSchema = blockBaseSchema.merge(
z.object({
type: z.enum([LogicBlockType.SCRIPT]),
options: scriptOptionsSchema,

View File

@ -8,7 +8,7 @@ export const setVariableOptionsSchema = z.object({
isCode: z.boolean().optional(),
})
export const setVariableBlockSchema = blockBaseSchema.and(
export const setVariableBlockSchema = blockBaseSchema.merge(
z.object({
type: z.enum([LogicBlockType.SET_VARIABLE]),
options: setVariableOptionsSchema,

View File

@ -7,7 +7,7 @@ export const typebotLinkOptionsSchema = z.object({
groupId: z.string().optional(),
})
export const typebotLinkBlockSchema = blockBaseSchema.and(
export const typebotLinkBlockSchema = blockBaseSchema.merge(
z.object({
type: z.enum([LogicBlockType.TYPEBOT_LINK]),
options: typebotLinkOptionsSchema,

View File

@ -6,7 +6,7 @@ export const waitOptionsSchema = z.object({
secondsToWaitFor: z.string().optional(),
})
export const waitBlockSchema = blockBaseSchema.and(
export const waitBlockSchema = blockBaseSchema.merge(
z.object({
type: z.enum([LogicBlockType.WAIT]),
options: waitOptionsSchema,