feat(useSearchParams): Add Date and Zod codec support#368
feat(useSearchParams): Add Date and Zod codec support#368huntabyte merged 6 commits intosvecosystem:mainfrom
Conversation
🦋 Changeset detectedLatest commit: 3416838 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
|
(not review, just commenting) Would it be useful to also/instead support |
|
Hey @cofl 👋 - For example, with a
Codecs provide the flexibility to control both directions. This also means adding support for other data types, such as const stringToBigInt = z.codec(z.string(), z.bigint(), {
decode: (str) => BigInt(str),
encode: (bigint) => bigint.toString(),
});const utf8ToBytes = z.codec(z.string(), z.instanceof(Uint8Array), {
decode: (str) => new TextEncoder().encode(str),
encode: (bytes) => new TextDecoder().decode(bytes),
});const zodSchema = z.object({
num: stringToBigInt.default(0n),
data: utf8ToBytes,
});Valibot is also investigating shipping something similar to codecs in the future. With all that said, codecs are purely optional, including for full and date-only ISO8601 strings (but is needed to support epoch time). This PR also supports
const schema = createSearchParamsSchema({
startDate: { type: "date", default: new Date() }
});
const schema = createSearchParamsSchema({
startDate: { type: "date", default: new Date(), dateFormat: 'date' }
}); |
|
Hey @techniq completely missed this PR! Will check it out now! |
built with Refined Cloudflare Pages Action⚡ Cloudflare Pages Deployment
|
|
Also I'm handling those merge conflicts I just created so don't worry about em :) |
|
Currently having a test around compression fail with the merge so debugging that and will get this released once done. |
|
I'm having a single one of your tests fail after merging in main (main includes some changes to how values including commas are handled, previously we naively assumed if it included a comma it was an array, which is not always the case). Unsure if that would have an impact on that test or not though? @techniq https://github.com/svecosystem/runed/actions/runs/19149411425/job/54735468957?pr=368#step:7:156 |
|
Thanks for the review, fix and release @huntabyte! |
Enhances
useSearchParamswith Date handling (standard schema compatible) and advanced Zod codec support for custom serialization.🎯 Key Features
1. Native Date Support
'date': YYYY-MM-DD format (e.g.,2025-10-21) - perfect for calendar dates'datetime': Full ISO8601 format (e.g.,2025-10-21T18:18:14.196Z) - preserves exact timestampsdateFormatsoptioncreateSearchParamsSchema, Zod, Valibot, and Arktype2. Zod Codec Support (v4.1.0+)
.default()wrapped codecs)validateSearchParams📝 Examples
Date Format Support
Zod Codecs
🔧 Technical Changes
dateFields,dateFormats,codecEncoders, andcodecFieldstracking to schema infoextractZodCodecEncoder()for codec detection (handles direct and.default()wrapped)serializeValue()to respect date formats and codec encodersextractParamValues()to automatically convert date strings to Date objectsvalidateSearchParamswithdateFormatsoption for server-side consistency✅ Testing
📚 Documentation
Comprehensive documentation added covering:
dateFormatsvs Zod codecs