Model validation and serialization
Speakeasy TypeScript SDKs support model validation and serialization backed by Zod. This feature enables validation of data against models and easy serialization or deserialization of data to and from JSON.
Example
As an example, consider the following model definition:
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`Last updated on