persist-standard-schema
Validate persisted state with Standard Schema (~standard) via withStandardSchema / withStandardSchemaAsync / createStandardSchemaStorage. Use for Zod/Yup (etc.) sync vs async lanes and PersistDecodeRethrowError.
- License 许可
- MIT
- 最近更新
- 2026-11-01
- Stars
- ★ 5
- Forks
- 0
01 前言 Front Matter
- name
- persist-standard-schema
- license
- MIT
- sources
- [ "stainless-code/persist:src/adapters/codecs/standard-schema.ts", "stainless-code/persist:apps/docs/content/recipes/standard-schema.mdx" ]
- metadata
- { "type": "composition", "library": "@stainless-code/persist", "library_version": "0.4.1" }
- requires
- [ "persist" ]
- description
- Validate persisted state with Standard Schema (~standard) via withStandardSchema / withStandardSchemaAsync / createStandardSchemaStorage. Use for Zod/Yup (etc.) sync vs async lanes and PersistDecodeRethrowError.
03 SKILL.md 正文
Standard Schema codec / wraps
Types for ~standard are vendored — no runtime peer. Bring Zod / Yup / Valibot yourself.
Validates persisted state only (not envelope version / timestamp / buster). Encode writes schema output (defaults/transforms applied).
Sync vs async lanes
| API | When |
|---|---|
withStandardSchema / createStandardSchemaStorage |
Sync validate (Zod ≥3.24 / v4 via ~standard) |
withStandardSchemaAsync / createStandardSchemaStorageAsync |
Async validate (Yup, async Zod) |
Wrong lane: sync wrap + async schema → PersistDecodeRethrowError (rethrown; not clearCorrupt). Async lane → async hydrate → gate UI (react-persist, …).
Sync wrap is still Promise-aware for async backend getItem (e.g. IDB).
Minimal wiring
import { withStandardSchema } from "@stainless-code/persist/codecs/standard-schema";
import { createIdbStorage } from "@stainless-code/persist/backends/idb";
const storage = withStandardSchema(createIdbStorage<Prefs>()!, prefsSchema, {
clearCorruptOnFailure: true,
});
JSON sugar: createStandardSchemaStorage(() => localStorage, schema).
Common mistakes
- Async schema on the sync wrap.
- Treating wrong-lane throws as corrupt — they must not clear storage.
- Schema-checking the whole envelope — only
state. - Skipping
useHydratedon the async lane. - Stacking wrap +
standardSchemaCodec— double-validates; pick one.
API surface
standardSchemaCodec(schema)·withStandardSchema/withStandardSchemaAsynccreateStandardSchemaStorage/createStandardSchemaStorageAsync
See also: persist-idb for async backends.
04 同仓库更多 Skills
vue-persist
Gate Vue 3 UI on Persist hydration with useHydrated (shallowRef). Use in setup/effectScope when avoiding flash of default state on async backends.
svelte-store-persist
Gate Svelte store-based UI on Persist hydration with hydratedStore (Readable). Use for Svelte 3–4 or Svelte 5 apps that still use stores; runes UI → svelte-persist.
svelte-persist
Gate Svelte 5 runes UI on Persist hydration with hydratedRune (.current). Use for Svelte 5; for stores / Svelte 3–4 use svelte-store-persist.
solid-persist
Gate Solid UI on Persist hydration with useHydrated (Accessor). Use when avoiding flash of default state on async backends; read hydrated() in reactive scopes.
react-persist
Gate React UI on Persist hydration with useHydrated + toHydrationSignal. Use when avoiding flash of default state on async backends (IndexedDB), SSR snapshot true, or wiring HydrationSignal into React 18/19.
preact-persist
Gate Preact UI on Persist hydration with useHydrated (preact/compat useSyncExternalStore). Use when avoiding flash of default state on async backends or wiring HydrationSignal into Preact.