[PR #4567] [CLOSED] feat(frontend): introduce Zod for runtime validation #5718

Closed
opened 2026-11-02 04:03:34 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/MHSanaei/3x-ui/pull/4567
Author: @MHSanaei
Created: 5/25/2026
Status: ❌ Closed

Base: main ← Head: feat/frontend-zod-validation


📝 Commits (10+)

  • 6846fac feat(frontend): add Zod runtime validation at API boundary
  • d00ddc3 feat(frontend): extend Zod validation to remaining query/mutation hooks
  • c16fb93 fix(frontend): allow null slices in client/summary schemas
  • 2cd2085 fix(vite): treat /panel/xray as SPA page, not API root
  • 6bbc9f6 feat(frontend): drive form validation from Zod schemas
  • 75b0a21 chore(frontend): silence swagger-ui-react peer-dep warnings on React 19
  • 2d55b3b fix(vite): bypass es-toolkit CJS shim for recharts deep imports
  • a3012da feat(frontend): migrate five secondary form modals to Zod schemas
  • 4ecbb0e feat(frontend): block invalid settings saves with Zod pre-save check
  • 9cf3523 feat(frontend): schema-guard Inbound and Outbound form submits

📊 Changes

59 files changed (+3145 additions, -804 deletions)

View changed files

📝 database/model/model.go (+8 -8)
📝 frontend/package-lock.json (+156 -155)
📝 frontend/package.json (+11 -2)
📝 frontend/src/api/queries/useAllSettings.ts (+14 -12)
📝 frontend/src/api/queries/useNodeMutations.ts (+16 -21)
📝 frontend/src/api/queries/useNodesQuery.ts (+7 -34)
📝 frontend/src/api/queries/useStatusQuery.ts (+4 -1)
➕ frontend/src/generated/types.ts (+359 -0)
➕ frontend/src/generated/zod.ts (+380 -0)
📝 frontend/src/hooks/useClients.ts (+59 -95)
📝 frontend/src/hooks/useDatepicker.ts (+5 -5)
📝 frontend/src/hooks/useXraySetting.ts (+40 -60)
📝 frontend/src/pages/clients/ClientBulkAddModal.tsx (+6 -23)
📝 frontend/src/pages/clients/ClientBulkAdjustModal.tsx (+9 -4)
📝 frontend/src/pages/clients/ClientFormModal.tsx (+22 -6)
📝 frontend/src/pages/inbounds/InboundFormModal.tsx (+15 -1)
📝 frontend/src/pages/inbounds/useInbounds.ts (+19 -31)
📝 frontend/src/pages/index/CustomGeoFormModal.tsx (+7 -25)
📝 frontend/src/pages/index/CustomGeoSection.tsx (+1 -1)
📝 frontend/src/pages/index/IndexPage.tsx (+2 -2)

...and 39 more files

📄 Description

Summary

Adds Zod 4 as the source of truth for runtime validation across the frontend, in three layers:

  • API boundary — parseMsg(msg, schema, context) validates ~20 endpoint responses against schemas (status, nodes, clients, inbounds, xray, settings, auth). On drift, logs a [zod] ... console warning and passes the payload through unchanged so users never see a broken UI from a backend metadata change.
  • Form validation — eight form modals migrated to schema-driven submit (LoginPage, NodeFormModal, ClientFormModal, ClientBulkAddModal, ClientBulkAdjustModal, BalancerFormModal, RuleFormModal, CustomGeoFormModal, TwoFactorModal). NodeFormModal is the full AntD Form.useForm + antdRule showcase; others use the lighter safeParse-on-submit pattern that keeps existing controlled-component state intact.
  • Settings save gate — AllSettingSchema now carries the real ranges and path rules (ports 1-65535, tgCpu 0-100, subUpdates 1-168 h, webBasePath/subPath must start with /, etc.). SettingsPage runs the full schema against the draft before save and surfaces the first issue.

Two unrelated infra fixes hitched on the same branch because they were uncovered during dev-server testing:

  • fix(vite): treat /panel/xray as SPA page — the dev-server bypass classified /panel/xray as an API path because of an exact-match against panel/xray/ (the API prefix with trailing slash stripped). On reload, /panel/xray got proxied to the Go backend, which returned the embedded built index.html and broke asset URLs.
  • fix(vite): bypass es-toolkit CJS shim for recharts deep imports — es-toolkit's ./compat/* exports map only declares a CJS condition, and the shim uses a require_X.Y pattern that both Vite's optimizer (Rolldown in Vite 8) and the prod build mishandle, crashing every recharts route with require_isUnsafeProperty is not a function. A small enforce: 'pre' plugin emits a virtual ESM module per es-toolkit/compat/<name> import that re-exports the named symbol as default — works in dev (registered via optimizeDeps.rolldownOptions.plugins) and prod (top-level plugin).

One small dep-compat chore: npm overrides to silence swagger-ui-react's React-19 peer-dep warnings, including the nested-override syntax ("react-debounce-input": { "react": "^19.0.0" }) for the wedged 2022 package that has no maintained successor.

Commits

4ecbb0e5 feat(frontend): block invalid settings saves with Zod pre-save check
a3012daa feat(frontend): migrate five secondary form modals to Zod schemas
2d55b3b6 fix(vite): bypass es-toolkit CJS shim for recharts deep imports
75b0a219 chore(frontend): silence swagger-ui-react peer-dep warnings on React 19
6bbc9f67 feat(frontend): drive form validation from Zod schemas
2cd2085b fix(vite): treat /panel/xray as SPA page, not API root
c16fb938 fix(frontend): allow null slices in client/summary schemas
d00ddc3f feat(frontend): extend Zod validation to remaining query/mutation hooks
6846fac1 feat(frontend): add Zod runtime validation at API boundary

Test plan

  • npm run typecheck && npm run lint && npm run build from frontend/ — all clean.
  • npm run dev, log in, leave the dashboard open ~30s. Console should show no [zod] ... warnings.
  • Open the Nodes page, Settings page, Xray Configs (was the recharts crash repro), Clients page — all render.
  • Drift simulation: in DevTools, override /panel/api/server/status to drop a top-level field. UI should keep rendering and exactly one [zod] server/status response failed validation warning should appear.
  • Login with empty username — inline AntD field error appears (proves antdRule reaches i18next).
  • Add a Node with empty name — inline error appears (proves the full AntD Form path).
  • In Settings, set webPort to 70000 and click Save — toast shows webPort: Number must be less than or equal to 65535, request never goes out. Reset to a valid port — save succeeds.
  • On /panel/api-docs, expand a JSON response (react-inspector v9 path) and toggle the "Try it out" textarea (react-debounce-input path) — both should work.

Non-goals (deferred to follow-ups)

  • InboundFormModal (2248 lines) and OutboundFormModal (1471 lines) — the largest forms still use the manual if (!form.x) pattern.
  • Class-based models (Status, AllSetting, DBInbound) stay; schemas validate input but the classes are still the runtime wrapper.
  • OpenAPI → Zod schema auto-generation in scripts/build-openapi.mjs.

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/MHSanaei/3x-ui/pull/4567 **Author:** [@MHSanaei](https://github.com/MHSanaei) **Created:** 5/25/2026 **Status:** ❌ Closed **Base:** `main` ← **Head:** `feat/frontend-zod-validation` --- ### 📝 Commits (10+) - [`6846fac`](https://github.com/MHSanaei/3x-ui/commit/6846fac1ccd27730cd9a2ce1e82006fca38b834a) feat(frontend): add Zod runtime validation at API boundary - [`d00ddc3`](https://github.com/MHSanaei/3x-ui/commit/d00ddc3f5823bea2fc3a81bebd6b919f52d004ee) feat(frontend): extend Zod validation to remaining query/mutation hooks - [`c16fb93`](https://github.com/MHSanaei/3x-ui/commit/c16fb9389925f1ad240ee17a3d5cf2bfb7f12d50) fix(frontend): allow null slices in client/summary schemas - [`2cd2085`](https://github.com/MHSanaei/3x-ui/commit/2cd2085b75bb5ae979387d33be4e54b3ee19792e) fix(vite): treat /panel/xray as SPA page, not API root - [`6bbc9f6`](https://github.com/MHSanaei/3x-ui/commit/6bbc9f6769fc0d6a29e810d972c3edff2f80b6c3) feat(frontend): drive form validation from Zod schemas - [`75b0a21`](https://github.com/MHSanaei/3x-ui/commit/75b0a2198797f795995551b36b2b647364b15d90) chore(frontend): silence swagger-ui-react peer-dep warnings on React 19 - [`2d55b3b`](https://github.com/MHSanaei/3x-ui/commit/2d55b3b663720d5ca075a4ffaff99517d92e877b) fix(vite): bypass es-toolkit CJS shim for recharts deep imports - [`a3012da`](https://github.com/MHSanaei/3x-ui/commit/a3012daa8f370804ceb7022648562513ede0b78b) feat(frontend): migrate five secondary form modals to Zod schemas - [`4ecbb0e`](https://github.com/MHSanaei/3x-ui/commit/4ecbb0e55f4352587e323c1e69721e861afe7515) feat(frontend): block invalid settings saves with Zod pre-save check - [`9cf3523`](https://github.com/MHSanaei/3x-ui/commit/9cf35234a5b26a73833e50140dd1bac31023aae5) feat(frontend): schema-guard Inbound and Outbound form submits ### 📊 Changes **59 files changed** (+3145 additions, -804 deletions) <details> <summary>View changed files</summary> 📝 `database/model/model.go` (+8 -8) 📝 `frontend/package-lock.json` (+156 -155) 📝 `frontend/package.json` (+11 -2) 📝 `frontend/src/api/queries/useAllSettings.ts` (+14 -12) 📝 `frontend/src/api/queries/useNodeMutations.ts` (+16 -21) 📝 `frontend/src/api/queries/useNodesQuery.ts` (+7 -34) 📝 `frontend/src/api/queries/useStatusQuery.ts` (+4 -1) ➕ `frontend/src/generated/types.ts` (+359 -0) ➕ `frontend/src/generated/zod.ts` (+380 -0) 📝 `frontend/src/hooks/useClients.ts` (+59 -95) 📝 `frontend/src/hooks/useDatepicker.ts` (+5 -5) 📝 `frontend/src/hooks/useXraySetting.ts` (+40 -60) 📝 `frontend/src/pages/clients/ClientBulkAddModal.tsx` (+6 -23) 📝 `frontend/src/pages/clients/ClientBulkAdjustModal.tsx` (+9 -4) 📝 `frontend/src/pages/clients/ClientFormModal.tsx` (+22 -6) 📝 `frontend/src/pages/inbounds/InboundFormModal.tsx` (+15 -1) 📝 `frontend/src/pages/inbounds/useInbounds.ts` (+19 -31) 📝 `frontend/src/pages/index/CustomGeoFormModal.tsx` (+7 -25) 📝 `frontend/src/pages/index/CustomGeoSection.tsx` (+1 -1) 📝 `frontend/src/pages/index/IndexPage.tsx` (+2 -2) _...and 39 more files_ </details> ### 📄 Description ## Summary Adds Zod 4 as the source of truth for runtime validation across the frontend, in three layers: - **API boundary** — `parseMsg(msg, schema, context)` validates ~20 endpoint responses against schemas (status, nodes, clients, inbounds, xray, settings, auth). On drift, logs a `[zod] ...` console warning and passes the payload through unchanged so users never see a broken UI from a backend metadata change. - **Form validation** — eight form modals migrated to schema-driven submit (`LoginPage`, `NodeFormModal`, `ClientFormModal`, `ClientBulkAddModal`, `ClientBulkAdjustModal`, `BalancerFormModal`, `RuleFormModal`, `CustomGeoFormModal`, `TwoFactorModal`). `NodeFormModal` is the full AntD `Form.useForm` + `antdRule` showcase; others use the lighter `safeParse`-on-submit pattern that keeps existing controlled-component state intact. - **Settings save gate** — `AllSettingSchema` now carries the real ranges and path rules (ports 1-65535, `tgCpu` 0-100, `subUpdates` 1-168 h, `webBasePath`/`subPath` must start with `/`, etc.). `SettingsPage` runs the full schema against the draft before save and surfaces the first issue. Two unrelated infra fixes hitched on the same branch because they were uncovered during dev-server testing: - **`fix(vite): treat /panel/xray as SPA page`** — the dev-server bypass classified `/panel/xray` as an API path because of an exact-match against `panel/xray/` (the API prefix with trailing slash stripped). On reload, `/panel/xray` got proxied to the Go backend, which returned the embedded built `index.html` and broke asset URLs. - **`fix(vite): bypass es-toolkit CJS shim for recharts deep imports`** — `es-toolkit`'s `./compat/*` exports map only declares a CJS condition, and the shim uses a `require_X.Y` pattern that both Vite's optimizer (Rolldown in Vite 8) and the prod build mishandle, crashing every recharts route with `require_isUnsafeProperty is not a function`. A small `enforce: 'pre'` plugin emits a virtual ESM module per `es-toolkit/compat/<name>` import that re-exports the named symbol as default — works in dev (registered via `optimizeDeps.rolldownOptions.plugins`) and prod (top-level plugin). One small dep-compat chore: npm `overrides` to silence `swagger-ui-react`'s React-19 peer-dep warnings, including the nested-override syntax (`"react-debounce-input": { "react": "^19.0.0" }`) for the wedged 2022 package that has no maintained successor. ## Commits ``` 4ecbb0e5 feat(frontend): block invalid settings saves with Zod pre-save check a3012daa feat(frontend): migrate five secondary form modals to Zod schemas 2d55b3b6 fix(vite): bypass es-toolkit CJS shim for recharts deep imports 75b0a219 chore(frontend): silence swagger-ui-react peer-dep warnings on React 19 6bbc9f67 feat(frontend): drive form validation from Zod schemas 2cd2085b fix(vite): treat /panel/xray as SPA page, not API root c16fb938 fix(frontend): allow null slices in client/summary schemas d00ddc3f feat(frontend): extend Zod validation to remaining query/mutation hooks 6846fac1 feat(frontend): add Zod runtime validation at API boundary ``` ## Test plan - [ ] `npm run typecheck && npm run lint && npm run build` from `frontend/` — all clean. - [ ] `npm run dev`, log in, leave the dashboard open ~30s. Console should show no `[zod] ...` warnings. - [ ] Open the Nodes page, Settings page, Xray Configs (was the recharts crash repro), Clients page — all render. - [ ] Drift simulation: in DevTools, override `/panel/api/server/status` to drop a top-level field. UI should keep rendering and exactly one `[zod] server/status response failed validation` warning should appear. - [ ] Login with empty username — inline AntD field error appears (proves `antdRule` reaches i18next). - [ ] Add a Node with empty name — inline error appears (proves the full AntD Form path). - [ ] In Settings, set `webPort` to `70000` and click Save — toast shows `webPort: Number must be less than or equal to 65535`, request never goes out. Reset to a valid port — save succeeds. - [ ] On `/panel/api-docs`, expand a JSON response (`react-inspector` v9 path) and toggle the "Try it out" textarea (`react-debounce-input` path) — both should work. ## Non-goals (deferred to follow-ups) - `InboundFormModal` (2248 lines) and `OutboundFormModal` (1471 lines) — the largest forms still use the manual `if (!form.x)` pattern. - Class-based models (`Status`, `AllSetting`, `DBInbound`) stay; schemas validate input but the classes are still the runtime wrapper. - OpenAPI → Zod schema auto-generation in `scripts/build-openapi.mjs`. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-02 04:03:34 +07:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
MHSanaei/3x-ui#5718
No description provided.