跳到正文
Skillpedia
通用编程75k · #3006

runtime-validation

它能做什么

这个技能找出应用从网络、存储、环境设置或表单接收数据的地方,并使用结构校验库添加运行时检查。它面向希望减少意外数据导致的错误的开发者。

适用场景

  • ✓你请智能体为来自 API 或表单的数据添加检查的时候。
  • ✓API 返回的数据结构与预期不同,导致应用出错的时候。
  • ✓你在检查读取本地存储或环境变量的代码,并希望对这些值进行检查的时候。

技能链接

https://github.com/thedaviddias/Front-End-Checklist/blob/main/skills/runtime-validation/SKILL.md

复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。

使用方法

最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。

可直接粘贴的消息

请用这个技能帮我:https://github.com/thedaviddias/Front-End-Checklist/blob/main/skills/runtime-validation/SKILL.md 阅读该链接中的 SKILL.md 文件,并在我的任务中按照它的说明来做。
想让你的 AI 自己找到这样的技能吗?安装免费的 Skillpedia MCP:只需一行,每次任务前它都会在 15,000+ 个技能中搜索。1 分钟完成安装 →

可以这样问 AI

“当 API 发送的数据与预期略有不同时,我的应用就会出错。请帮我在数据进入的地方添加检查吗?”

每周 5 个新技能,1 分钟读完。

工作原理

智能体会找出数据进入应用的地方,例如 fetch 调用、本地存储读取和表单提交。它会添加结构定义,例如使用 Zod,用来检查数据形状,并在不匹配时给出清晰的错误。

技术内容(原始 SKILL.md)

作者在 GitHub 上的英文原文。

Validate external data at runtime with a schema library

TypeScript gives you confidence at compile time, but data from the network, user input, and storage arrives at runtime as raw, untyped values. A backend schema change, a misconfigured API, or malicious input can produce data that does not match your TypeScript types — and the compiler will never warn you. Runtime validation with a schema library catches these mismatches at the boundary, surfaces clear error messages, and prevents type-unsafe data from propagating through your application.

Quick Reference

  • TypeScript types are compile-time only — they are completely erased at runtime
  • API responses can differ from their declared types without causing a compile error
  • A Zod schema simultaneously validates data and infers the TypeScript type
  • Validate at trust boundaries only — not inside every internal function call

Check

Identify all places in this code where external data enters the application (fetch calls, localStorage reads, env variable access, form submissions) and report which ones lack runtime schema validation.

Fix

Add Zod schemas to validate the external data entry points in this code. Show the schema definition, the validated type inference, and where to call .parse() or .safeParse().

Explain

Explain why TypeScript types do not protect against runtime data mismatches, how Zod bridges compile-time and runtime safety, and when to use .parse() versus .safeParse().

Code Review

Review all external data entry points in this file: API calls, storage reads, environment variable access, and form handling. Flag any location where data is cast to a TypeScript type without a preceding runtime validation step.


For full implementation details, code examples, and framework-specific guidance, see references/rule.md.

Rule page: https://frontendchecklist.io/rules/javascript/runtime-validation

常见问题

为什么 TypeScript 类型还不够?

这个技能说,TypeScript 类型在代码运行时会被移除,因此无法在运行时检查实际数据。

应该在哪里进行验证?

这个技能说,应在信任边界即外部数据进入的地方验证,而不是在每个内部函数中验证。

结构定义能给我带来什么?

这个技能说,Zod 结构定义既能检查数据,也能提供对应的 TypeScript 类型。

详情

作者的原始描述

Use when reviewing code that calls fetch(), reads from localStorage, accesses process.env, or processes form submissions without explicitly validating the incoming data shape.

repo: thedaviddias/Front-End-Checklist

热度

GitHub 星标
75k
排名
#3006
许可证
MIT
更新时间
2026/10/31

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能