Model Validation and Serialization

Our TypeScript SDKs support model validation and serialization backed by Zod. This feature allows you to validate data against your models and easily serialize or deserialize data to and from JSON.

Overview

For example, given the following model definition:

// src/models/components/book.ts
import * as z from "zod";
export type Book = {
/**
* The unique identifier for the book
*/
id: string;
/**
* The title of the book
*/
title: string;
/**
* The author of the book
*/
author: string;
};

From JSON

import { bookFromJSON, bookToJSON } from "my-sdk/models/components/book";
const result = bookFromJSON('{"id":"1","title":"1984","author":"George Orwell"}');
if (result.ok) {
console.log(result.value);
// 👆 result.value is of type `Book`
} else {
// Handle validation errors
console.error(result.error);
}

To JSON

const jsonString = bookToJSON({ id: "1", title: "1984", author: "George Orwell" });
// jsonString is of type `string`