@macalinao/zod-solana
Version:
Zod schemas for Solana types with @solana/kit integration
29 lines (26 loc) • 722 B
text/typescript
import { z } from "zod";
import { u8Schema } from "./u8-schema.js";
/**
* A Zod schema for Uint8Array.
* Accepts an array of numbers and transforms it to a Uint8Array.
*/
export 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.
*/
export const jsonUint8ArraySchema = z
.string()
.transform((str) => {
try {
const parsed: unknown = JSON.parse(str);
return parsed;
} catch (error) {
throw new Error(
`Invalid JSON: ${error instanceof Error ? error.message : "Unknown error"}`,
);
}
})
.pipe(uint8ArraySchema);