File tree Expand file tree Collapse file tree 4 files changed +25
-25
lines changed
Expand file tree Collapse file tree 4 files changed +25
-25
lines changed Original file line number Diff line number Diff line change 1- import { z } from "zod" ;
1+ import { ZodError , type ZodSchema , z } from "zod" ;
2+
3+ /**
4+ * Validates the given value against the provided Zod schema.
5+ * @param schema The Zod schema to validate against.
6+ * @param value The value to validate.
7+ * @returns The validated and parsed data if successful.
8+ * @throws {Error } If validation fails, with a formatted error message.
9+ */
10+ export function validate < T extends ZodSchema > ( schema : T , value : unknown ) : T [ "_output" ] {
11+ try {
12+ return schema . parse ( value ) ;
13+ } catch ( error ) {
14+ if ( error instanceof ZodError ) {
15+ const formattedErrors = error . errors . map ( ( err ) => `- ${ err . path . join ( "." ) } : ${ err . message } ` ) . join ( "\n" ) ;
16+ throw new Error ( `Validation error(s):\n${ formattedErrors } ` ) ;
17+ }
18+ throw error ; // Re-throw if it's not a ZodError
19+ }
20+ }
221
322export const AccessTokenSchema = z . string ( ) . regex ( / ^ b t p _ p a t _ .* | b t p _ a a t _ .* $ / ) ;
423
Load Diff This file was deleted.
Original file line number Diff line number Diff line change @@ -25,8 +25,8 @@ import {
2525 type UniqueName ,
2626 type WorkspaceReturnValue ,
2727 WorkspaceReturnValueSchema ,
28+ validate ,
2829} from "@/schemas/schemas" ;
29- import { validate } from "@/schemas/validator" ;
3030import { type ZodSchema , z } from "zod" ;
3131
3232/**
@@ -211,6 +211,3 @@ export function createSettleMintClient() {
211211 ) ,
212212 } ;
213213}
214-
215- export * as schemas from "./schemas/schemas" ;
216- export * as schemaValidator from "./schemas/validator" ;
Original file line number Diff line number Diff line change @@ -17,5 +17,9 @@ export default defineConfig(({ watch }) => {
1717 entry : [ "src/settlemint.ts" ] ,
1818 ...sharedConfig ,
1919 } ,
20+ {
21+ entry : [ "src/schemas/schemas.ts" ] ,
22+ ...sharedConfig ,
23+ } ,
2024 ] ;
2125} ) ;
You can’t perform that action at this time.
0 commit comments