From b505c12fe945dac443ad789731088885d0309919 Mon Sep 17 00:00:00 2001 From: Max Holman Date: Thu, 9 Apr 2026 16:28:23 +0700 Subject: [PATCH] fix: generate v.null() for type: "null" schemas in valibot Previously type: "null" (valid in OAS 3.1 / JSON Schema 2020-12) fell through to v.unknown(). Common in oneOf patterns like oneOf: [{ type: "string" }, { type: "null" }] for nullable fields. Co-Authored-By: Claude Opus 4.6 (1M context) --- .../__snapshots__/nullables.test.ts.snap | 7 +++++++ __tests__/nullables.test.ts | 20 +++++++++++++++++++ lib/valibot.ts | 4 ++++ 3 files changed, 31 insertions(+) diff --git a/__tests__/__snapshots__/nullables.test.ts.snap b/__tests__/__snapshots__/nullables.test.ts.snap index 77e58ed..5baf5e5 100644 --- a/__tests__/__snapshots__/nullables.test.ts.snap +++ b/__tests__/__snapshots__/nullables.test.ts.snap @@ -75,6 +75,13 @@ exports[`nullables 1`] = ` " `; +exports[`oneOf with type null generates v.null() 1`] = ` +"import * as v from "valibot"; + +export const nullableImageSchema = v.union([v.string(), v.null()]); +" +`; + exports[`query and header integer params coerce strings to numbers 1`] = ` "export type Dummy = string; export type ExpireTime = number; diff --git a/__tests__/nullables.test.ts b/__tests__/nullables.test.ts index ca16aef..265bc8e 100644 --- a/__tests__/nullables.test.ts +++ b/__tests__/nullables.test.ts @@ -103,6 +103,26 @@ test("const values", async () => { expect(result.valibotFile?.getText()).toMatchSnapshot(); }); +test("oneOf with type null generates v.null()", async () => { + const result = await processOpenApiDocument( + "/tmp/like-you-know-whatever", + { + openapi: "3.1.0", + info: { title: "Test", version: "1.0.0" }, + paths: {}, + components: { + schemas: { + NullableImage: { + oneOf: [{ type: "string", format: "uri" }, { type: "null" }], + }, + }, + }, + }, + ); + + expect(result.valibotFile.getText()).toMatchSnapshot(); +}); + test("query and header integer params coerce strings to numbers", async () => { const schema: oas31.OpenAPIObject = { openapi: "3.1.0", diff --git a/lib/valibot.ts b/lib/valibot.ts index a558305..0c6372c 100644 --- a/lib/valibot.ts +++ b/lib/valibot.ts @@ -284,6 +284,10 @@ export function schemaToValidator( return maybeNullable(vcall("strictObject", strictObjectWriter), isNullable); } + if (schema.type === "null") { + return vcall("null"); + } + return vcall("unknown"); }