|
| 1 | +import assert from "node:assert/strict"; |
| 2 | +import { it } from "node:test"; |
| 3 | +import { toJSONSchema } from "zod/v4/core"; |
| 4 | +import { describeMatrix } from "./matrix.js"; |
| 5 | + |
| 6 | +describeMatrix("OffsetDateTime", (zt, z) => { |
| 7 | + it("should allow ISO offset date time value", () => { |
| 8 | + const schema = zt.instant(); |
| 9 | + const instant = Temporal.Instant.from("2021-01-01T20:00:00Z"); |
| 10 | + const result = schema.parse("2021-01-01T20:00:00Z"); |
| 11 | + assert(result.equals(instant)); |
| 12 | + }); |
| 13 | + |
| 14 | + it("should report invalid date", () => { |
| 15 | + const schema = zt.instant(); |
| 16 | + assert.throws(() => schema.parse("foo")); |
| 17 | + }); |
| 18 | + |
| 19 | + it("should report invalid type", () => { |
| 20 | + const schema = zt.instant(); |
| 21 | + assert.throws(() => schema.parse(1)); |
| 22 | + }); |
| 23 | + |
| 24 | + it("should allow error override", () => { |
| 25 | + const schema = zt.instant({ error: "whoops" }); |
| 26 | + const result = schema.safeParse("a"); |
| 27 | + assert(!result.success); |
| 28 | + assert.deepEqual(result.error.issues[0].message, "whoops"); |
| 29 | + }); |
| 30 | + |
| 31 | + it("should create a standard JSON schema", () => { |
| 32 | + const schema = zt.instant(); |
| 33 | + const jsonSchema = toJSONSchema(schema, { io: "input" }); |
| 34 | + assert.partialDeepStrictEqual(jsonSchema, { |
| 35 | + type: "string", |
| 36 | + format: "date-time", |
| 37 | + example: "2020-01-01T14:00:00Z", |
| 38 | + }); |
| 39 | + }); |
| 40 | + |
| 41 | + it("should encode to ISO offset date time", () => { |
| 42 | + const schema = zt.instant(); |
| 43 | + const result = z.encode(schema, Temporal.Instant.from("2021-01-01T20:00:00Z")); |
| 44 | + assert.equal(result, "2021-01-01T20:00:00Z"); |
| 45 | + }); |
| 46 | + |
| 47 | + it("should preserve JSON schema over refine", () => { |
| 48 | + const schema = zt.instant().check(z.refine(() => true)); |
| 49 | + const jsonSchema = toJSONSchema(schema, { io: "input" }); |
| 50 | + assert.partialDeepStrictEqual(jsonSchema, { |
| 51 | + type: "string", |
| 52 | + format: "date-time", |
| 53 | + example: "2020-01-01T14:00:00Z", |
| 54 | + }); |
| 55 | + }); |
| 56 | +}); |
0 commit comments