Skip to content

Commit 72b44d4

Browse files
committed
Basic default support.
1 parent b3f2917 commit 72b44d4

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

src/core/converter.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import { ConstComplexHandler } from "../handlers/refinement/constComplex";
2525
import { MetadataHandler } from "../handlers/refinement/metadata";
2626
import { ProtoRequiredHandler } from "../handlers/refinement/protoRequired";
2727
import { ContainsHandler } from "../handlers/refinement/contains";
28+
import { DefaultHandler } from "../handlers/refinement/default";
2829

2930
// Initialize handlers
3031
const primitiveHandlers: PrimitiveHandler[] = [
@@ -67,6 +68,7 @@ const refinementHandlers: RefinementHandler[] = [
6768
new ProtoRequiredHandler(),
6869
new EnumComplexHandler(),
6970
new ConstComplexHandler(),
71+
new DefaultHandler(),
7072

7173
// Logical combinations
7274
new AllOfHandler(),

src/handlers/refinement/default.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { z } from "zod/v4";
2+
import type { JSONSchema } from "zod/v4/core";
3+
import { RefinementHandler } from "../../core/types";
4+
import { deepEqual } from "../../core/utils";
5+
6+
export class DefaultHandler implements RefinementHandler {
7+
apply(zodSchema: z.ZodTypeAny, schema: JSONSchema.BaseSchema): z.ZodTypeAny {
8+
if (!schema.default) return zodSchema;
9+
10+
return zodSchema.default(schema.default);
11+
}
12+
}

src/index.test.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1217,4 +1217,40 @@ describe("jsonSchemaObjectToZodRawShape", () => {
12171217
}),
12181218
).not.toThrow();
12191219
});
1220+
1221+
it("schema with defaults should parse empty objects", () => {
1222+
const jsonSchema = {
1223+
type: "object",
1224+
properties: {
1225+
field1: { type: "string", default: "test" },
1226+
field2: { type: "number", default: 42 },
1227+
field3: { type: "boolean", default: true },
1228+
field4: { type: "string", enum: ["test1", "test2"], default: "test2" },
1229+
field5: {
1230+
type: "object",
1231+
default: { name: "test", age: 10, isActive: true },
1232+
properties: {
1233+
name: { type: "string" },
1234+
age: { type: "integer", minimum: -43, maximum: 120 },
1235+
isActive: { type: "boolean" },
1236+
},
1237+
required: ["name", "age", "isActive"],
1238+
}
1239+
},
1240+
// No required field - all properties should be optional
1241+
};
1242+
1243+
const rawShape = jsonSchemaObjectToZodRawShape(jsonSchema);
1244+
const schema = z.object(rawShape);
1245+
1246+
// All fields should be optional
1247+
expect(() => schema.parse({})).not.toThrow();
1248+
expect(schema.parse({})).toEqual({
1249+
field1: "test",
1250+
field2: 42,
1251+
field3: true,
1252+
field4: "test2",
1253+
field5: { name: "test", age: 10, isActive: true },
1254+
})
1255+
});
12201256
});

0 commit comments

Comments
 (0)