Skip to content

Commit

Permalink
add zod schema validation
Browse files Browse the repository at this point in the history
  • Loading branch information
suejung-sentry committed Dec 20, 2024
1 parent a4fa5a5 commit 6deb723
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ const mockGetUploadTokenRequired = {

const mockDetailOwner = {
owner: {
ownerid: '1234',
ownerid: 1234,
username: 'codecov',
avatarUrl: 'https://avatars.githubusercontent.com/u/1234?v=4',
isCurrentUserPartOfOrg: true,
Expand Down
27 changes: 27 additions & 0 deletions src/services/user/useOwner.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,23 @@
import { useQuery } from '@tanstack/react-query'
import { useParams } from 'react-router-dom'
import { z } from 'zod'

import Api from 'shared/api'
import { rejectNetworkError } from 'shared/api/helpers'

const OwnerSchema = z.object({
ownerid: z.number().nullish(),
username: z.string().nullish(),
avatarUrl: z.string().nullish(),
isCurrentUserPartOfOrg: z.boolean().nullish(),
isAdmin: z.boolean().nullish(),
})

export type Owner = z.infer<typeof OwnerSchema>

const RequestSchema = z.object({
owner: OwnerSchema.nullish(),
})

interface URLParams {
provider: string
Expand Down Expand Up @@ -40,6 +56,17 @@ export function useOwner({
queryKey: ['owner', variables, provider, query],
queryFn: ({ signal }) =>
Api.graphql({ provider, query, variables, signal }).then((res) => {
const parsedRes = RequestSchema.safeParse(res?.data)

if (!parsedRes.success) {
return rejectNetworkError({
status: 404,
data: {},
dev: 'useOwner - 404 Failed to parse data',
error: parsedRes.error,
})
}

return res?.data?.owner
}),
...opts,
Expand Down

0 comments on commit 6deb723

Please sign in to comment.