Skip to content

Commit e503b10

Browse files
authored
Add multipleOf support (#358)
1 parent 590cb69 commit e503b10

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -717,7 +717,8 @@ For example in `z.string().nullable()` will be rendered differently
717717
- ZodNullable
718718
- ZodNumber
719719
- `integer` `type` mapping for `.int()`
720-
- `exclusiveMin`/`min`/`exclusiveMax`/`max` mapping for `.min()`, `.max()`, `lt()`, `gt()`
720+
- `exclusiveMin`/`min`/`exclusiveMax`/`max` mapping for `.min()`, `.max()`, `lt()`, `gt()`, `.positive()`, `.negative()`, `.nonnegative()`, `.nonpositive()`.
721+
- `multipleOf` mapping for `.multipleOf()`
721722
- ZodObject
722723
- `additionalProperties` mapping for `.catchall()`, `.strict()`
723724
- `allOf` mapping for `.extend()` when the base object is registered and does not have `catchall()`, `strict()` and extension does not override a field.

src/create/schema/parsers/number.test.ts

+15
Original file line numberDiff line numberDiff line change
@@ -104,4 +104,19 @@ describe('createNumberSchema', () => {
104104

105105
expect(result).toStrictEqual(expected);
106106
});
107+
108+
it('supports multipleOf', () => {
109+
const expected: Schema = {
110+
type: 'schema',
111+
schema: {
112+
type: 'number',
113+
multipleOf: 2,
114+
},
115+
};
116+
const schema = z.number().multipleOf(2);
117+
118+
const result = createNumberSchema(schema, createOutputState());
119+
120+
expect(result).toStrictEqual(expected);
121+
});
107122
});

src/create/schema/parsers/number.ts

+9
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,26 @@ export const createNumberSchema = (
1313

1414
const minimum = mapMinimum(zodNumberChecks, state.components.openapi);
1515
const maximum = mapMaximum(zodNumberChecks, state.components.openapi);
16+
const multipleOf = mapMultipleOf(zodNumberChecks);
1617

1718
return {
1819
type: 'schema',
1920
schema: {
2021
type: mapNumberType(zodNumberChecks),
22+
...(multipleOf && multipleOf),
2123
...(minimum && (minimum as oas31.SchemaObject)), // Union types are not easy to tame
2224
...(maximum && (maximum as oas31.SchemaObject)),
2325
},
2426
};
2527
};
2628

29+
export const mapMultipleOf = (
30+
zodNumberCheck: ZodNumberCheckMap,
31+
): Pick<oas31.SchemaObject | oas30.SchemaObject, 'multipleOf'> | undefined =>
32+
zodNumberCheck.multipleOf
33+
? { multipleOf: zodNumberCheck.multipleOf.value }
34+
: undefined;
35+
2736
export const mapMaximum = (
2837
zodNumberCheck: ZodNumberCheckMap,
2938
openapi: ZodOpenApiVersion,

0 commit comments

Comments
 (0)