Repository: samchungy/zod-openapi
Pull request #371 (2024-11-25 01:59:35)

Bump yaml from 2.6.0 to 2.6.1
Bumps [yaml](https://github.com/eemeli/yaml) from 2.6.0 to 2.6.1.
<details>
<summary>Release notes</summary>
<p><em>Sourced from <a href="https://github.com/eemeli/yaml/releases">yaml's releases</a>.</em></p>
<blockquote>
<h2>v2.6.1</h2>
<ul>
<li>Do not strip <code>:00</code> seconds from <code>!!timestamp</code> values (<a href="https://redirect.github.com/eemeli/yaml/issues/578">#578</a>, with thanks to <a href="https://github.com/qraynaud"><code>@​qraynaud</code></a>)</li>
<li>Tighten regexp for JSON <code>!!bool</code> (<a href="https://redirect.github.com/eemeli/yaml/issues/587">#587</a>, with thanks to <a href="https://github.com/vra5107"><code>@​vra5107</code></a>)</li>
<li>Default to literal block scalar if folded would overflow (<a href="https://redirect.github.com/eemeli/yaml/issues/585">#585</a>)</li>
</ul>
</blockquote>
</details>
<details>
<summary>Commits</summary>
<ul>
<li><a href="https://github.com/eemeli/yaml/commit/aa1898ae61605ea09bb79621d25ad5e7fd9b4217"><code>aa1898a</code></a> 2.6.1</li>
<li><a href="https://github.com/eemeli/yaml/commit/6be0a9136dca077864782d1ad3b53a2cbb35748e"><code>6be0a91</code></a> fix: Default to literal block scalar if folded would overflow (fixes <a href="https://redirect.github.com/eemeli/yaml/issues/585">#585</a>)</li>
<li><a href="https://github.com/eemeli/yaml/commit/108f699cf20354221df0fc6ac059a9ad39e70d6e"><code>108f699</code></a> fix: Tighten regexp for JSON !!bool (<a href="https://redirect.github.com/eemeli/yaml/issues/587">#587</a>)</li>
<li><a href="https://github.com/eemeli/yaml/commit/2e85b914b918b13e3985c58b1ce735d2c3f5a813"><code>2e85b91</code></a> fix: Do not strip :00 seconds from !!timestamp values (<a href="https://redirect.github.com/eemeli/yaml/issues/578">#578</a>)</li>
<li><a href="https://github.com/eemeli/yaml/commit/58506001bdf531eaa2c22316edaf00f32a825af6"><code>5850600</code></a> chore: Refresh lockfile</li>
<li><a href="https://github.com/eemeli/yaml/commit/16fb6e2c34dce338c22ed7a989683a9cd1f49688"><code>16fb6e2</code></a> chore: Add .well-known/funding-manifest-urls</li>
<li>See full diff in <a href="https://github.com/eemeli/yaml/compare/v2.6.0...v2.6.1">compare view</a></li>
</ul>
</details>
<br />


[![Dependabot compatibility score](https://dependabot-badges.githubapp.com/badges/compatibility_score?dependency-name=yaml&package-manager=npm_and_yarn&previous-version=2.6.0&new-version=2.6.1)](https://docs.github.com/en/github/managing-security-vulnerabilities/about-dependabot-security-updates#about-compatibility-scores)

Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting `@dependabot rebase`.

[//]: # (dependabot-automerge-start)
[//]: # (dependabot-automerge-end)

---

<details>
<summary>Dependabot commands and options</summary>
<br />

You can trigger Dependabot actions by commenting on this PR:
- `@dependabot rebase` will rebase this PR
- `@dependabot recreate` will recreate this PR, overwriting any edits that have been made to it
- `@dependabot merge` will merge this PR after your CI passes on it
- `@dependabot squash and merge` will squash and merge this PR after your CI passes on it
- `@dependabot cancel merge` will cancel a previously requested merge and block automerging
- `@dependabot reopen` will reopen this PR if it is closed
- `@dependabot close` will close this PR and stop Dependabot recreating it. You can achieve the same result by closing it manually
- `@dependabot show <dependency name> ignore conditions` will show all of the ignore conditions of the specified dependency
- `@dependabot ignore this major version` will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself)
- `@dependabot ignore this minor version` will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself)
- `@dependabot ignore this dependency` will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself)


</details>


Patch:
diff --git a/README.md b/README.md
index d265caa..cabaa97 100644
--- a/README.md
+++ b/README.md
@@ -206,6 +206,7 @@ const document = createDocument({
 const document = createDocument(details, {
   defaultDateSchema: { type: 'string', format: 'date-time' }, // defaults to { type: 'string' }
   unionOneOf: true, // defaults to false. Forces all ZodUnions to output oneOf instead of anyOf. An `.openapi()` `unionOneOf` value takes precedence over this one.
+  enforceDiscriminatedUnionComponents: true, // defaults to false. Throws an error if a Discriminated Union member is not registered as a component.
 });
 ```
 
diff --git a/package.json b/package.json
index 13238f1..b39eb11 100644
--- a/package.json
+++ b/package.json
@@ -79,7 +79,7 @@
     "eslint-plugin-zod-openapi": "^1.0.0-beta.0",
     "openapi3-ts": "4.4.0",
     "skuba": "9.1.0",
-    "yaml": "2.6.0",
+    "yaml": "2.6.1",
     "zod": "3.23.8"
   },
   "peerDependencies": {
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 4115c99..ce7346f 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -30,8 +30,8 @@ importers:
         specifier: 9.1.0
         version: 9.1.0(@babel/core@7.24.6)(@jest/transform@29.7.0)(@swc/core@1.6.1)(@typescript-eslint/eslint-plugin@8.7.0(@typescript-eslint/parser@8.7.0(eslint@9.11.1(jiti@1.21.6))(typescript@5.6.2))(eslint@9.11.1(jiti@1.21.6))(typescript@5.6.2))(@typescript-eslint/parser@8.7.0(eslint@9.11.1(jiti@1.21.6))(typescript@5.6.2))(babel-jest@29.7.0(@babel/core@7.24.6))(eslint-plugin-import@2.29.1(@typescript-eslint/parser@8.7.0(eslint@9.11.1(jiti@1.21.6))(typescript@5.6.2))(eslint@9.11.1(jiti@1.21.6)))(jiti@1.21.6)
       yaml:
-        specifier: 2.6.0
-        version: 2.6.0
+        specifier: 2.6.1
+        version: 2.6.1
       zod:
         specifier: 3.23.8
         version: 3.23.8
@@ -5565,8 +5565,8 @@ packages:
     resolution: {integrity: sha512-r3vXyErRCYJ7wg28yvBY5VSoAF8ZvlcW9/BwUzEtUsjvX/DKs24dIkuwjtuprwJJHsbyUbLApepYTR1BN4uHrg==}
     engines: {node: '>= 6'}
 
-  yaml@2.6.0:
-    resolution: {integrity: sha512-a6ae//JvKDEra2kdi1qzCyrJW/WZCgFi8ydDV+eXExl95t+5R+ijnqHJbz9tmMh8FUjx3iv2fCQ4dclAQlO2UQ==}
+  yaml@2.6.1:
+    resolution: {integrity: sha512-7r0XPzioN/Q9kXBro/XPnA6kznR73DHq+GXh5ON7ZozRO6aMjbmiBuKste2wslTFkC5d1dw0GooOCepZXJ2SAg==}
     engines: {node: '>= 14'}
     hasBin: true
 
@@ -10382,7 +10382,7 @@ snapshots:
 
   openapi3-ts@4.4.0:
     dependencies:
-      yaml: 2.6.0
+      yaml: 2.6.1
 
   optionator@0.9.4:
     dependencies:
@@ -11976,11 +11976,11 @@ snapshots:
     dependencies:
       eslint-visitor-keys: 3.4.3
       lodash: 4.17.21
-      yaml: 2.6.0
+      yaml: 2.6.1
 
   yaml@1.10.2: {}
 
-  yaml@2.6.0: {}
+  yaml@2.6.1: {}
 
   yargs-parser@20.2.9: {}
 
diff --git a/src/create/document.ts b/src/create/document.ts
index 33fbafc..d316815 100644
--- a/src/create/document.ts
+++ b/src/create/document.ts
@@ -155,6 +155,10 @@ export type ZodObjectInputType<
 > = ZodType<Output, Def, Input>;
 
 export interface CreateDocumentOptions {
+  /**
+   * Used to throw an error if a Discriminated Union member is not registered as a component
+   */
+  enforceDiscriminatedUnionComponents?: boolean;
   /**
    * Used to change the default Zod Date schema
    */
diff --git a/src/create/schema/parsers/discriminatedUnion.ts b/src/create/schema/parsers/discriminatedUnion.ts
index 6861dfc..1f928ba 100644
--- a/src/create/schema/parsers/discriminatedUnion.ts
+++ b/src/create/schema/parsers/discriminatedUnion.ts
@@ -36,6 +36,7 @@ export const createDiscriminatedUnionSchema = <
     zodDiscriminatedUnion.discriminator,
     state,
   );
+
   return {
     type: 'schema',
     schema: {
@@ -98,6 +99,11 @@ export const mapDiscriminator = (
     const schema = schemas[index] as oas31.SchemaObject | oas31.ReferenceObject;
     const componentSchemaRef = '$ref' in schema ? schema?.$ref : undefined;
     if (!componentSchemaRef) {
+      if (state.documentOptions?.enforceDiscriminatedUnionComponents) {
+        throw new Error(
+          `Discriminated Union member ${index} at ${state.path.join(' > ')} is not registered as a component`,
+        );
+      }
       return undefined;
     }
 


Test patch:
diff --git a/src/create/schema/parsers/discriminatedUnion.test.ts b/src/create/schema/parsers/discriminatedUnion.test.ts
index 7c77470..69c2f7f 100644
--- a/src/create/schema/parsers/discriminatedUnion.test.ts
+++ b/src/create/schema/parsers/discriminatedUnion.test.ts
@@ -486,4 +486,26 @@ describe('createDiscriminatedUnionSchema', () => {
       ],
     });
   });
+
+  it('throws an error if enforceDiscriminatedUnionComponents is specified and a schema is not registered', () => {
+    const schema = z.discriminatedUnion('type', [
+      z.object({
+        type: z.literal('a'),
+      }),
+      z
+        .object({
+          type: z.literal('b'),
+        })
+        .openapi({ ref: 'b' }),
+    ]);
+
+    expect(() =>
+      createDiscriminatedUnionSchema(schema, {
+        ...createOutputState({}, { enforceDiscriminatedUnionComponents: true }),
+        path: ['some', 'path'],
+      }),
+    ).toThrowErrorMatchingInlineSnapshot(
+      `"Discriminated Union member 0 at some > path is not registered as a component"`,
+    );
+  });
 });