Skip to content

Commit d46ac92

Browse files
authored
fix(api): correctly format ZodError responses (#703)
1 parent 4ad5b3a commit d46ac92

8 files changed

Lines changed: 28 additions & 23 deletions

File tree

api/src/routes/get-profile.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { HTTPException } from 'hono/http-exception'
22
import z from 'zod'
3-
import { zValidator } from '@hono/zod-validator'
43
import {
54
ConfigStorageService,
65
ConfigStorageServiceError,
@@ -11,17 +10,17 @@ import { AWS_PREFIX } from '@shared/defines'
1110
import { PROFILE_IDS, TOOLS } from '@shared/types'
1211
import type { Configuration } from '@shared/types'
1312
import { app } from '../app.js'
14-
import { createHTTPException } from '../utils/utils.js'
13+
import { createHTTPException, validate } from '../utils/utils.js'
1514

1615
app.get(
1716
'/profile/:tool',
18-
zValidator(
17+
validate(
1918
'param',
2019
z.object({
2120
tool: z.enum(TOOLS),
2221
}),
2322
),
24-
zValidator(
23+
validate(
2524
'query',
2625
z.object({
2726
wa: z.url(),

api/src/routes/payment/initiate.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { HTTPException } from 'hono/http-exception'
22
import z from 'zod'
3-
import { zValidator } from '@hono/zod-validator'
43
import { createId } from '@paralleldrive/cuid2'
54
import { app } from '../../app'
65
import {
@@ -9,7 +8,7 @@ import {
98
} from '../../schemas/payment'
109
import { OpenPaymentsService } from '../../utils/open-payments'
1110
import { setData } from '../../utils/payments-kv'
12-
import { createHTTPException } from '../../utils/utils'
11+
import { createHTTPException, validate } from '../../utils/utils'
1312

1413
const PaymentInitiateSchema = z
1514
.object({
@@ -30,7 +29,7 @@ export type PaymentInitiateResult = {
3029

3130
app.post(
3231
'/payment/initiate',
33-
zValidator('json', PaymentInitiateSchema),
32+
validate('json', PaymentInitiateSchema),
3433
async ({ req, json, env }) => {
3534
try {
3635
const params = req.valid('json')

api/src/routes/payment/quotes.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import z from 'zod'
2-
import { zValidator } from '@hono/zod-validator'
32
import type { Amount, PaymentError } from '@shared/types'
43
import { app } from '../../app.js'
54
import {
@@ -10,7 +9,7 @@ import {
109
isNonPositiveAmountError,
1110
OpenPaymentsService,
1211
} from '../../utils/open-payments.js'
13-
import { createHTTPException } from '../../utils/utils.js'
12+
import { createHTTPException, validate } from '../../utils/utils.js'
1413

1514
const PaymentQuoteSchema = z
1615
.object({
@@ -23,7 +22,7 @@ export type PaymentQuoteInput = z.infer<typeof PaymentQuoteSchema>
2322

2423
app.post(
2524
'/payment/quotes',
26-
zValidator('json', PaymentQuoteSchema),
25+
validate('json', PaymentQuoteSchema),
2726
async ({ req, json, env }) => {
2827
try {
2928
const openPayments = await OpenPaymentsService.getInstance(env)

api/src/routes/payment/redirect.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import { HTTPException } from 'hono/http-exception'
22
import z from 'zod'
3-
import { zValidator } from '@hono/zod-validator'
43
import { urlWithParams } from '@shared/utils'
54
import { app } from '../../app'
65
import { PaymentIdSchema } from '../../schemas/payment'
76
import { OpenPaymentsService } from '../../utils/open-payments'
87
import { getData, setData } from '../../utils/payments-kv'
9-
import { createHTTPException } from '../../utils/utils'
8+
import { createHTTPException, validate } from '../../utils/utils'
109

1110
export const PaymentStatusSuccessSchema = z.object({
1211
hash: z.string().min(1, 'Hash is required'),
@@ -33,8 +32,8 @@ export type PaymentStatusRejected = z.infer<typeof PaymentStatusRejectedSchema>
3332
// as grant is accepted, outgoing-payment will be created.
3433
app.get(
3534
'/payment/redirect/:paymentId',
36-
zValidator('param', z.object({ paymentId: PaymentIdSchema })),
37-
zValidator('query', PaymentStatusSchema),
35+
validate('param', z.object({ paymentId: PaymentIdSchema })),
36+
validate('query', PaymentStatusSchema),
3837
async ({ req, redirect, env }) => {
3938
try {
4039
const openPayments = await OpenPaymentsService.getInstance(env)

api/src/routes/payment/status2.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
import { HTTPException } from 'hono/http-exception'
22
import z from 'zod'
3-
import { zValidator } from '@hono/zod-validator'
43
import { app, type Env } from '../../app'
54
import { PaymentIdSchema } from '../../schemas/payment'
65
import { OpenPaymentsService } from '../../utils/open-payments'
76
import { getData, setData, type PaymentKvData } from '../../utils/payments-kv'
8-
import { createHTTPException } from '../../utils/utils'
7+
import { createHTTPException, validate } from '../../utils/utils'
98

109
app.get(
1110
'/payment/status/:paymentId',
12-
zValidator('param', z.object({ paymentId: PaymentIdSchema })),
11+
validate('param', z.object({ paymentId: PaymentIdSchema })),
1312
async ({ req, json, env }) => {
1413
const { paymentId } = req.param()
1514

api/src/routes/probabilistic-revshare.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
import { HTTPException } from 'hono/http-exception'
22
import type { ContentfulStatusCode } from 'hono/utils/http-status'
33
import z from 'zod'
4-
import { zValidator } from '@hono/zod-validator'
54
import type { WalletAddress } from '@interledger/open-payments'
65
import { decode, pickWeightedRandom } from '@shared/probabilistic-revenue-share'
76
import { isWalletAddress, validateWalletAddressOrPointer } from '@shared/utils'
87
import { app } from '../app.js'
9-
import { createHTTPException } from '../utils/utils'
8+
import { createHTTPException, validate } from '../utils/utils'
109

1110
app.get(
1211
'/revshare/:payload',
13-
zValidator(
12+
validate(
1413
'param',
1514
z.object({
1615
payload: z.base64url().max(50_000).min(20),

api/src/routes/wallet.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import z from 'zod'
2-
import { zValidator } from '@hono/zod-validator'
32
import type { WalletAddress } from '@interledger/open-payments'
43
import {
54
getWalletAddress,
@@ -8,7 +7,7 @@ import {
87
WalletAddressFormatError,
98
} from '@shared/utils'
109
import { app } from '../app.js'
11-
import { createHTTPException } from '../utils/utils.js'
10+
import { createHTTPException, validate } from '../utils/utils.js'
1211

1312
const walletAddressSchema = z.object({
1413
walletAddress: z.url('Wallet address must be a valid URL'),
@@ -20,7 +19,7 @@ export interface WalletAddressInfo extends WalletAddress {
2019

2120
app.get(
2221
'/wallet',
23-
zValidator('query', walletAddressSchema),
22+
validate('query', walletAddressSchema),
2423
async ({ req, json }) => {
2524
const { walletAddress } = req.valid('query')
2625

api/src/utils/utils.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
import type { ValidationTargets } from 'hono'
12
import { HTTPException } from 'hono/http-exception'
23
import type { ContentfulStatusCode } from 'hono/utils/http-status'
34
import type { Request } from 'http-message-signatures'
45
import { signMessage } from 'http-message-signatures/lib/httpbis'
56
import { createContentDigestHeader } from 'httpbis-digest-headers'
7+
import type { ZodType } from 'zod'
8+
import { zValidator } from '@hono/zod-validator'
69
import { signAsync } from '@noble/ed25519'
710

811
type Headers = SignatureHeaders & Partial<ContentHeaders>
@@ -138,3 +141,12 @@ export function createHTTPException(
138141
cause: serializedError,
139142
})
140143
}
144+
145+
export const validate = <T extends keyof ValidationTargets, S extends ZodType>(
146+
target: T,
147+
schema: S,
148+
) => {
149+
return zValidator(target, schema, (result) => {
150+
if (!result.success) throw result.error
151+
})
152+
}

0 commit comments

Comments
 (0)