Skip to content

Commit 890f5a0

Browse files
committed
Don't allow a schema to be nullable more than once
1 parent b6fbbd3 commit 890f5a0

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

src/jsonSchema/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ export function toJSONSchema(inferredSchema: InferredSchema): SchemaBuilder<Sche
88
.with({ type: "unknown" }, () => s.$false()) // This should never be reached
99
.with({ type: "boolean" }, () => s.boolean())
1010
.with({ type: "nullable" }, ({ schema }) =>
11-
schema.type == "unknown" ? s.nil() : s.nullable(toJSONSchema(schema)),
11+
schema.type == "unknown"
12+
? s.nil()
13+
: schema.type === "nullable"
14+
? toJSONSchema(schema)
15+
: s.nullable(toJSONSchema(schema)),
1216
)
1317
.with({ type: "int" }, () => {
1418
return s.integer();

tests/jsonSchema.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,17 @@ describe("nullables", () => {
339339
},
340340
});
341341
});
342+
343+
test("infers just a single null type if multiple nulls are found", () => {
344+
const schema = inferSchema([1, 2, null, 3, null, 4]);
345+
346+
expect(toSchema(schema)).toStrictEqual({
347+
type: "array",
348+
items: {
349+
type: ["integer", "null"],
350+
},
351+
});
352+
});
342353
});
343354

344355
describe("real world tests", () => {

0 commit comments

Comments
 (0)