Skip to content

Commit

Permalink
fix(helpers/zod): add nullableStrategy option
Browse files Browse the repository at this point in the history
so we can generate `nullable: true` instead of `type: ["foo", "null"]`
and avoid having to change the `target` json schema version as we're
in a weird in the middle state
  • Loading branch information
RobertCraigie authored and stainless-app[bot] committed Aug 8, 2024
1 parent d8a80a9 commit ad89892
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 21 deletions.
2 changes: 2 additions & 0 deletions src/_vendor/zod-to-json-schema/Options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export type Options<Target extends Targets = 'jsonSchema7'> = {
dateStrategy: DateStrategy | DateStrategy[];
mapStrategy: 'entries' | 'record';
removeAdditionalStrategy: 'passthrough' | 'strict';
nullableStrategy: 'from-target' | 'property';
target: Target;
strictUnions: boolean;
definitionPath: string;
Expand Down Expand Up @@ -45,6 +46,7 @@ export const defaultOptions: Options = {
pipeStrategy: 'all',
dateStrategy: 'format:date-time',
mapStrategy: 'entries',
nullableStrategy: 'from-target',
removeAdditionalStrategy: 'passthrough',
definitionPath: 'definitions',
target: 'jsonSchema7',
Expand Down
2 changes: 1 addition & 1 deletion src/_vendor/zod-to-json-schema/parsers/nullable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export function parseNullableDef(def: ZodNullableDef, refs: Refs): JsonSchema7Nu
['ZodString', 'ZodNumber', 'ZodBigInt', 'ZodBoolean', 'ZodNull'].includes(def.innerType._def.typeName) &&
(!def.innerType._def.checks || !def.innerType._def.checks.length)
) {
if (refs.target === 'openApi3') {
if (refs.target === 'openApi3' || refs.nullableStrategy === 'property') {
return {
type: primitiveMappings[def.innerType._def.typeName as keyof typeof primitiveMappings],
nullable: true,
Expand Down
1 change: 1 addition & 0 deletions src/helpers/zod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ function zodToJsonSchema(schema: z.ZodType, options: { name: string }): Record<s
name: options.name,
nameStrategy: 'duplicate-ref',
$refStrategy: 'extract-to-root',
nullableStrategy: 'property',
});
}

Expand Down
30 changes: 10 additions & 20 deletions tests/lib/parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -307,19 +307,15 @@ describe('.parse()', () => {
"properties": {
"description": {
"description": "Open text for any other relevant information about what the contact does.",
"type": [
"string",
"null",
],
"nullable": true,
"type": "string",
},
"name": {
"type": "string",
},
"phone_number": {
"type": [
"string",
"null",
],
"nullable": true,
"type": "string",
},
"roles": {
"description": "Any roles for which the contact is important, use other for custom roles",
Expand Down Expand Up @@ -376,10 +372,8 @@ describe('.parse()', () => {
"type": "string",
},
"contactPerson_properties_person1_properties_phone_number": {
"type": [
"string",
"null",
],
"nullable": true,
"type": "string",
},
},
"properties": {
Expand All @@ -388,19 +382,15 @@ describe('.parse()', () => {
"properties": {
"description": {
"description": "Open text for any other relevant information about what the contact does.",
"type": [
"string",
"null",
],
"nullable": true,
"type": "string",
},
"name": {
"type": "string",
},
"phone_number": {
"type": [
"string",
"null",
],
"nullable": true,
"type": "string",
},
"roles": {
"description": "Any roles for which the contact is important, use other for custom roles",
Expand Down

0 comments on commit ad89892

Please sign in to comment.