蓝色天空.知识 · 实践 · 创造
我的收藏
在线代码实验室

比较库存工具的默认值、非法参数与执行检查

TYPESCRIPTWindows、OpenCode V1.19.5、Zod 4.6.7、Python 3.12 与回环测试模型。stock.ts
stock.ts30 行
import { z } from "zod";

export const remaining = {
  description: "Subtract the reserved stock from a known quantity",
  args: { quantity: z.number().int().min(0), reserved: z.number().int().min(0).optional() },
  async execute({quantity, reserved = 0}) {
    return "remaining=" + Math.max(0, quantity - reserved);
  },
};

export const restock = {
  description: "Calculate how many items are needed to reach a minimum",
  args: { quantity: z.number().int().min(0), min: z.number().int().min(0) },
  async execute({quantity, min}) {
    return "restock=" + Math.max(0, min - quantity);
  },
};

const counts = z.object({quantity: z.number().int().min(0)});

export const checked = {
  description: "Return a nonnegative integer after validating inside execute",
  args: counts.shape,
  async execute(input) {
    const result = counts.safeParse(input);
    if (!result.success) return "invalid quantity";
    return "quantity=" + result.data.quantity;
  },
};
本例需在 Windows、OpenCode V1.19.5、Zod 4.6.7、Python 3.12 与回环测试模型。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程

运行环境:Windows、OpenCode V1.19.5、Zod 4.6.7、Python 3.12 与回环测试模型。

入口文件:projects/opencode-custom-tools/tools/stock.ts

本地运行步骤快速上手
  1. 点击上方“保存源码”保存为 stock.ts,或下载右侧课程工程包。
  2. 确认本地已准备 Windows、OpenCode V1.19.5、Zod 4.6.7、Python 3.12 与回环测试模型。。
  3. 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载

下载课程关联工程包:OpenCode

课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a240.zip

下载课程关联工程包:OpenCode

课程关联工程包(登记关联28章) · 含当前章节或实例 · mbskycom202612a236.zip
SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习