Codecs

You can use Zod z.codec() schemas to store arbitrary data types such as class instances within CoValues by defining custom encoders. This allows you to directly use these data types within CoValues without having to do an extra manual conversion step.

Using Zod codecs

To use a Zod z.codec() with Jazz, your encoder must encode the data into a JSON-compatible format. This is means that the Input type shall map to the JSON-compatible type, and Output will map to your custom type.

class class GreeterGreeter {
  constructor(public Greeter.name: stringname: string) {}

  Greeter.greet(): voidgreet() {
    var console: Console
The `console` module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers. The module exports two specific components: * A `Console` class with methods such as `console.log()`, `console.error()` and `console.warn()` that can be used to write to any Node.js stream. * A global `console` instance configured to write to [`process.stdout`](https://nodejs.org/docs/latest-v20.x/api/process.html#processstdout) and [`process.stderr`](https://nodejs.org/docs/latest-v20.x/api/process.html#processstderr). The global `console` can be used without importing the `node:console` module. _**Warning**_: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the [`note on process I/O`](https://nodejs.org/docs/latest-v20.x/api/process.html#a-note-on-process-io) for more information. Example using the global `console`: ```js console.log('hello world'); // Prints: hello world, to stdout console.log('hello %s', 'world'); // Prints: hello world, to stdout console.error(new Error('Whoops, something bad happened')); // Prints error message and stack trace to stderr: // Error: Whoops, something bad happened // at [eval]:5:15 // at Script.runInThisContext (node:vm:132:18) // at Object.runInThisContext (node:vm:309:38) // at node:internal/process/execution:77:19 // at [eval]-wrapper:6:22 // at evalScript (node:internal/process/execution:76:60) // at node:internal/main/eval_string:23:3 const name = 'Will Robinson'; console.warn(`Danger ${name}! Danger!`); // Prints: Danger Will Robinson! Danger!, to stderr ``` Example using the `Console` class: ```js const out = getStreamSomehow(); const err = getStreamSomehow(); const myConsole = new console.Console(out, err); myConsole.log('hello world'); // Prints: hello world, to out myConsole.log('hello %s', 'world'); // Prints: hello world, to out myConsole.error(new Error('Whoops, something bad happened')); // Prints: [Error: Whoops, something bad happened], to err const name = 'Will Robinson'; myConsole.warn(`Danger ${name}! Danger!`); // Prints: Danger Will Robinson! Danger!, to err ```
@see[source](https://github.com/nodejs/node/blob/v20.11.1/lib/console.js)
console
.Console.log(message?: any, ...optionalParams: any[]): void (+2 overloads)
[MDN Reference](https://developer.mozilla.org/docs/Web/API/console/log_static)
log
(`Hello, ${this.Greeter.name: stringname}!`);
} } const
const schema: co.Map<{
    greeter: z.z.ZodCodec<z.z.ZodString, z.z.ZodCustom<Greeter, Greeter>>;
}, unknown, Account | Group, true>
schema
= import coco.
map<{
    greeter: z.z.ZodCodec<z.z.ZodString, z.z.ZodCustom<Greeter, Greeter>>;
}>(shape: {
    greeter: z.z.ZodCodec<z.z.ZodString, z.z.ZodCustom<Greeter, Greeter>>;
}): co.Map<...>
export map
map
({
greeter: z.z.ZodCodec<z.z.ZodString, z.z.ZodCustom<Greeter, Greeter>>greeter: import zz.
codec<z.z.ZodString, z.z.ZodCustom<Greeter, Greeter>>(in_: z.z.ZodString, out: z.z.ZodCustom<Greeter, Greeter>, params: {
    decode: (value: string, payload: z.z.core.ParsePayload<...>) => z.z.core.util.MaybeAsync<...>;
    encode: (value: Greeter, payload: z.z.core.ParsePayload<...>) => z.z.core.util.MaybeAsync<...>;
}): z.z.ZodCodec<...>
export codec
codec
(import zz.
function string(params?: string | z.z.core.$ZodStringParams): z.z.ZodString (+1 overload)
export string
string
(), import zz.export zz.
instanceof<typeof Greeter>(cls: typeof Greeter, params?: {
    when?: ((payload: z.z.core.ParsePayload) => boolean) | undefined;
    error?: string | z.z.core.$ZodErrorMap<z.z.core.$ZodIssueCustom> | undefined;
    message?: string | undefined;
}): z.z.ZodCustom<...>
export instanceof
instanceof
(class GreeterGreeter), {
encode: (value: Greeter, payload: z.z.core.ParsePayload<Greeter>) => z.z.core.util.MaybeAsync<string>encode: (value: Greetervalue) => value: Greetervalue.Greeter.name: stringname, decode: (value: string, payload: z.z.core.ParsePayload<string>) => z.z.core.util.MaybeAsync<Greeter>decode: (value: stringvalue) => new constructor Greeter(name: string): GreeterGreeter(value: stringvalue), }), }); const
const porter: {
    readonly greeter: Greeter;
} & CoMap
porter
=
const schema: co.Map<{
    greeter: z.z.ZodCodec<z.z.ZodString, z.z.ZodCustom<Greeter, Greeter>>;
}, unknown, Account | Group, true>
schema
.
CoMapSchema<{ greeter: ZodCodec<ZodString, ZodCustom<Greeter, Greeter>>; }, unknown, Account | Group, true>.create(init: {
    greeter: Greeter;
}, options?: {
    owner?: Group;
    unique?: CoValueUniqueness["uniqueness"];
} | Group): {
    ...;
} & CoMap (+1 overload)
create
({
greeter: Greetergreeter: new constructor Greeter(name: string): GreeterGreeter("Alice"), });
const porter: {
    readonly greeter: Greeter;
} & CoMap
porter
.greeter: Greetergreeter.Greeter.greet(): voidgreet();

Schemas that are not directly supported by Jazz such as z.instanceof are not re-exported by Jazz under the z object. The full Zod API is exported under z.z if you need to use any of these schemas as part of a codec.