wxt-dev / wxt-dev/wxt

Storage Item Serialization and Validation

Open
#1,173 12 comments 6 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TypeScript
Stars
10.5k
Forks
564
PR merge metrics
No merged PRs in 30d

Description

Feature Request

When defineing storage items, getting values, or setting values, we should support serialization options for converting storage to and from a specific data type:

const enabledSites = storage.defineItem<Set<string>, {}, string[]>("enabledSites", {
  schema: {
    // Convert usable value to storage compatible value
    serialize: (set) => [...set],
    // Convert storage compatible value to usable type
    deserialize: (array) => new Set(array),
  },
});

const set = await enabledSites.getValue();
set.has("google.com")

A natural extension to this is to support various other popular data serialization and validation libraries, like Zod and TypeBox.

import { z } from 'zod';

const themePreference: WxtStorageItem<"light" | "dark" | "system"> = storage.defineItem("themePreference", {
  schema: z.enum(["light", "dark", "system"]),
});
import { Type } from '@sinclair/typebox';
import { Value } from '@sinclair/typebox';

const themePreference: WxtStorageItem<"light" | "dark" | "system"> = storage.defineItem("themePreference", {
  schema: (value) => Value.Parse(
    Type.Union([Type.Literal("light"), Type.Literal("dark"), Type.Literal("system")]),
    value,
  ),
});
Is your feature request related to a bug?

https://github.com/wxt-dev/wxt/discussions/1123

What are the alternatives?

Write a wrapper around storage.defineItem yourself.

Additional context

N/A

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing the storage.defineItem entry point and its schema handling. Determine how serialize and deserialize callbacks should affect defining, reading, and setting storage items, then consider the requested Zod and TypeBox validation patterns. Done means storage items support the shown Set-to-array conversion and schema validation behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
developer-experience
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.