Skip to content

Commit

Permalink
fix: Handle parsing errors on base64 and json encoded data (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
schpet authored Aug 15, 2024
1 parent dd9b08b commit 7f3bb39
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 7 deletions.
2 changes: 2 additions & 0 deletions .aider.conf.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ auto-lint: true
# runs vitest
test-cmd: pnpm run test && pnpm run types
auto-test: true

read: CONVENTIONS.md
10 changes: 5 additions & 5 deletions CONVENTIONS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
- Avoid casting with as whenever possible
- Avoid unsafe types like any
- Prefer checking for presence with 'val == null' instead of '!val'
- Use unique test identifiers
- Don't add comments
- avoid casting with as whenever possible
- avoid unsafe types like any
- prefer checking for presence with 'val == null' instead of '!val'
- avoid adding comments explaining code
- prefer for of loops over traditional for loops or forEach
8 changes: 7 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,13 @@ const stringToNumber = z.string().transform((val) => {
})
const stringToDate = z.string().transform((val) => new Date(val))
const stringToBigInt = z.string().transform((val) => BigInt(val))
const stringToOther = z.string().transform((val) => JSON.parse(base64ToUtf8(val)))
const stringToOther = z.string().transform((val) => {
try {
return JSON.parse(base64ToUtf8(val))
} catch (error) {
return undefined
}
})

function parseValue(value: string, schemaType: z.ZodTypeAny): unknown {
if (schemaType instanceof z.ZodNumber) {
Expand Down
57 changes: 56 additions & 1 deletion test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { assert, test } from "vitest"
import { assert, expect, test } from "vitest"
import { z } from "zod"
import { ZodURLSearchParamSerializer, lenientParse, parse, safeParse, serialize } from "../src"

Expand Down Expand Up @@ -245,6 +245,61 @@ test("serialize and parse nested object with emoji", () => {
assert.strictEqual(parsed.p.c, "Hello, 🌍!")
})

test("serialize a nested object", () => {
const schema = z.object({
user: z.object({
name: z.string(),
}),
})

const data = {
user: {
name: "John Doe",
},
}

const serialized = serialize({ schema, data })
expect(serialized.toString()).toMatchInlineSnapshot(`"user=eyJuYW1lIjoiSm9obiBEb2UifQ"`)
})

test("parse a nested object", () => {
const schema = z.object({
user: z.object({
name: z.string(),
}),
})
let result = parse({ schema, input: new URLSearchParams("user=eyJuYW1lIjoiSm9obiBEb2UifQ") })
expect(result.user.name).toBe("John Doe")
})

test("parse a nested object with invalid json", () => {
const schema = z.object({
user: z.object({
name: z.string(),
}),
})

expect(() => parse({ schema, input: new URLSearchParams("user=nope") })).toThrow(z.ZodError)
})

test("lenientParse a nested object with invalid json", () => {
const schema = z.object({
user: z.object({
name: z.string(),
}),
})

const defaultData = {
user: {
name: "Default Name",
},
}

const result = lenientParse({ schema, input: new URLSearchParams("user=nope"), defaultData })

expect(result).toEqual(defaultData)
})

test("safeParse with valid and invalid input", () => {
const schema = z.object({
age: z.number(),
Expand Down

0 comments on commit 7f3bb39

Please sign in to comment.