-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Do request validation properly with zod
- Loading branch information
1 parent
578e1a0
commit 4cc093d
Showing
5 changed files
with
48 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
.../backend-credit-card-enrollment/backend-typescript/src/common/util/ParseWithValidation.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import {z} from 'zod'; | ||
|
||
export class ValidationError extends Error { | ||
constructor( | ||
public readonly errors: z.ZodError | ||
) { | ||
super('Validation failed'); | ||
this.name = 'ValidationError'; | ||
} | ||
} | ||
|
||
export function parseWithValidation<T>(data: unknown, schema?: z.ZodType<T>): T { | ||
if (!schema) { | ||
throw new Error('Schema must be provided'); | ||
} | ||
|
||
try { | ||
return schema.parse(data); | ||
} catch (error) { | ||
if (error instanceof z.ZodError) { | ||
throw new ValidationError(error); | ||
} | ||
throw error; | ||
} | ||
} |
6 changes: 0 additions & 6 deletions
6
...ion/backend-credit-card-enrollment/backend-typescript/src/common/util/TypeSafeCoercion.ts
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 8 additions & 4 deletions
12
...ment/backend-typescript/src/creditCard/enrollment/command/RequestEnrollmentHttpRequest.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,8 @@ | ||
export interface RequestEnrollmentHttpRequest { | ||
productId: string; | ||
annualIncomeInCents: number; | ||
} | ||
import { z } from 'zod'; | ||
|
||
export const requestEnrollmentHttpRequestSchema = z.object({ | ||
productId: z.string(), | ||
annualIncomeInCents: z.number().min(0, "Annual income cannot be negative").max(1_000_000_000, "Annual income is too high") | ||
}); | ||
|
||
export type RequestEnrollmentHttpRequest = z.infer<typeof requestEnrollmentHttpRequestSchema>; |