From f12d3cf4febb10a320f9442cbe1781bb35318d42 Mon Sep 17 00:00:00 2001 From: Hide_D Date: Thu, 15 Jan 2026 15:01:58 +0000 Subject: [PATCH] =?UTF-8?q?zod=20arg=20parse=20=EC=9E=91=EC=97=85=20?= =?UTF-8?q?=EC=A7=84=ED=96=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/actions/turn/general/che_NPC능동.ts | 21 ++++++++----------- .../src/actions/turn/general/che_강행.ts | 9 +++++--- .../src/actions/turn/general/che_이동.ts | 9 +++++--- packages/logic/src/actions/turn/parseArgs.ts | 6 ++++++ 4 files changed, 27 insertions(+), 18 deletions(-) create mode 100644 packages/logic/src/actions/turn/parseArgs.ts diff --git a/packages/logic/src/actions/turn/general/che_NPC능동.ts b/packages/logic/src/actions/turn/general/che_NPC능동.ts index f77b798..b5d75b3 100644 --- a/packages/logic/src/actions/turn/general/che_NPC능동.ts +++ b/packages/logic/src/actions/turn/general/che_NPC능동.ts @@ -11,9 +11,11 @@ import type { import { createGeneralPatchEffect } from '@sammo-ts/logic/actions/engine.js'; import { LogCategory, LogFormat } from '@sammo-ts/logic/logging/types.js'; import { JosaUtil } from '@sammo-ts/common'; +import { z } from 'zod'; import type { TurnCommandEnv } from '@sammo-ts/logic/actions/turn/commandEnv.js'; import type { GeneralTurnCommandSpec } from './index.js'; import type { MapDefinition } from '@sammo-ts/logic/world/types.js'; +import { parseArgsWithSchema } from '../parseArgs.js'; export interface NPCSelfArgs { optionText: string; @@ -27,6 +29,12 @@ export type NPCSelfResolveContext; - if (!data.optionText) return null; - - if (data.optionText === '순간이동') { - if (typeof data.destCityId !== 'number') return null; - // We can check city existence here deeply but constraint is better place usually, - // simplified parse just structural. - return { optionText: data.optionText, destCityId: data.destCityId }; - } - - // Only '순간이동' implemented in legacy file shown. - return null; + return parseArgsWithSchema(NPC_SELF_ARGS_SCHEMA, raw); } buildConstraints(_ctx: ConstraintContext, args: NPCSelfArgs): Constraint[] { diff --git a/packages/logic/src/actions/turn/general/che_강행.ts b/packages/logic/src/actions/turn/general/che_강행.ts index 6f3bd40..fcbd7ec 100644 --- a/packages/logic/src/actions/turn/general/che_강행.ts +++ b/packages/logic/src/actions/turn/general/che_강행.ts @@ -17,10 +17,12 @@ import type { import { createGeneralPatchEffect } from '@sammo-ts/logic/actions/engine.js'; import { LogCategory, LogFormat } from '@sammo-ts/logic/logging/types.js'; import { JosaUtil } from '@sammo-ts/common'; +import { z } from 'zod'; import type { TurnCommandEnv } from '@sammo-ts/logic/actions/turn/commandEnv.js'; import type { GeneralTurnCommandSpec } from './index.js'; import type { MapDefinition } from '@sammo-ts/logic/world/types.js'; import type { ActionContextBuilder } from '@sammo-ts/logic/actions/turn/actionContext.js'; +import { parseArgsWithSchema } from '../parseArgs.js'; export interface ForcedMoveArgs { destCityId: number; @@ -36,6 +38,9 @@ export interface ForcedMoveResolveContext< const ACTION_NAME = '강행'; const ACTION_KEY = 'che_강행'; +const FORCED_MOVE_ARGS_SCHEMA = z.object({ + destCityId: z.number(), +}); export class ActionResolver< TriggerState extends GeneralTriggerState = GeneralTriggerState, @@ -159,9 +164,7 @@ export class ActionDefinition< } parseArgs(raw: unknown): ForcedMoveArgs | null { - const data = raw as Partial; - if (typeof data.destCityId !== 'number') return null; - return { destCityId: data.destCityId }; + return parseArgsWithSchema(FORCED_MOVE_ARGS_SCHEMA, raw); } buildConstraints(ctx: ConstraintContext, _args: ForcedMoveArgs): Constraint[] { diff --git a/packages/logic/src/actions/turn/general/che_이동.ts b/packages/logic/src/actions/turn/general/che_이동.ts index fabd2b4..7dee991 100644 --- a/packages/logic/src/actions/turn/general/che_이동.ts +++ b/packages/logic/src/actions/turn/general/che_이동.ts @@ -17,10 +17,12 @@ import type { import { createGeneralPatchEffect } from '@sammo-ts/logic/actions/engine.js'; import { LogCategory, LogFormat } from '@sammo-ts/logic/logging/types.js'; import { JosaUtil } from '@sammo-ts/common'; +import { z } from 'zod'; import type { TurnCommandEnv } from '@sammo-ts/logic/actions/turn/commandEnv.js'; import type { ActionContextBuilder } from '@sammo-ts/logic/actions/turn/actionContext.js'; import type { GeneralTurnCommandSpec } from './index.js'; import type { MapDefinition } from '@sammo-ts/logic/world/types.js'; +import { parseArgsWithSchema } from '../parseArgs.js'; export interface MoveArgs { destCityId: number; @@ -36,6 +38,9 @@ export interface MoveResolveContext< const ACTION_NAME = '이동'; const ACTION_KEY = 'che_이동'; +const MOVE_ARGS_SCHEMA = z.object({ + destCityId: z.number(), +}); // Helper for map connectivity const connectedCity = (): Constraint => ({ @@ -155,9 +160,7 @@ export class ActionDefinition< } parseArgs(raw: unknown): MoveArgs | null { - const data = raw as Partial; - if (typeof data.destCityId !== 'number') return null; - return { destCityId: data.destCityId }; + return parseArgsWithSchema(MOVE_ARGS_SCHEMA, raw); } buildConstraints(ctx: ConstraintContext, _args: MoveArgs): Constraint[] { diff --git a/packages/logic/src/actions/turn/parseArgs.ts b/packages/logic/src/actions/turn/parseArgs.ts new file mode 100644 index 0000000..6f59c1d --- /dev/null +++ b/packages/logic/src/actions/turn/parseArgs.ts @@ -0,0 +1,6 @@ +import type { ZodType } from 'zod'; + +export const parseArgsWithSchema = (schema: ZodType, raw: unknown): T | null => { + const result = schema.safeParse(raw); + return result.success ? result.data : null; +};