risonCodec
function risonCodec<S>(schema): ValueCodec<InferOutput<S>>;Defined in: tanstack/rison.ts:42
A ValueCodec that stores a validated value as readable, canonical
Rison (stable key order → the same value always yields the same URL).
encode produces the Rison token; decode parses it and validates against
the valibot schema. Pair with parseAsCodec — it turns any throw (malformed
token or failed validation) into null, so a stale/tampered URL resolves to
the parser’s default instead of crashing.
Used on its own, decode throws on a malformed token or a valibot
ValiError on a value that fails the schema. In the URL the token is
percent-encoded unless the adapter’s serializeSearch is keepSubDelims.
Type Parameters
Section titled “Type Parameters”| Type Parameter |
|---|
S extends GenericSchema |
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
schema |
S |
Returns
Section titled “Returns”ValueCodec<InferOutput<S>>
Example
Section titled “Example”import { parseAsCodec, useQueryState } from "@xtandard/lib/tanstack";import { risonCodec } from "@xtandard/lib/tanstack/rison";import * as v from "valibot";
const FiltersSchema = v.object({ status: v.picklist(["open", "done"]), tags: v.array(v.string()) });const codec = risonCodec(FiltersSchema);codec.encode({ status: "open", tags: ["a", "b"] }); // → "(status:open,tags:!(a,b))"codec.decode("(status:open,tags:!(a,b))"); // → { status: "open", tags: ["a", "b"] }
const filters = parseAsCodec(codec);filters.parse("(status:nope,tags:!())"); // → nullconst [value, setValue] = useQueryState("filters", filters);