Skip to content

Commit

Permalink
No commit message
Browse files Browse the repository at this point in the history
  • Loading branch information
schpet committed Aug 11, 2024
1 parent 1a70e55 commit a392595
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 45 deletions.
12 changes: 9 additions & 3 deletions README.markdown
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
# zod urlsearchparams

A library for serializing and parsing URLSearchParams using Zod schemas.
solving the age old problem of reading and writing urls.

- minimal urls, scalars are readable and editable by humans
- vectors are encoded with base64
- zero dependencies

## examples

note: use the ZodURLSearchParamSerializer class api, but `serialize`, `parse`, `shape`, `lenientParse` etc are exported on their own too.

### serializing

```ts
Expand All @@ -28,7 +34,7 @@ assert.strictEqual(params.toString(), "name=John+Doe&age=30&hobbies=reading&hobb

```ts
// sometimes people will visit a url that doesn't conform
const invalidParams = new URLSearchParams("name=Jane+Doe&age=invalid&hobbies=reading&hobbies=gardening")
const invalidParams = new URLSearchParams("name=Jane+Doe&age=nope&hobbies=reading&hobbies=gardening")

// so we provide defaults to fall back to
const defaultData: z.infer<typeof schema> = {
Expand All @@ -43,7 +49,7 @@ const lenientResult = serializer.lenientParse(invalidParams, defaultData)
// it'll drop the invalid field and use the default value
assert.deepStrictEqual(lenientResult, {
name: "Jane Doe",
age: 25, // Uses default value because 'invalid' can't be parsed as a number
age: 25, // Uses default value because 'nope' can't be parsed as a number
hobbies: ["reading", "gardening"],
})
```
42 changes: 0 additions & 42 deletions docs/readme-example.ts

This file was deleted.

0 comments on commit a392595

Please sign in to comment.