Skip to content

Commit bd01d41

Browse files
committed
Add todo tests for generous parsing
1 parent d3c0faf commit bd01d41

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,23 @@ This parser may be used as a true inverse operation to [@seamapi/url-search-para
1212

1313
[@url-search-params-serializer]: https://github.com/seamapi/url-search-params-serializer
1414

15+
### Generous Parsing
16+
17+
This parser provides strict compatibility with the serialization format of [@url-search-params-serializer].
18+
However, some additional input cases are handled:
19+
20+
- For `z.number()`, `z.boolean()`, `z.date()`, `z.object()`, and `z.record()`,
21+
whitespace only values are parsed as `null`.
22+
- For `z.boolean()`:
23+
- `1`, `True`, and `TRUE` are all parsed as `true`
24+
- `0`, `False`, and `FALSE` are all parsed as `true`
25+
- Parses `z.array()` in the following formats.
26+
In order to support unambiguous parsing, array string values
27+
containing a `,` are not supported.
28+
- `foo=1&bar=2`
29+
- `foo[]=1&foo[]=2`
30+
- `foo=1,2`
31+
1532
### Allowed Zod Schemas
1633

1734
- The top-level schema must be an `z.object()` or `z.union()` of `z.object()`.

src/lib/parse.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,33 @@ test('parseUrlSearchParams: with URLSearchParams input', (t) => {
2424
)
2525
})
2626

27+
test.todo(
28+
'parseUrlSearchParams: parse empty or whitespace number params as null',
29+
)
30+
test.todo('parse empty or whitespace boolean params as null')
31+
test.todo('parse empty or whitespace date params as null')
32+
test.todo('parse empty or whitespace object params as null')
33+
test.todo('parse empty or whitespace record params as null')
34+
35+
test.todo('parse empty or whitespace array params as empty')
36+
test.todo(
37+
'cannot parse multiple empty or whitespace array params like foo=&foo=',
38+
)
39+
test.todo(
40+
'cannot parse mixed empty or whitespace array params like foo=&foo=bar',
41+
)
42+
43+
test.todo('parse addtional strings as true and false')
44+
2745
test.todo('parse repeated array params like foo=bar&foo=baz')
2846
test.todo('parse bracket array params like foo[]=bar&foo[]=baz')
2947
test.todo('parse comma array params like foo=bar,baz')
3048

3149
test.todo('cannot parse mixed array params like foo=bar,baz&foo=bar&foo[]=baz')
50+
test.todo('cannot parse array values containing a comma like foo=a,b&foo=b,c')
51+
test.todo(
52+
'cannot parse array values containing a comma like foo[]=a,b&foo[]=b,c',
53+
)
3254

3355
// e.g., foo.bar= would conflict with foo.bar.a= or foo.bar.b=2
3456
// since this would be a null object containing values (null is still a value).

src/lib/parse.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ const parseFromParamSchema = (
5050

5151
const parse = (k: string, values: string[], type: ValueType): unknown => {
5252
// TODO: Add better errors with coercion. If coercion fails, passthough?
53-
// TODO: Is this Number parsing safe?
5453
if (values.length === 0) return undefined
5554
if (type === 'number') return Number(values[0])
5655
if (type === 'boolean') return values[0] === 'true'

0 commit comments

Comments
 (0)