Skip to content

Commit 6311f4c

Browse files
committed
feat: expose schemas
1 parent 71f0844 commit 6311f4c

File tree

4 files changed

+25
-25
lines changed

4 files changed

+25
-25
lines changed

packages/js/src/schemas/schemas.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,23 @@
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

322
export const AccessTokenSchema = z.string().regex(/^btp_pat_.*|btp_aat_.*$/);
423

packages/js/src/schemas/validator.ts

Lines changed: 0 additions & 20 deletions
This file was deleted.

packages/js/src/settlemint.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff 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";
3030
import { 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";

packages/js/tsup.config.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff 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
});

0 commit comments

Comments
 (0)