Skip to content

Instantly share code, notes, and snippets.

@kevinmichaelchen
Last active October 24, 2025 19:25
Show Gist options
  • Select an option

  • Save kevinmichaelchen/2aafe5c9ea63b11595e920f0bf9746ce to your computer and use it in GitHub Desktop.

Select an option

Save kevinmichaelchen/2aafe5c9ea63b11595e920f0bf9746ce to your computer and use it in GitHub Desktop.
Temporal Zod Deno Demo - Demonstrating Temporal.Instant validation and coercion

Temporal Zod Deno Demo

A demonstration of using temporal-polyfill and temporal-zod in a Deno TypeScript project, focusing on Temporal.Instant validation and coercion.

What This Demonstrates

This project shows how temporal-zod schemas can accept either:

  • A Temporal.Instant instance
  • An ISO 8601 timestamp string

...and automatically coerce both to a Temporal.Instant object.

Prerequisites

  • Deno installed on your system

Running the Code

deno test
{
"imports": {
"temporal-polyfill": "npm:[email protected]",
"temporal-zod": "npm:[email protected]",
"zod": "npm:[email protected]"
}
}
import { describe, it, expect } from "vitest";
import { Temporal } from "temporal-polyfill";
import { z } from "zod";
import { zInstant } from "temporal-zod";
const EventSchema = z.object({
name: z.string(),
timestamp: zInstant,
});
type Event = z.infer<typeof EventSchema>;
function createEventWithInstant(): Event {
return EventSchema.parse({
name: "System Boot",
timestamp: Temporal.Now.instant(),
});
}
function createEventWithISOString(): Event {
return EventSchema.parse({
name: "User Login",
timestamp: "2025-10-24T15:30:00Z",
});
}
describe("Event creation with Temporal.Instant", () => {
it("should create event with Temporal.Instant", () => {
const event = createEventWithInstant();
expect(event.name).toBe("System Boot");
expect(event.timestamp).toBeInstanceOf(Temporal.Instant);
expect(event.timestamp.toString()).toMatch(/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d+)?Z$/);
});
it("should have correct type for timestamp", () => {
const event = createEventWithInstant();
expect(event.timestamp.constructor.name).toBe("Instant");
});
});
describe("Event creation with ISO String", () => {
it("should create event with ISO string and parse to Temporal.Instant", () => {
const event = createEventWithISOString();
expect(event.name).toBe("User Login");
expect(event.timestamp).toBeInstanceOf(Temporal.Instant);
expect(event.timestamp.toString()).toBe("2025-10-24T15:30:00Z");
});
it("should have correct type for timestamp", () => {
const event = createEventWithISOString();
expect(event.timestamp.constructor.name).toBe("Instant");
});
});
@kevinmichaelchen
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment