Skip to content

Commit

Permalink
fix(helpers/zod): correct logic for adding root schema to definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
RobertCraigie authored and stainless-app[bot] committed Aug 8, 2024
1 parent 31e4afd commit e4a247a
Show file tree
Hide file tree
Showing 2 changed files with 181 additions and 4 deletions.
6 changes: 2 additions & 4 deletions src/_vendor/zod-to-json-schema/zodToJsonSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,6 @@ const zodToJsonSchema = <Target extends Targets = 'jsonSchema7'>(
main.title = title;
}

const rootRefPath = name ? [...refs.basePath, refs.definitionPath, name].join('/') : null;

const combined: ReturnType<typeof zodToJsonSchema<Target>> =
name === undefined ?
definitions ?
Expand All @@ -74,13 +72,13 @@ const zodToJsonSchema = <Target extends Targets = 'jsonSchema7'>(
: refs.nameStrategy === 'duplicate-ref' ?
{
...main,
...(definitions || refs.seenRefs.has(rootRefPath!) ?
...(definitions || refs.seenRefs.size ?
{
[refs.definitionPath]: {
...definitions,
// only actually duplicate the schema definition if it was ever referenced
// otherwise the duplication is completely pointless
...(refs.seenRefs.has(rootRefPath!) ? { [name]: main } : undefined),
...(refs.seenRefs.size ? { [name]: main } : undefined),
},
}
: undefined),
Expand Down
179 changes: 179 additions & 0 deletions tests/lib/parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,5 +267,184 @@ describe('.parse()', () => {
}
`);
});

test('merged schemas', async () => {
const personSchema = z.object({
name: z.string(),
phone_number: z.string().nullable(),
});

const contactPersonSchema = z.object({
person1: personSchema.merge(
z.object({
roles: z
.array(z.enum(['parent', 'child', 'sibling', 'spouse', 'friend', 'other']))
.describe('Any roles for which the contact is important, use other for custom roles'),
description: z
.string()
.nullable()
.describe('Open text for any other relevant information about what the contact does.'),
}),
),
person2: personSchema.merge(
z.object({
differentField: z.string(),
}),
),
});

expect(zodResponseFormat(contactPersonSchema, 'contactPerson').json_schema.schema)
.toMatchInlineSnapshot(`
{
"$schema": "http://json-schema.org/draft-07/schema#",
"additionalProperties": false,
"definitions": {
"contactPerson": {
"additionalProperties": false,
"properties": {
"person1": {
"additionalProperties": false,
"properties": {
"description": {
"description": "Open text for any other relevant information about what the contact does.",
"type": [
"string",
"null",
],
},
"name": {
"type": "string",
},
"phone_number": {
"type": [
"string",
"null",
],
},
"roles": {
"description": "Any roles for which the contact is important, use other for custom roles",
"items": {
"enum": [
"parent",
"child",
"sibling",
"spouse",
"friend",
"other",
],
"type": "string",
},
"type": "array",
},
},
"required": [
"name",
"phone_number",
"roles",
"description",
],
"type": "object",
},
"person2": {
"additionalProperties": false,
"properties": {
"differentField": {
"type": "string",
},
"name": {
"$ref": "#/definitions/contactPerson/properties/person1/properties/name",
},
"phone_number": {
"$ref": "#/definitions/contactPerson/properties/person1/properties/phone_number",
},
},
"required": [
"name",
"phone_number",
"differentField",
],
"type": "object",
},
},
"required": [
"person1",
"person2",
],
"type": "object",
},
},
"properties": {
"person1": {
"additionalProperties": false,
"properties": {
"description": {
"description": "Open text for any other relevant information about what the contact does.",
"type": [
"string",
"null",
],
},
"name": {
"type": "string",
},
"phone_number": {
"type": [
"string",
"null",
],
},
"roles": {
"description": "Any roles for which the contact is important, use other for custom roles",
"items": {
"enum": [
"parent",
"child",
"sibling",
"spouse",
"friend",
"other",
],
"type": "string",
},
"type": "array",
},
},
"required": [
"name",
"phone_number",
"roles",
"description",
],
"type": "object",
},
"person2": {
"additionalProperties": false,
"properties": {
"differentField": {
"type": "string",
},
"name": {
"$ref": "#/definitions/contactPerson/properties/person1/properties/name",
},
"phone_number": {
"$ref": "#/definitions/contactPerson/properties/person1/properties/phone_number",
},
},
"required": [
"name",
"phone_number",
"differentField",
],
"type": "object",
},
},
"required": [
"person1",
"person2",
],
"type": "object",
}
`);
});
});
});

0 comments on commit e4a247a

Please sign in to comment.