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"); }