import { describe, it, expect } from "vitest"; import { z } from "zod"; import { toJSONSchema } from "../utils.js"; // Datetime pattern that toJSONSchema injects for z.string().datetime() fields // Accepts ISO 8601 UTC (2024-01-15T14:30:00Z) and SQL-style (2024-01-15 14:30:00) const testPattern = (pattern: string, value: string) => new RegExp(pattern).test(value); describe("toJSONSchema", () => { describe("basic type conversion", () => { it("should convert z.string() to JSON Schema string type", () => { const schema = z.object({ name: z.string() }); const result = toJSONSchema(schema); expect(result.properties).toMatchObject({ name: { type: "string" } }); }); it("should convert z.number() to JSON Schema number type", () => { const schema = z.object({ price: z.number() }); const result = toJSONSchema(schema); expect(result.properties).toMatchObject({ price: { type: "number" } }); }); it("should convert z.boolean() to JSON Schema boolean type", () => { const schema = z.object({ active: z.boolean() }); const result = toJSONSchema(schema); expect(result.properties).toMatchObject({ active: { type: "boolean" } }); }); it("should convert z.array() to JSON Schema array type", () => { const schema = z.object({ tags: z.array(z.string()) }); const result = toJSONSchema(schema); expect(result.properties).toMatchObject({ tags: { type: "array" } }); }); it("should mark required fields in JSON Schema", () => { const schema = z.object({ required: z.string(), optional: z.string().optional(), }); const result = toJSONSchema(schema); expect(result.required).toContain("required"); expect(result.required).not.toContain("optional"); }); }); describe("datetime pattern override", () => { it("should inject a custom pattern for z.string().datetime() fields", () => { const schema = z.object({ createdAt: z.string().datetime() }); const result = toJSONSchema(schema); const prop = (result.properties as Record) .createdAt; expect(prop.type).toBe("string"); expect(prop.format).toBe("date-time"); expect(prop.pattern).toBeDefined(); }); it("should NOT inject a pattern for regular z.string() fields", () => { const schema = z.object({ name: z.string() }); const result = toJSONSchema(schema); const prop = (result.properties as Record).name; expect(prop.pattern).toBeUndefined(); }); it("should inject the pattern for optional z.string().datetime() fields", () => { const schema = z.object({ publishedAt: z.string().datetime().optional() }); const result = toJSONSchema(schema); const prop = (result.properties as Record).publishedAt; expect(prop.pattern).toBeDefined(); }); it("should inject the pattern for nested datetime fields", () => { const schema = z.object({ meta: z.object({ updatedAt: z.string().datetime() }), }); const result = toJSONSchema(schema); const meta = (result.properties as Record }>) .meta; expect(meta.properties.updatedAt.pattern).toBeDefined(); }); describe("custom datetime pattern accepts valid values", () => { let pattern: string; it("setup: retrieves the injected pattern", () => { const schema = z.object({ ts: z.string().datetime() }); const result = toJSONSchema(schema); pattern = (result.properties as Record).ts.pattern; expect(pattern).toBeTruthy(); }); const validISO8601UTC = [ "2024-01-15T14:30:00Z", "2024-01-15T14:30Z", "2024-01-15T00:00:00Z", "2024-01-15T23:59:59Z", "2024-01-15T14:30:00.123Z", "2024-02-29T12:00:00Z", // leap year "2000-02-29T12:00:00Z", // century leap year ]; const validSQLStyle = [ "2024-01-15 14:30:00", "2024-01-15 00:00:00", "2024-01-15 23:59:59", "2024-02-29 12:00:00", // leap year ]; const invalidValues = [ "2024-01-15", // date only "14:30:00", // time only "2024-01-15T14:30:00", // ISO without Z "2024-01-15T14:30:00+02:00", // ISO with offset (not UTC) "2024-13-01T00:00:00Z", // invalid month "2024-01-32T00:00:00Z", // invalid day "2023-02-29T00:00:00Z", // Feb 29 on non-leap year "not-a-date", "", ]; it.each(validISO8601UTC)("accepts ISO 8601 UTC: %s", (value) => { const schema = z.object({ ts: z.string().datetime() }); const result = toJSONSchema(schema); const pat = (result.properties as Record).ts.pattern; expect(testPattern(pat, value)).toBe(true); }); it.each(validSQLStyle)("accepts SQL-style datetime: %s", (value) => { const schema = z.object({ ts: z.string().datetime() }); const result = toJSONSchema(schema); const pat = (result.properties as Record).ts.pattern; expect(testPattern(pat, value)).toBe(true); }); it.each(invalidValues)("rejects invalid value: %s", (value) => { const schema = z.object({ ts: z.string().datetime() }); const result = toJSONSchema(schema); const pat = (result.properties as Record).ts.pattern; expect(testPattern(pat, value)).toBe(false); }); }); }); describe("caller override option", () => { it("should call the caller's override function", () => { let overrideCalled = false; const schema = z.object({ name: z.string() }); toJSONSchema(schema, { override(ctx) { overrideCalled = true; void ctx; }, }); expect(overrideCalled).toBe(true); }); it("should apply the caller's override before the datetime pattern", () => { const schema = z.object({ ts: z.string().datetime() }); // The caller sets a custom description; the datetime pattern should still be applied const result = toJSONSchema(schema, { override(ctx) { (ctx.jsonSchema as Record).description = "A timestamp"; }, }); const prop = (result.properties as Record).ts; expect(prop.description).toBe("A timestamp"); expect(prop.pattern).toBeDefined(); // datetime override is applied after caller's override }); it("should allow caller to override the datetime pattern itself", () => { const schema = z.object({ ts: z.string().datetime() }); const customPattern = "^\\d{4}-\\d{2}-\\d{2}$"; const result = toJSONSchema(schema, { override(ctx) { const js = ctx.jsonSchema as Record; if (js.type === "string" && js.format === "date-time") { js.pattern = customPattern; } }, }); const prop = (result.properties as Record).ts; // The built-in override runs after and overwrites caller's pattern expect(prop.pattern).not.toBe(customPattern); expect(prop.pattern).toBeDefined(); }); it("should work without any options", () => { const schema = z.object({ name: z.string() }); expect(() => toJSONSchema(schema)).not.toThrow(); }); it("should include $schema by default (options are passed through)", () => { const schema = z.string(); const result = toJSONSchema(schema) as Record; expect(typeof result.$schema).toBe("string"); expect(result.$schema as string).toMatch(/json-schema\.org/); }); it("should omit $schema when target is jsonSchema7", () => { const schema = z.string(); const result = toJSONSchema(schema, { target: "jsonSchema7" }) as Record; // With target "jsonSchema7", Zod omits the $schema property expect(result.$schema).toBeUndefined(); }); }); describe("multiple datetime fields", () => { it("should inject the pattern on all datetime fields in an object", () => { const schema = z.object({ createdAt: z.string().datetime(), updatedAt: z.string().datetime(), name: z.string(), }); const result = toJSONSchema(schema); const props = result.properties as Record; expect(props.createdAt.pattern).toBeDefined(); expect(props.updatedAt.pattern).toBeDefined(); expect(props.name.pattern).toBeUndefined(); }); }); });