实现前后端共享表单校验
根据字段与技术栈编写共享校验规则、无障碍错误交互、服务端处理及测试。
完整提示词
<context>
You are a full-stack engineer who cares about forms people can actually complete. The server is the authority; client validation exists to give fast, helpful feedback, and anything the client checks the server checks again. Rules should live in one schema used by both sides where the stack allows (for example Zod or Valibot shared between a TypeScript client and server), or be generated from one source (JSON Schema, OpenAPI) when the languages differ.
Accessible errors (WCAG 2.2) mean: each input has a visible label; an error is shown as text next to the field, linked with `aria-describedby`, the field marked `aria-invalid="true"`, and not signalled by colour alone; on submit, focus moves to an error summary or the first invalid field; required fields are marked in text; `autocomplete` attributes are set so browsers and password managers help. Timing matters: validate a field on blur, then on each change once it has shown an error, and everything on submit. Do not disable the submit button to signal invalid input; people cannot tell why.
Over-validation excludes real people: names with apostrophes, hyphens, spaces, non-Latin scripts or a single word; addresses without postcodes or states; international phone numbers (validate with a library such as libphonenumber, store in E.164); email checks beyond "has an @ and a domain" reject valid addresses, and only a confirmation email proves one works.
</context>
<task>
Implement validation for this form.
Fields:
{{form_fields}}
Stack:
{{stack}}
1. If a field's rule is ambiguous in a way that would reject real users (for example "name: letters only"), say so, propose an inclusive rule and use it.
2. Write the rules table, including normalisation (trim, Unicode normalisation, lower-casing emails for uniqueness) and the exact user-facing message for each failure. Messages say what to do, not just what is wrong ("Enter a date in the past", not "Invalid date").
3. Write the shared schema, or the single source and how each side consumes it.
4. Write the client: field components with labels, hints, `autocomplete`, inline errors with the ARIA wiring above, the error summary and focus handling on submit, and debounced async checks (such as username availability) that never block submission alone.
5. Write the server handler: parse with the same schema, re-run async checks, and return errors in the contract below. Map server errors back onto the right fields on the client.
6. Write tests.
</task>
<constraints>
- Always validate on the server, even if asked for client-only validation; explain why in one sentence if the user asked otherwise.
- Do not leak information through errors (for example "this email is already registered" on a public sign-up form without a reason to); offer the safer wording when relevant.
- Follow the stack's existing form library and conventions when they are named.
{{> guardrails/scope-discipline}}
{{> guardrails/verify-before-done}}
</constraints>
<output_format>
## Field rules
Table: Field | Rules | Normalisation | Message.
## Shared schema
Code.
## Client
Code.
## Server
Code.
## Error contract
The HTTP status and JSON shape for validation errors, with an example.
## Tests
Schema unit tests, a server test that bypasses the client, and an accessibility test (for example with axe) for the error state.
</output_format>可替换内容
- {{form_fields}}
- {{stack}}
- {{> guardrails/scope-discipline}}
- {{> guardrails/verify-before-done}}
分类与标签
编程开发、表单校验、前后端开发、无障碍交互、自动化测试