Skip to content

Commit

Permalink
fix: return appropriate FC error on validation failure (#157)
Browse files Browse the repository at this point in the history
  • Loading branch information
satish-ravi authored Aug 25, 2023
1 parent 3f36cbd commit 8217a36
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/middleware/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const errorToStatusCode = (
) => {
if (error instanceof ValidationError) {
res.status(400).json({
error: error.message,
error: error.fiatConnectError,
data: error.validationError,
})
} else if (
Expand Down
7 changes: 6 additions & 1 deletion src/routes/accounts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
} from '../types'
import { siweAuthMiddleware } from '../middleware/authenticate'
import {
FiatConnectError,
deleteFiatAccountRequestParamsSchema,
postFiatAccountRequestBodySchema,
} from '@fiatconnect/fiatconnect-types'
Expand All @@ -27,7 +28,11 @@ export function accountsRouter({
_res: express.Response,
next: express.NextFunction,
) => {
validateZodSchema(req.body, postFiatAccountRequestBodySchema)
validateZodSchema(
req.body,
postFiatAccountRequestBodySchema,
FiatConnectError.InvalidSchema,
)
next()
}

Expand Down
7 changes: 6 additions & 1 deletion src/routes/kyc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import express from 'express'
import { asyncRoute } from './async-route'
import { validateZodSchema } from '../schema/'
import {
FiatConnectError,
KycRequestParams,
KycSchemas,
NotImplementedError,
Expand Down Expand Up @@ -55,7 +56,11 @@ export function kycRouter({
_res: express.Response,
) => {
// Delegate to type-specific handlers after validation provides type guards
validateZodSchema(req.body, kycSchemaToZodSchema[req.params.kycSchema])
validateZodSchema(
req.body,
kycSchemaToZodSchema[req.params.kycSchema],
FiatConnectError.InvalidParameters,
)

throw new NotImplementedError('POST /kyc/:kycSchema not implemented')
},
Expand Down
4 changes: 3 additions & 1 deletion src/schema/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ValidationError } from '../types'
import { FiatConnectError, ValidationError } from '../types'

import { z, ZodError } from 'zod'
import { ZodType } from 'zod/lib/types'
Expand All @@ -12,6 +12,7 @@ import { ZodType } from 'zod/lib/types'
export function validateZodSchema<T extends ZodType>(
obj: any,
schema: T,
fiatConnectError: FiatConnectError = FiatConnectError.InvalidParameters,
): z.infer<T> {
try {
return schema.parse(obj)
Expand All @@ -20,6 +21,7 @@ export function validateZodSchema<T extends ZodType>(
throw new ValidationError(
`Error validating object with schema ${schema.description}`,
err.issues,
fiatConnectError,
)
}
throw err
Expand Down
8 changes: 7 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,15 @@ export enum Network {

export class ValidationError extends Error {
validationError: any
constructor(msg: string, validationError: any) {
fiatConnectError: FiatConnectError
constructor(
msg: string,
validationError: any,
fiatConnectError: FiatConnectError,
) {
super(msg)
this.validationError = validationError
this.fiatConnectError = fiatConnectError
}
}

Expand Down

0 comments on commit 8217a36

Please sign in to comment.