AnyVali logo AnyVali

Examples

Portable schemas, shown quickly.

A few short examples across SDKs.

Example 01

Shared account contract

Define once in TypeScript, export a portable document, import it in Python, and keep the validation contract aligned.

// TypeScript
const Account = v.object({
  id: v.int64(),
  email: v.string().format("email"),
  status: v.enum(["active", "suspended"]).default("active"),
});

const document = Account.export({ mode: "portable" });
# Python
account = v.import_schema(document)
result = account.safe_parse({
    "id": 42,
    "email": "team@anyvali.dev",
})

Example 02

Coercion and defaults with explicit ordering

AnyVali defines the parse pipeline, so you do not need to guess how coercion, missing values, and defaults interact.

const Config = v.object({
  port: v.int().coerce("string").default(8080),
  secure: v.bool().coerce("string").default(false),
});

Config.parse({ port: "9000", secure: "true" });
// => { port: 9000, secure: true }
Config.parse({});
// => { port: 8080, secure: false }

Example 03

Portability boundaries

Portable features export cleanly. Local-only behavior stays local.

// portable
v.string().minLength(3).format("email")
v.int().min(0).max(100)
v.object({ role: v.enum(["admin", "user"]) })
// local-only
v.string().custom((value) => value.startsWith("usr_"))
v.string().default(() => crypto.randomUUID())

Docs

Go deeper in the reference docs

For the full spec, JSON format, portability rules, and SDK guidance, use the docs.