@macalinao/zod-solana
Version:
Zod schemas for Solana types with @solana/kit integration
23 lines (22 loc) • 775 B
JavaScript
import { u8Schema } from "./u8-schema.js";
import * as z from "zod";
//#region src/uint8array-schema.ts
/**
* A Zod schema for Uint8Array.
* Accepts an array of numbers and transforms it to a Uint8Array.
*/
const uint8ArraySchema = z.array(u8Schema).transform((arr) => new Uint8Array(arr));
/**
* A Zod schema for JSON-encoded Uint8Array.
* Accepts a JSON string, parses it, and transforms to Uint8Array.
*/
const jsonUint8ArraySchema = z.string().transform((str) => {
try {
return JSON.parse(str);
} catch (error) {
throw new Error(`Invalid JSON: ${error instanceof Error ? error.message : "Unknown error"}`, { cause: error });
}
}).pipe(uint8ArraySchema);
//#endregion
export { jsonUint8ArraySchema, uint8ArraySchema };
//# sourceMappingURL=uint8array-schema.js.map