Skip to content

Commit

Permalink
isValid
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanniser committed Mar 13, 2023
1 parent 6a28d49 commit 3c62ac9
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/stupid-wasps-push.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"next-typesafe-url": patch
---

feat: added isValid flag to prevent need to check isReady && !isError
21 changes: 16 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,14 +160,23 @@ $path({ path: "/" searchParams: { foo: undefined, bar: true } }) // => "/?bar=tr

```tsx
const params = useSearchParams(Route.searchParams);
const { data, isReady, isError, error } = params;

// if isReady is true, and isError is false, then data will be in the shape of Route.searchParams
const {
data,
isValid,
isReady,
isError,
error
} = params;

// if isReady is true, and isError is false (isValid is true), then data *will* be in the shape of Route.searchParams
// in this case, data will be { userInfo: { name: string, age: number } }

if (isError) {
return <div>Invalid search params</div>;
if (!isReady) {
return <div>loading...</div>;
} else if (isError) {
return <div>Invalid search params {error.message}</div>;
} else {
// isValid === true
return <div>{data.userInfo.name}</div>;
}
```
Expand All @@ -176,6 +185,8 @@ if (isError) {

**If `isReady` is true and `isError` is false, then `data` will always be valid and match the schema.**

*For convenience, instead of needing checking `isReady && !isError`, I have added the `isValid` flag which is only true when `isReady` is `true` and and `isError` is false.*

## Reccomended Usage

**It is reccomended to only call `useSearchParams` and `useRouteParams` in the top level component of each route, and pass the data down to child components through props or context.**
Expand Down
3 changes: 3 additions & 0 deletions src/generateTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,18 +143,21 @@ declare function useSearchParams<T extends z.AnyZodObject>(
type UseParamsResult<T extends z.AnyZodObject> =
| {
data: z.infer<T>;
isValid: true;
isReady: true;
isError: false;
error: undefined;
}
| {
data: undefined;
isValid: false;
isReady: true;
isError: true;
error: z.ZodError<T>;
}
| {
data: undefined;
isValid: false;
isReady: false;
isError: false;
error: undefined;
Expand Down
10 changes: 6 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,13 @@ export function useRouteParams<T extends z.AnyZodObject>(
}, [router, validator]);

if (isError && isReady) {
return { data: undefined, isReady: true, isError: true, error: error };
return { data: undefined, isValid: false, isReady: true, isError: true, error: error };
} else if (data !== undefined && isReady) {
return { data: data, isReady: true, isError: false, error: undefined };
return { data: data, isValid: true, isReady: true, isError: false, error: undefined };
} else {
return {
data: undefined,
isValid: false,
isReady: false,
isError: false,
error: undefined,
Expand Down Expand Up @@ -99,12 +100,13 @@ export function useSearchParams<T extends z.AnyZodObject>(
}, [router, searchValidator]);

if (isError && isReady) {
return { data: undefined, isReady: true, isError: true, error: error };
return { data: undefined, isValid: false, isReady: true, isError: true, error: error };
} else if (data !== undefined && isReady) {
return { data: data, isReady: true, isError: false, error: undefined };
return { data: data, isValid: true, isReady: true, isError: false, error: undefined };
} else {
return {
data: undefined,
isValid: false,
isReady: false,
isError: false,
error: undefined,
Expand Down
3 changes: 3 additions & 0 deletions src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,18 +65,21 @@ declare function useSearchParams<T extends z.AnyZodObject>(
export type UseParamsResult<T extends z.AnyZodObject> =
| {
data: z.infer<T>;
isValid: true;
isReady: true;
isError: false;
error: undefined;
}
| {
data: undefined;
isValid: false;
isReady: true;
isError: true;
error: z.ZodError<T>;
}
| {
data: undefined;
isValid: false;
isReady: false;
isError: false;
error: undefined;
Expand Down

0 comments on commit 3c62ac9

Please sign in to comment.