Skip to content

Commit f6ad2cc

Browse files
authored
Merge pull request #16 from maxholman/fix/valibot-type-null
fix: generate v.null() for type: "null" schemas in valibot
2 parents e58864d + b505c12 commit f6ad2cc

3 files changed

Lines changed: 31 additions & 0 deletions

File tree

__tests__/__snapshots__/nullables.test.ts.snap

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,13 @@ exports[`nullables 1`] = `
7575
"
7676
`;
7777

78+
exports[`oneOf with type null generates v.null() 1`] = `
79+
"import * as v from "valibot";
80+
81+
export const nullableImageSchema = v.union([v.string(), v.null()]);
82+
"
83+
`;
84+
7885
exports[`query and header integer params coerce strings to numbers 1`] = `
7986
"export type Dummy = string;
8087
export type ExpireTime = number;

__tests__/nullables.test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,26 @@ test("const values", async () => {
103103
expect(result.valibotFile?.getText()).toMatchSnapshot();
104104
});
105105

106+
test("oneOf with type null generates v.null()", async () => {
107+
const result = await processOpenApiDocument(
108+
"/tmp/like-you-know-whatever",
109+
{
110+
openapi: "3.1.0",
111+
info: { title: "Test", version: "1.0.0" },
112+
paths: {},
113+
components: {
114+
schemas: {
115+
NullableImage: {
116+
oneOf: [{ type: "string", format: "uri" }, { type: "null" }],
117+
},
118+
},
119+
},
120+
},
121+
);
122+
123+
expect(result.valibotFile.getText()).toMatchSnapshot();
124+
});
125+
106126
test("query and header integer params coerce strings to numbers", async () => {
107127
const schema: oas31.OpenAPIObject = {
108128
openapi: "3.1.0",

lib/valibot.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,10 @@ export function schemaToValidator(
284284
return maybeNullable(vcall("strictObject", strictObjectWriter), isNullable);
285285
}
286286

287+
if (schema.type === "null") {
288+
return vcall("null");
289+
}
290+
287291
return vcall("unknown");
288292
}
289293

0 commit comments

Comments
 (0)