Schema validation

Often times, your API Definition may drift from your server implementation. If you are concerned about this, you can toggle on schema validation in the SDKs.

It’s worth nothing that this feature is optional for duck-typed languages like Python, TypeScript, and Ruby. However, for strongly typed languages like Go, Java, and C# this is always on.

The TypeScript SDK contains a serialization folder when generated.

2 api/
3 core/
4 errors/
5 serialization/
6 Client.ts
7 index.ts

Inside of this serialization folder, are zod-like schemas for every generated model.

For example, if you had a Movie schema defined in your API Definition and wanted to validate the schema, you could simply do:

1import { Movie } from "@my-company/sdk";
2import * as serializers from "@my-company/sdk/serialization";
4const movie: Movie = {
5 name: "GoodwillHunting";
7// serialize
9// deserialize
10serializers.Movie.parseOrThrow("{ \"name\": \"Goodwill Hunting\" }");

Fern allows you to generate the SDK in three different modes:

  1. No validation: In this mode, there is no serialization layer generated. Types exist at compile time, but they are completely removed at runtime.
  2. Request validation only: In this mode, the serialization layer is only used to validate requests. Responses are always returned even if they do not match the schema. This is the recommended mode for public API companies.
  3. Full validation: In this mode, the serialization layer is used to validate both requests and responses. This is the recommended mode for internal SDKs so that you can catch issues as soon as possible.