@tambo-ai/react 0.65.3 → 0.66.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +120 -20
- package/dist/{providers/hoc → hoc}/with-tambo-interactable.d.ts +2 -2
- package/dist/hoc/with-tambo-interactable.d.ts.map +1 -0
- package/dist/{providers/hoc → hoc}/with-tambo-interactable.js +29 -2
- package/dist/hoc/with-tambo-interactable.js.map +1 -0
- package/dist/hoc/with-tambo-interactable.test.d.ts +2 -0
- package/dist/hoc/with-tambo-interactable.test.d.ts.map +1 -0
- package/dist/hoc/with-tambo-interactable.test.js +192 -0
- package/dist/hoc/with-tambo-interactable.test.js.map +1 -0
- package/dist/hooks/index.d.ts +1 -1
- package/dist/hooks/index.d.ts.map +1 -1
- package/dist/hooks/index.js +2 -1
- package/dist/hooks/index.js.map +1 -1
- package/dist/hooks/use-current-message.d.ts +51 -7
- package/dist/hooks/use-current-message.d.ts.map +1 -1
- package/dist/hooks/use-current-message.js +50 -6
- package/dist/hooks/use-current-message.js.map +1 -1
- package/dist/hooks/use-current-message.test.d.ts +2 -0
- package/dist/hooks/use-current-message.test.d.ts.map +1 -0
- package/dist/hooks/use-current-message.test.js +264 -0
- package/dist/hooks/use-current-message.test.js.map +1 -0
- package/dist/index.d.ts +10 -8
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +7 -3
- package/dist/index.js.map +1 -1
- package/dist/mcp/index.d.ts +1 -1
- package/dist/mcp/index.d.ts.map +1 -1
- package/dist/mcp/index.js +2 -1
- package/dist/mcp/index.js.map +1 -1
- package/dist/mcp/mcp-hooks.d.ts +77 -6
- package/dist/mcp/mcp-hooks.d.ts.map +1 -1
- package/dist/mcp/mcp-hooks.js +104 -40
- package/dist/mcp/mcp-hooks.js.map +1 -1
- package/dist/mcp/mcp-hooks.test.js +83 -18
- package/dist/mcp/mcp-hooks.test.js.map +1 -1
- package/dist/mcp/tambo-mcp-provider.d.ts.map +1 -1
- package/dist/mcp/tambo-mcp-provider.js +2 -1
- package/dist/mcp/tambo-mcp-provider.js.map +1 -1
- package/dist/model/component-metadata.d.ts +444 -14
- package/dist/model/component-metadata.d.ts.map +1 -1
- package/dist/model/component-metadata.js.map +1 -1
- package/dist/model/generate-component-response.d.ts +12 -1
- package/dist/model/generate-component-response.d.ts.map +1 -1
- package/dist/model/generate-component-response.js.map +1 -1
- package/dist/model/resource-info.d.ts +55 -0
- package/dist/model/resource-info.d.ts.map +1 -0
- package/dist/model/resource-info.js +3 -0
- package/dist/model/resource-info.js.map +1 -0
- package/dist/providers/index.d.ts +1 -1
- package/dist/providers/index.d.ts.map +1 -1
- package/dist/providers/index.js.map +1 -1
- package/dist/providers/tambo-component-provider.d.ts +4 -4
- package/dist/providers/tambo-component-provider.d.ts.map +1 -1
- package/dist/providers/tambo-component-provider.js.map +1 -1
- package/dist/providers/tambo-interactable-provider-partial-updates.test.js +87 -87
- package/dist/providers/tambo-interactable-provider-partial-updates.test.js.map +1 -1
- package/dist/providers/tambo-interactable-provider.d.ts +2 -3
- package/dist/providers/tambo-interactable-provider.d.ts.map +1 -1
- package/dist/providers/tambo-interactable-provider.js +47 -41
- package/dist/providers/tambo-interactable-provider.js.map +1 -1
- package/dist/providers/tambo-interactables-additional-context-edge-cases.test.js +9 -9
- package/dist/providers/tambo-interactables-additional-context-edge-cases.test.js.map +1 -1
- package/dist/providers/tambo-interactables-additional-context.test.js +11 -11
- package/dist/providers/tambo-interactables-additional-context.test.js.map +1 -1
- package/dist/providers/tambo-registry-provider.d.ts +28 -7
- package/dist/providers/tambo-registry-provider.d.ts.map +1 -1
- package/dist/providers/tambo-registry-provider.js +70 -181
- package/dist/providers/tambo-registry-provider.js.map +1 -1
- package/dist/providers/tambo-registry-provider.test.js +152 -30
- package/dist/providers/tambo-registry-provider.test.js.map +1 -1
- package/dist/providers/tambo-registry-schema-compat.test.d.ts +2 -0
- package/dist/providers/tambo-registry-schema-compat.test.d.ts.map +1 -0
- package/dist/providers/tambo-registry-schema-compat.test.js +616 -0
- package/dist/providers/tambo-registry-schema-compat.test.js.map +1 -0
- package/dist/providers/tambo-stubs.d.ts +2 -2
- package/dist/providers/tambo-stubs.d.ts.map +1 -1
- package/dist/providers/tambo-stubs.js +5 -0
- package/dist/providers/tambo-stubs.js.map +1 -1
- package/dist/providers/tambo-thread-input-provider.d.ts +1 -0
- package/dist/providers/tambo-thread-input-provider.d.ts.map +1 -1
- package/dist/providers/tambo-thread-input-provider.js +3 -3
- package/dist/providers/tambo-thread-input-provider.js.map +1 -1
- package/dist/providers/tambo-thread-provider.d.ts.map +1 -1
- package/dist/providers/tambo-thread-provider.js.map +1 -1
- package/dist/providers/tambo-thread-provider.test.js +32 -36
- package/dist/providers/tambo-thread-provider.test.js.map +1 -1
- package/dist/schema/index.d.ts +6 -0
- package/dist/schema/index.d.ts.map +1 -0
- package/dist/schema/index.js +18 -0
- package/dist/schema/index.js.map +1 -0
- package/dist/schema/json-schema.d.ts +35 -0
- package/dist/schema/json-schema.d.ts.map +1 -0
- package/dist/schema/json-schema.js +103 -0
- package/dist/schema/json-schema.js.map +1 -0
- package/dist/schema/schema.d.ts +66 -0
- package/dist/schema/schema.d.ts.map +1 -0
- package/dist/schema/schema.js +189 -0
- package/dist/schema/schema.js.map +1 -0
- package/dist/schema/schema.test.d.ts +2 -0
- package/dist/schema/schema.test.d.ts.map +1 -0
- package/dist/schema/schema.test.js +41 -0
- package/dist/schema/schema.test.js.map +1 -0
- package/dist/schema/standard-schema.d.ts +21 -0
- package/dist/schema/standard-schema.d.ts.map +1 -0
- package/dist/schema/standard-schema.js +37 -0
- package/dist/schema/standard-schema.js.map +1 -0
- package/dist/schema/validate.d.ts +14 -0
- package/dist/schema/validate.d.ts.map +1 -0
- package/dist/schema/validate.js +148 -0
- package/dist/schema/validate.js.map +1 -0
- package/dist/schema/validate.test.d.ts +2 -0
- package/dist/schema/validate.test.d.ts.map +1 -0
- package/dist/schema/validate.test.js +128 -0
- package/dist/schema/validate.test.js.map +1 -0
- package/dist/schema/zod.d.ts +54 -0
- package/dist/schema/zod.d.ts.map +1 -0
- package/dist/schema/zod.js +147 -0
- package/dist/schema/zod.js.map +1 -0
- package/dist/testing/tools.d.ts +29 -15
- package/dist/testing/tools.d.ts.map +1 -1
- package/dist/testing/tools.js +64 -19
- package/dist/testing/tools.js.map +1 -1
- package/dist/util/generate-component.d.ts.map +1 -1
- package/dist/util/generate-component.js +3 -3
- package/dist/util/generate-component.js.map +1 -1
- package/dist/util/mcp-server-utils.d.ts +23 -0
- package/dist/util/mcp-server-utils.d.ts.map +1 -0
- package/dist/util/mcp-server-utils.js +107 -0
- package/dist/util/mcp-server-utils.js.map +1 -0
- package/dist/util/mcp-server-utils.test.d.ts +2 -0
- package/dist/util/mcp-server-utils.test.d.ts.map +1 -0
- package/dist/util/mcp-server-utils.test.js +287 -0
- package/dist/util/mcp-server-utils.test.js.map +1 -0
- package/dist/util/message-builder.d.ts +2 -1
- package/dist/util/message-builder.d.ts.map +1 -1
- package/dist/util/message-builder.js +54 -36
- package/dist/util/message-builder.js.map +1 -1
- package/dist/util/message-builder.test.js +500 -13
- package/dist/util/message-builder.test.js.map +1 -1
- package/dist/util/registry-validators.d.ts +26 -0
- package/dist/util/registry-validators.d.ts.map +1 -0
- package/dist/util/registry-validators.js +105 -0
- package/dist/util/registry-validators.js.map +1 -0
- package/dist/util/registry-validators.test.d.ts +2 -0
- package/dist/util/registry-validators.test.d.ts.map +1 -0
- package/dist/util/registry-validators.test.js +235 -0
- package/dist/util/registry-validators.test.js.map +1 -0
- package/dist/util/registry.d.ts +35 -7
- package/dist/util/registry.d.ts.map +1 -1
- package/dist/util/registry.js +60 -77
- package/dist/util/registry.js.map +1 -1
- package/dist/util/registry.test.d.ts +2 -0
- package/dist/util/registry.test.d.ts.map +1 -0
- package/dist/util/registry.test.js +204 -0
- package/dist/util/registry.test.js.map +1 -0
- package/dist/util/resource-validators.d.ts +16 -0
- package/dist/util/resource-validators.d.ts.map +1 -0
- package/dist/util/resource-validators.js +34 -0
- package/dist/util/resource-validators.js.map +1 -0
- package/dist/util/tool-caller.d.ts +2 -2
- package/dist/util/tool-caller.d.ts.map +1 -1
- package/dist/util/tool-caller.js +12 -4
- package/dist/util/tool-caller.js.map +1 -1
- package/esm/{providers/hoc → hoc}/with-tambo-interactable.d.ts +2 -2
- package/esm/hoc/with-tambo-interactable.d.ts.map +1 -0
- package/esm/{providers/hoc → hoc}/with-tambo-interactable.js +29 -2
- package/esm/hoc/with-tambo-interactable.js.map +1 -0
- package/esm/hoc/with-tambo-interactable.test.d.ts +2 -0
- package/esm/hoc/with-tambo-interactable.test.d.ts.map +1 -0
- package/esm/hoc/with-tambo-interactable.test.js +187 -0
- package/esm/hoc/with-tambo-interactable.test.js.map +1 -0
- package/esm/hooks/index.d.ts +1 -1
- package/esm/hooks/index.d.ts.map +1 -1
- package/esm/hooks/index.js +1 -1
- package/esm/hooks/index.js.map +1 -1
- package/esm/hooks/use-current-message.d.ts +51 -7
- package/esm/hooks/use-current-message.d.ts.map +1 -1
- package/esm/hooks/use-current-message.js +48 -5
- package/esm/hooks/use-current-message.js.map +1 -1
- package/esm/hooks/use-current-message.test.d.ts +2 -0
- package/esm/hooks/use-current-message.test.d.ts.map +1 -0
- package/esm/hooks/use-current-message.test.js +259 -0
- package/esm/hooks/use-current-message.test.js.map +1 -0
- package/esm/index.d.ts +10 -8
- package/esm/index.d.ts.map +1 -1
- package/esm/index.js +4 -2
- package/esm/index.js.map +1 -1
- package/esm/mcp/index.d.ts +1 -1
- package/esm/mcp/index.d.ts.map +1 -1
- package/esm/mcp/index.js +1 -1
- package/esm/mcp/index.js.map +1 -1
- package/esm/mcp/mcp-hooks.d.ts +77 -6
- package/esm/mcp/mcp-hooks.d.ts.map +1 -1
- package/esm/mcp/mcp-hooks.js +103 -40
- package/esm/mcp/mcp-hooks.js.map +1 -1
- package/esm/mcp/mcp-hooks.test.js +84 -19
- package/esm/mcp/mcp-hooks.test.js.map +1 -1
- package/esm/mcp/tambo-mcp-provider.d.ts.map +1 -1
- package/esm/mcp/tambo-mcp-provider.js +2 -1
- package/esm/mcp/tambo-mcp-provider.js.map +1 -1
- package/esm/model/component-metadata.d.ts +444 -14
- package/esm/model/component-metadata.d.ts.map +1 -1
- package/esm/model/component-metadata.js.map +1 -1
- package/esm/model/generate-component-response.d.ts +12 -1
- package/esm/model/generate-component-response.d.ts.map +1 -1
- package/esm/model/generate-component-response.js.map +1 -1
- package/esm/model/resource-info.d.ts +55 -0
- package/esm/model/resource-info.d.ts.map +1 -0
- package/esm/model/resource-info.js +2 -0
- package/esm/model/resource-info.js.map +1 -0
- package/esm/providers/index.d.ts +1 -1
- package/esm/providers/index.d.ts.map +1 -1
- package/esm/providers/index.js.map +1 -1
- package/esm/providers/tambo-component-provider.d.ts +4 -4
- package/esm/providers/tambo-component-provider.d.ts.map +1 -1
- package/esm/providers/tambo-component-provider.js.map +1 -1
- package/esm/providers/tambo-interactable-provider-partial-updates.test.js +1 -1
- package/esm/providers/tambo-interactable-provider-partial-updates.test.js.map +1 -1
- package/esm/providers/tambo-interactable-provider.d.ts +2 -3
- package/esm/providers/tambo-interactable-provider.d.ts.map +1 -1
- package/esm/providers/tambo-interactable-provider.js +44 -38
- package/esm/providers/tambo-interactable-provider.js.map +1 -1
- package/esm/providers/tambo-interactables-additional-context-edge-cases.test.js +3 -3
- package/esm/providers/tambo-interactables-additional-context-edge-cases.test.js.map +1 -1
- package/esm/providers/tambo-interactables-additional-context.test.js +3 -3
- package/esm/providers/tambo-interactables-additional-context.test.js.map +1 -1
- package/esm/providers/tambo-registry-provider.d.ts +28 -7
- package/esm/providers/tambo-registry-provider.d.ts.map +1 -1
- package/esm/providers/tambo-registry-provider.js +67 -175
- package/esm/providers/tambo-registry-provider.js.map +1 -1
- package/esm/providers/tambo-registry-provider.test.js +148 -26
- package/esm/providers/tambo-registry-provider.test.js.map +1 -1
- package/esm/providers/tambo-registry-schema-compat.test.d.ts +2 -0
- package/esm/providers/tambo-registry-schema-compat.test.d.ts.map +1 -0
- package/esm/providers/tambo-registry-schema-compat.test.js +578 -0
- package/esm/providers/tambo-registry-schema-compat.test.js.map +1 -0
- package/esm/providers/tambo-stubs.d.ts +2 -2
- package/esm/providers/tambo-stubs.d.ts.map +1 -1
- package/esm/providers/tambo-stubs.js +5 -0
- package/esm/providers/tambo-stubs.js.map +1 -1
- package/esm/providers/tambo-thread-input-provider.d.ts +1 -0
- package/esm/providers/tambo-thread-input-provider.d.ts.map +1 -1
- package/esm/providers/tambo-thread-input-provider.js +3 -3
- package/esm/providers/tambo-thread-input-provider.js.map +1 -1
- package/esm/providers/tambo-thread-provider.d.ts.map +1 -1
- package/esm/providers/tambo-thread-provider.js.map +1 -1
- package/esm/providers/tambo-thread-provider.test.js +24 -28
- package/esm/providers/tambo-thread-provider.test.js.map +1 -1
- package/esm/schema/index.d.ts +6 -0
- package/esm/schema/index.d.ts.map +1 -0
- package/esm/schema/index.js +6 -0
- package/esm/schema/index.js.map +1 -0
- package/esm/schema/json-schema.d.ts +35 -0
- package/esm/schema/json-schema.d.ts.map +1 -0
- package/esm/schema/json-schema.js +98 -0
- package/esm/schema/json-schema.js.map +1 -0
- package/esm/schema/schema.d.ts +66 -0
- package/esm/schema/schema.d.ts.map +1 -0
- package/esm/schema/schema.js +182 -0
- package/esm/schema/schema.js.map +1 -0
- package/esm/schema/schema.test.d.ts +2 -0
- package/esm/schema/schema.test.d.ts.map +1 -0
- package/esm/schema/schema.test.js +39 -0
- package/esm/schema/schema.test.js.map +1 -0
- package/esm/schema/standard-schema.d.ts +21 -0
- package/esm/schema/standard-schema.d.ts.map +1 -0
- package/esm/schema/standard-schema.js +34 -0
- package/esm/schema/standard-schema.js.map +1 -0
- package/esm/schema/validate.d.ts +14 -0
- package/esm/schema/validate.d.ts.map +1 -0
- package/esm/schema/validate.js +145 -0
- package/esm/schema/validate.js.map +1 -0
- package/esm/schema/validate.test.d.ts +2 -0
- package/esm/schema/validate.test.d.ts.map +1 -0
- package/esm/schema/validate.test.js +126 -0
- package/esm/schema/validate.test.js.map +1 -0
- package/esm/schema/zod.d.ts +54 -0
- package/esm/schema/zod.d.ts.map +1 -0
- package/esm/schema/zod.js +136 -0
- package/esm/schema/zod.js.map +1 -0
- package/esm/testing/tools.d.ts +29 -15
- package/esm/testing/tools.d.ts.map +1 -1
- package/esm/testing/tools.js +62 -16
- package/esm/testing/tools.js.map +1 -1
- package/esm/util/generate-component.d.ts.map +1 -1
- package/esm/util/generate-component.js +3 -3
- package/esm/util/generate-component.js.map +1 -1
- package/esm/util/mcp-server-utils.d.ts +23 -0
- package/esm/util/mcp-server-utils.d.ts.map +1 -0
- package/esm/util/mcp-server-utils.js +102 -0
- package/esm/util/mcp-server-utils.js.map +1 -0
- package/esm/util/mcp-server-utils.test.d.ts +2 -0
- package/esm/util/mcp-server-utils.test.d.ts.map +1 -0
- package/esm/util/mcp-server-utils.test.js +285 -0
- package/esm/util/mcp-server-utils.test.js.map +1 -0
- package/esm/util/message-builder.d.ts +2 -1
- package/esm/util/message-builder.d.ts.map +1 -1
- package/esm/util/message-builder.js +54 -36
- package/esm/util/message-builder.js.map +1 -1
- package/esm/util/message-builder.test.js +500 -13
- package/esm/util/message-builder.test.js.map +1 -1
- package/esm/util/registry-validators.d.ts +26 -0
- package/esm/util/registry-validators.d.ts.map +1 -0
- package/esm/util/registry-validators.js +100 -0
- package/esm/util/registry-validators.js.map +1 -0
- package/esm/util/registry-validators.test.d.ts +2 -0
- package/esm/util/registry-validators.test.d.ts.map +1 -0
- package/esm/util/registry-validators.test.js +233 -0
- package/esm/util/registry-validators.test.js.map +1 -0
- package/esm/util/registry.d.ts +35 -7
- package/esm/util/registry.d.ts.map +1 -1
- package/esm/util/registry.js +57 -73
- package/esm/util/registry.js.map +1 -1
- package/esm/util/registry.test.d.ts +2 -0
- package/esm/util/registry.test.d.ts.map +1 -0
- package/esm/util/registry.test.js +169 -0
- package/esm/util/registry.test.js.map +1 -0
- package/esm/util/resource-validators.d.ts +16 -0
- package/esm/util/resource-validators.d.ts.map +1 -0
- package/esm/util/resource-validators.js +30 -0
- package/esm/util/resource-validators.js.map +1 -0
- package/esm/util/tool-caller.d.ts +2 -2
- package/esm/util/tool-caller.d.ts.map +1 -1
- package/esm/util/tool-caller.js +12 -4
- package/esm/util/tool-caller.js.map +1 -1
- package/package.json +13 -8
- package/dist/providers/hoc/with-tambo-interactable.d.ts.map +0 -1
- package/dist/providers/hoc/with-tambo-interactable.js.map +0 -1
- package/dist/util/validate-zod-schema.d.ts +0 -10
- package/dist/util/validate-zod-schema.d.ts.map +0 -1
- package/dist/util/validate-zod-schema.js +0 -100
- package/dist/util/validate-zod-schema.js.map +0 -1
- package/dist/util/validate-zod-schema.test.d.ts +0 -2
- package/dist/util/validate-zod-schema.test.d.ts.map +0 -1
- package/dist/util/validate-zod-schema.test.js +0 -96
- package/dist/util/validate-zod-schema.test.js.map +0 -1
- package/esm/providers/hoc/with-tambo-interactable.d.ts.map +0 -1
- package/esm/providers/hoc/with-tambo-interactable.js.map +0 -1
- package/esm/util/validate-zod-schema.d.ts +0 -10
- package/esm/util/validate-zod-schema.d.ts.map +0 -1
- package/esm/util/validate-zod-schema.js +0 -97
- package/esm/util/validate-zod-schema.js.map +0 -1
- package/esm/util/validate-zod-schema.test.d.ts +0 -2
- package/esm/util/validate-zod-schema.test.d.ts.map +0 -1
- package/esm/util/validate-zod-schema.test.js +0 -94
- package/esm/util/validate-zod-schema.test.js.map +0 -1
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.assertNoRecordSchema = assertNoRecordSchema;
|
|
4
|
+
const schema_1 = require("./schema");
|
|
5
|
+
const standard_schema_1 = require("./standard-schema");
|
|
6
|
+
/*
|
|
7
|
+
* Check if a JSON Schema represents a record type (map with dynamic keys).
|
|
8
|
+
*
|
|
9
|
+
* For our purposes, any `object` schema with `additionalProperties` defined
|
|
10
|
+
* as a nested schema (not just `true`/`false`) is treated as a record, even
|
|
11
|
+
* if it also declares some explicit `properties`.
|
|
12
|
+
* @param schema - The JSON Schema to check
|
|
13
|
+
* @returns True if the schema represents a record-like type
|
|
14
|
+
*/
|
|
15
|
+
function isRecordJsonSchema(schema) {
|
|
16
|
+
return (schema.type === "object" &&
|
|
17
|
+
typeof schema.additionalProperties === "object" &&
|
|
18
|
+
schema.additionalProperties !== null);
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Recursively walks a JSON Schema and throws when it encounters a record type.
|
|
22
|
+
* Records are not supported because they use dynamic keys which don't serialize
|
|
23
|
+
* well for the Tambo backend.
|
|
24
|
+
* @param schema - The JSON Schema to check
|
|
25
|
+
* @param path - Current path in the schema (for error messages)
|
|
26
|
+
* @param contextName - Human-readable context name for error messages
|
|
27
|
+
*/
|
|
28
|
+
function assertNoRecordInJsonSchema(schema, path, contextName) {
|
|
29
|
+
if (isRecordJsonSchema(schema)) {
|
|
30
|
+
const joined = path.length ? path.join(".") : "(root)";
|
|
31
|
+
throw new Error(`Record types (objects with dynamic keys) are not supported in ${contextName}. ` +
|
|
32
|
+
`Found at path "${joined}". ` +
|
|
33
|
+
"Replace it with an object using explicit keys.");
|
|
34
|
+
}
|
|
35
|
+
if (schema.properties) {
|
|
36
|
+
for (const [key, propSchema] of Object.entries(schema.properties)) {
|
|
37
|
+
if (typeof propSchema === "object" && propSchema !== null) {
|
|
38
|
+
assertNoRecordInJsonSchema(propSchema, [...path, key], contextName);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
if (typeof schema.additionalProperties === "object" &&
|
|
43
|
+
schema.additionalProperties !== null) {
|
|
44
|
+
assertNoRecordInJsonSchema(schema.additionalProperties, [...path, "[*]"], contextName);
|
|
45
|
+
}
|
|
46
|
+
if (schema.items) {
|
|
47
|
+
if (Array.isArray(schema.items)) {
|
|
48
|
+
// Tuple (JSON Schema draft-07): check each item schema
|
|
49
|
+
schema.items.forEach((itemSchema, index) => {
|
|
50
|
+
if (typeof itemSchema === "object" && itemSchema !== null) {
|
|
51
|
+
assertNoRecordInJsonSchema(itemSchema, [...path, `${index}`], contextName);
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
else if (typeof schema.items === "object") {
|
|
56
|
+
// Array: check the item schema
|
|
57
|
+
assertNoRecordInJsonSchema(schema.items, [...path, "[]"], contextName);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
const prefixItems = schema.prefixItems;
|
|
61
|
+
if (Array.isArray(prefixItems)) {
|
|
62
|
+
prefixItems.forEach((itemSchema, index) => {
|
|
63
|
+
if (typeof itemSchema === "object" && itemSchema !== null) {
|
|
64
|
+
assertNoRecordInJsonSchema(itemSchema, [...path, `${index}`], contextName);
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
if (schema.allOf) {
|
|
69
|
+
schema.allOf.forEach((subSchema, index) => {
|
|
70
|
+
if (typeof subSchema === "object" && subSchema !== null) {
|
|
71
|
+
assertNoRecordInJsonSchema(subSchema, [...path, `&${index}`], contextName);
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
if (schema.anyOf) {
|
|
76
|
+
schema.anyOf.forEach((subSchema, index) => {
|
|
77
|
+
if (typeof subSchema === "object" && subSchema !== null) {
|
|
78
|
+
assertNoRecordInJsonSchema(subSchema, [...path, `|${index}`], contextName);
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
if (schema.oneOf) {
|
|
83
|
+
schema.oneOf.forEach((subSchema, index) => {
|
|
84
|
+
if (typeof subSchema === "object" && subSchema !== null) {
|
|
85
|
+
assertNoRecordInJsonSchema(subSchema, [...path, `|${index}`], contextName);
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
if (typeof schema.not === "object" && schema.not !== null) {
|
|
90
|
+
assertNoRecordInJsonSchema(schema.not, [...path, "!"], contextName);
|
|
91
|
+
}
|
|
92
|
+
if (typeof schema.if === "object" && schema.if !== null) {
|
|
93
|
+
assertNoRecordInJsonSchema(schema.if, [...path, "if"], contextName);
|
|
94
|
+
}
|
|
95
|
+
if (typeof schema.then === "object" && schema.then !== null) {
|
|
96
|
+
assertNoRecordInJsonSchema(schema.then, [...path, "then"], contextName);
|
|
97
|
+
}
|
|
98
|
+
if (typeof schema.else === "object" && schema.else !== null) {
|
|
99
|
+
assertNoRecordInJsonSchema(schema.else, [...path, "else"], contextName);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Asserts that a schema does not contain record types (objects with dynamic keys).
|
|
104
|
+
* Records are not serializable to JSON Schema in a way that the Tambo backend
|
|
105
|
+
* understands, so they are disallowed.
|
|
106
|
+
*
|
|
107
|
+
* This function accepts Standard Schema validators (Zod, Valibot, ArkType, etc.)
|
|
108
|
+
* or JSON Schema objects. It converts them to JSON Schema and checks for record patterns.
|
|
109
|
+
*
|
|
110
|
+
* For tool schemas that are function types, this extracts and validates the input parameters.
|
|
111
|
+
* @param schema - The schema to validate (Standard Schema or JSON Schema)
|
|
112
|
+
* @param contextName - A human-readable label echoed in the error message
|
|
113
|
+
*/
|
|
114
|
+
function assertNoRecordSchema(schema, contextName = "schema") {
|
|
115
|
+
if (!schema) {
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
let jsonSchema;
|
|
119
|
+
try {
|
|
120
|
+
if ((0, standard_schema_1.isStandardSchema)(schema)) {
|
|
121
|
+
// Handle function schemas specially - extract input parameters
|
|
122
|
+
const converted = (0, schema_1.schemaToJsonSchema)(schema);
|
|
123
|
+
// If it's a function schema that couldn't be converted, the input params
|
|
124
|
+
// were extracted in registry.ts, so we can skip validation here
|
|
125
|
+
if (!converted || Object.keys(converted).length === 0) {
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
jsonSchema = converted;
|
|
129
|
+
}
|
|
130
|
+
else if (typeof schema === "object" &&
|
|
131
|
+
schema !== null &&
|
|
132
|
+
"type" in schema) {
|
|
133
|
+
// Already JSON Schema
|
|
134
|
+
jsonSchema = schema;
|
|
135
|
+
}
|
|
136
|
+
else {
|
|
137
|
+
// Unknown schema type - skip validation
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
catch {
|
|
142
|
+
// If schema conversion fails, skip validation
|
|
143
|
+
// This can happen for function schemas or other special types
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
assertNoRecordInJsonSchema(jsonSchema, [], contextName);
|
|
147
|
+
}
|
|
148
|
+
//# sourceMappingURL=validate.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"validate.js","sourceRoot":"","sources":["../../src/schema/validate.ts"],"names":[],"mappings":";;AAmJA,oDAwCC;AA1LD,qCAA8C;AAC9C,uDAAqD;AAErD;;;;;;;;GAQG;AACH,SAAS,kBAAkB,CAAC,MAAmB;IAC7C,OAAO,CACL,MAAM,CAAC,IAAI,KAAK,QAAQ;QACxB,OAAO,MAAM,CAAC,oBAAoB,KAAK,QAAQ;QAC/C,MAAM,CAAC,oBAAoB,KAAK,IAAI,CACrC,CAAC;AACJ,CAAC;AAED;;;;;;;GAOG;AACH,SAAS,0BAA0B,CACjC,MAAmB,EACnB,IAAc,EACd,WAAmB;IAEnB,IAAI,kBAAkB,CAAC,MAAM,CAAC,EAAE,CAAC;QAC/B,MAAM,MAAM,GAAG,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC;QACvD,MAAM,IAAI,KAAK,CACb,iEAAiE,WAAW,IAAI;YAC9E,kBAAkB,MAAM,KAAK;YAC7B,gDAAgD,CACnD,CAAC;IACJ,CAAC;IACD,IAAI,MAAM,CAAC,UAAU,EAAE,CAAC;QACtB,KAAK,MAAM,CAAC,GAAG,EAAE,UAAU,CAAC,IAAI,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AAG,IAAA,2BAAkB,EAAC,MAAM,CAAC,CAAC;YAE7C,yEAAyE;YACzE,gEAAgE;YAChE,IAAI,CAAC,SAAS,IAAI,MAAM,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;gBACtD,OAAO;YACT,CAAC;YAED,UAAU,GAAG,SAAS,CAAC;QACzB,CAAC;aAAM,IACL,OAAO,MAAM,KAAK,QAAQ;YAC1B,MAAM,KAAK,IAAI;YACf,MAAM,IAAI,MAAM,EAChB,CAAC;YACD,sBAAsB;YACtB,UAAU,GAAG,MAAqB,CAAC;QACrC,CAAC;aAAM,CAAC;YACN,wCAAwC;YACxC,OAAO;QACT,CAAC;IACH,CAAC;IAAC,MAAM,CAAC;QACP,8CAA8C;QAC9C,8DAA8D;QAC9D,OAAO;IACT,CAAC;IAED,0BAA0B,CAAC,UAAU,EAAE,EAAE,EAAE,WAAW,CAAC,CAAC;AAC1D,CAAC","sourcesContent":["import type { JSONSchema7 } from \"json-schema\";\nimport { schemaToJsonSchema } from \"./schema\";\nimport { isStandardSchema } from \"./standard-schema\";\n\n/*\n * Check if a JSON Schema represents a record type (map with dynamic keys).\n *\n * For our purposes, any `object` schema with `additionalProperties` defined\n * as a nested schema (not just `true`/`false`) is treated as a record, even\n * if it also declares some explicit `properties`.\n * @param schema - The JSON Schema to check\n * @returns True if the schema represents a record-like type\n */\nfunction isRecordJsonSchema(schema: JSONSchema7): boolean {\n return (\n schema.type === \"object\" &&\n typeof schema.additionalProperties === \"object\" &&\n schema.additionalProperties !== null\n );\n}\n\n/**\n * Recursively walks a JSON Schema and throws when it encounters a record type.\n * Records are not supported because they use dynamic keys which don't serialize\n * well for the Tambo backend.\n * @param schema - The JSON Schema to check\n * @param path - Current path in the schema (for error messages)\n * @param contextName - Human-readable context name for error messages\n */\nfunction assertNoRecordInJsonSchema(\n schema: JSONSchema7,\n path: string[],\n contextName: string,\n): void {\n if (isRecordJsonSchema(schema)) {\n const joined = path.length ? path.join(\".\") : \"(root)\";\n throw new Error(\n `Record types (objects with dynamic keys) are not supported in ${contextName}. ` +\n `Found at path \"${joined}\". ` +\n \"Replace it with an object using explicit keys.\",\n );\n }\n if (schema.properties) {\n for (const [key, propSchema] of Object.entries(schema.properties)) {\n if (typeof propSchema === \"object\" && propSchema !== null) {\n assertNoRecordInJsonSchema(propSchema, [...path, key], contextName);\n }\n }\n }\n if (\n typeof schema.additionalProperties === \"object\" &&\n schema.additionalProperties !== null\n ) {\n assertNoRecordInJsonSchema(\n schema.additionalProperties,\n [...path, \"[*]\"],\n contextName,\n );\n }\n if (schema.items) {\n if (Array.isArray(schema.items)) {\n // Tuple (JSON Schema draft-07): check each item schema\n schema.items.forEach((itemSchema, index) => {\n if (typeof itemSchema === \"object\" && itemSchema !== null) {\n assertNoRecordInJsonSchema(\n itemSchema,\n [...path, `${index}`],\n contextName,\n );\n }\n });\n } else if (typeof schema.items === \"object\") {\n // Array: check the item schema\n assertNoRecordInJsonSchema(schema.items, [...path, \"[]\"], contextName);\n }\n }\n const prefixItems = (schema as Record<string, unknown>).prefixItems;\n if (Array.isArray(prefixItems)) {\n prefixItems.forEach((itemSchema, index) => {\n if (typeof itemSchema === \"object\" && itemSchema !== null) {\n assertNoRecordInJsonSchema(\n itemSchema as JSONSchema7,\n [...path, `${index}`],\n contextName,\n );\n }\n });\n }\n if (schema.allOf) {\n schema.allOf.forEach((subSchema, index) => {\n if (typeof subSchema === \"object\" && subSchema !== null) {\n assertNoRecordInJsonSchema(\n subSchema,\n [...path, `&${index}`],\n contextName,\n );\n }\n });\n }\n if (schema.anyOf) {\n schema.anyOf.forEach((subSchema, index) => {\n if (typeof subSchema === \"object\" && subSchema !== null) {\n assertNoRecordInJsonSchema(\n subSchema,\n [...path, `|${index}`],\n contextName,\n );\n }\n });\n }\n if (schema.oneOf) {\n schema.oneOf.forEach((subSchema, index) => {\n if (typeof subSchema === \"object\" && subSchema !== null) {\n assertNoRecordInJsonSchema(\n subSchema,\n [...path, `|${index}`],\n contextName,\n );\n }\n });\n }\n if (typeof schema.not === \"object\" && schema.not !== null) {\n assertNoRecordInJsonSchema(schema.not, [...path, \"!\"], contextName);\n }\n if (typeof schema.if === \"object\" && schema.if !== null) {\n assertNoRecordInJsonSchema(schema.if, [...path, \"if\"], contextName);\n }\n if (typeof schema.then === \"object\" && schema.then !== null) {\n assertNoRecordInJsonSchema(schema.then, [...path, \"then\"], contextName);\n }\n if (typeof schema.else === \"object\" && schema.else !== null) {\n assertNoRecordInJsonSchema(schema.else, [...path, \"else\"], contextName);\n }\n}\n\n/**\n * Asserts that a schema does not contain record types (objects with dynamic keys).\n * Records are not serializable to JSON Schema in a way that the Tambo backend\n * understands, so they are disallowed.\n *\n * This function accepts Standard Schema validators (Zod, Valibot, ArkType, etc.)\n * or JSON Schema objects. It converts them to JSON Schema and checks for record patterns.\n *\n * For tool schemas that are function types, this extracts and validates the input parameters.\n * @param schema - The schema to validate (Standard Schema or JSON Schema)\n * @param contextName - A human-readable label echoed in the error message\n */\nexport function assertNoRecordSchema(\n schema: unknown,\n contextName = \"schema\",\n): void {\n if (!schema) {\n return;\n }\n\n let jsonSchema: JSONSchema7;\n\n try {\n if (isStandardSchema(schema)) {\n // Handle function schemas specially - extract input parameters\n const converted = schemaToJsonSchema(schema);\n\n // If it's a function schema that couldn't be converted, the input params\n // were extracted in registry.ts, so we can skip validation here\n if (!converted || Object.keys(converted).length === 0) {\n return;\n }\n\n jsonSchema = converted;\n } else if (\n typeof schema === \"object\" &&\n schema !== null &&\n \"type\" in schema\n ) {\n // Already JSON Schema\n jsonSchema = schema as JSONSchema7;\n } else {\n // Unknown schema type - skip validation\n return;\n }\n } catch {\n // If schema conversion fails, skip validation\n // This can happen for function schemas or other special types\n return;\n }\n\n assertNoRecordInJsonSchema(jsonSchema, [], contextName);\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"validate.test.d.ts","sourceRoot":"","sources":["../../src/schema/validate.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const zodInternalAlias_1 = require("zodInternalAlias");
|
|
4
|
+
const validate_1 = require("./validate");
|
|
5
|
+
describe("assertNoRecordSchema", () => {
|
|
6
|
+
it("should allow valid schemas without records", () => {
|
|
7
|
+
const schema = zodInternalAlias_1.z.object({
|
|
8
|
+
name: zodInternalAlias_1.z.string(),
|
|
9
|
+
age: zodInternalAlias_1.z.number(),
|
|
10
|
+
tags: zodInternalAlias_1.z.array(zodInternalAlias_1.z.string()),
|
|
11
|
+
address: zodInternalAlias_1.z.object({
|
|
12
|
+
street: zodInternalAlias_1.z.string(),
|
|
13
|
+
city: zodInternalAlias_1.z.string(),
|
|
14
|
+
}),
|
|
15
|
+
});
|
|
16
|
+
expect(() => (0, validate_1.assertNoRecordSchema)(schema)).not.toThrow();
|
|
17
|
+
});
|
|
18
|
+
it("should throw when encountering a record at root level", () => {
|
|
19
|
+
const schema = zodInternalAlias_1.z.record(zodInternalAlias_1.z.string(), zodInternalAlias_1.z.string());
|
|
20
|
+
expect(() => (0, validate_1.assertNoRecordSchema)(schema)).toThrow('Record types (objects with dynamic keys) are not supported in schema. Found at path "(root)". Replace it with an object using explicit keys.');
|
|
21
|
+
});
|
|
22
|
+
it("should throw when encountering a nested record", () => {
|
|
23
|
+
const schema = zodInternalAlias_1.z.object({
|
|
24
|
+
name: zodInternalAlias_1.z.string(),
|
|
25
|
+
metadata: zodInternalAlias_1.z.record(zodInternalAlias_1.z.string(), zodInternalAlias_1.z.string()),
|
|
26
|
+
});
|
|
27
|
+
expect(() => (0, validate_1.assertNoRecordSchema)(schema)).toThrow('Record types (objects with dynamic keys) are not supported in schema. Found at path "metadata". Replace it with an object using explicit keys.');
|
|
28
|
+
});
|
|
29
|
+
it("should throw when encountering a record in an array", () => {
|
|
30
|
+
const schema = zodInternalAlias_1.z.object({
|
|
31
|
+
items: zodInternalAlias_1.z.array(zodInternalAlias_1.z.record(zodInternalAlias_1.z.string(), zodInternalAlias_1.z.string())),
|
|
32
|
+
});
|
|
33
|
+
expect(() => (0, validate_1.assertNoRecordSchema)(schema)).toThrow('Record types (objects with dynamic keys) are not supported in schema. Found at path "items.[]". Replace it with an object using explicit keys.');
|
|
34
|
+
});
|
|
35
|
+
it("should throw when encountering a record in a union", () => {
|
|
36
|
+
const schema = zodInternalAlias_1.z.union([zodInternalAlias_1.z.string(), zodInternalAlias_1.z.record(zodInternalAlias_1.z.string(), zodInternalAlias_1.z.number())]);
|
|
37
|
+
expect(() => (0, validate_1.assertNoRecordSchema)(schema)).toThrow('Record types (objects with dynamic keys) are not supported in schema. Found at path "|1". Replace it with an object using explicit keys.');
|
|
38
|
+
});
|
|
39
|
+
it("should throw when encountering a record in an intersection", () => {
|
|
40
|
+
const schema = zodInternalAlias_1.z.intersection(zodInternalAlias_1.z.object({ name: zodInternalAlias_1.z.string() }), zodInternalAlias_1.z.object({ metadata: zodInternalAlias_1.z.record(zodInternalAlias_1.z.string(), zodInternalAlias_1.z.string()) }));
|
|
41
|
+
expect(() => (0, validate_1.assertNoRecordSchema)(schema)).toThrow(/Record types \(objects with dynamic keys\) are not supported in schema\. Found at path "(&1\.)?metadata"\. Replace it with an object using explicit keys\./);
|
|
42
|
+
});
|
|
43
|
+
it("should throw when encountering a record in a discriminated union", () => {
|
|
44
|
+
const schema = zodInternalAlias_1.z.discriminatedUnion("type", [
|
|
45
|
+
zodInternalAlias_1.z.object({ type: zodInternalAlias_1.z.literal("string"), value: zodInternalAlias_1.z.string() }),
|
|
46
|
+
zodInternalAlias_1.z.object({
|
|
47
|
+
type: zodInternalAlias_1.z.literal("record"),
|
|
48
|
+
value: zodInternalAlias_1.z.record(zodInternalAlias_1.z.string(), zodInternalAlias_1.z.number()),
|
|
49
|
+
}),
|
|
50
|
+
]);
|
|
51
|
+
expect(() => (0, validate_1.assertNoRecordSchema)(schema)).toThrow(/Record types \(objects with dynamic keys\) are not supported in schema\. Found at path "\|1\.value"\. Replace it with an object using explicit keys\./);
|
|
52
|
+
});
|
|
53
|
+
it("should throw when encountering a record in an optional field", () => {
|
|
54
|
+
const schema = zodInternalAlias_1.z.object({
|
|
55
|
+
optional: zodInternalAlias_1.z.optional(zodInternalAlias_1.z.record(zodInternalAlias_1.z.string(), zodInternalAlias_1.z.string())),
|
|
56
|
+
});
|
|
57
|
+
expect(() => (0, validate_1.assertNoRecordSchema)(schema)).toThrow('Record types (objects with dynamic keys) are not supported in schema. Found at path "optional". Replace it with an object using explicit keys.');
|
|
58
|
+
});
|
|
59
|
+
it("should throw when encountering a record in a nullable field", () => {
|
|
60
|
+
const schema = zodInternalAlias_1.z.object({
|
|
61
|
+
nullable: zodInternalAlias_1.z.nullable(zodInternalAlias_1.z.record(zodInternalAlias_1.z.string(), zodInternalAlias_1.z.string())),
|
|
62
|
+
});
|
|
63
|
+
// Nullable creates a oneOf in JSON Schema, so the path includes the union index
|
|
64
|
+
expect(() => (0, validate_1.assertNoRecordSchema)(schema)).toThrow(/Record types \(objects with dynamic keys\) are not supported in schema\. Found at path "nullable(\.\|0)?"\. Replace it with an object using explicit keys\./);
|
|
65
|
+
});
|
|
66
|
+
it("should throw when encountering a record in a tuple", () => {
|
|
67
|
+
const schema = zodInternalAlias_1.z.tuple([zodInternalAlias_1.z.string(), zodInternalAlias_1.z.record(zodInternalAlias_1.z.string(), zodInternalAlias_1.z.number())]);
|
|
68
|
+
expect(() => (0, validate_1.assertNoRecordSchema)(schema)).toThrow('Record types (objects with dynamic keys) are not supported in schema. Found at path "1". Replace it with an object using explicit keys.');
|
|
69
|
+
});
|
|
70
|
+
it("should use custom context name in error message", () => {
|
|
71
|
+
const schema = zodInternalAlias_1.z.record(zodInternalAlias_1.z.string(), zodInternalAlias_1.z.string());
|
|
72
|
+
expect(() => (0, validate_1.assertNoRecordSchema)(schema, "mySchema")).toThrow('Record types (objects with dynamic keys) are not supported in mySchema. Found at path "(root)". Replace it with an object using explicit keys.');
|
|
73
|
+
});
|
|
74
|
+
it("should handle ZodLazy schemas", () => {
|
|
75
|
+
const schema = zodInternalAlias_1.z.lazy(() => zodInternalAlias_1.z.record(zodInternalAlias_1.z.string(), zodInternalAlias_1.z.string()));
|
|
76
|
+
expect(() => (0, validate_1.assertNoRecordSchema)(schema)).toThrow('Record types (objects with dynamic keys) are not supported in schema. Found at path "(root)". Replace it with an object using explicit keys.');
|
|
77
|
+
});
|
|
78
|
+
it("should allow z.function() with valid arguments", () => {
|
|
79
|
+
const schema = zodInternalAlias_1.z.function({
|
|
80
|
+
input: [zodInternalAlias_1.z.string(), zodInternalAlias_1.z.number()],
|
|
81
|
+
output: zodInternalAlias_1.z.string(),
|
|
82
|
+
});
|
|
83
|
+
// Function schemas can't be converted to JSON Schema directly,
|
|
84
|
+
// so they should not throw (validation happens on the extracted parameters)
|
|
85
|
+
expect(() => (0, validate_1.assertNoRecordSchema)(schema)).not.toThrow();
|
|
86
|
+
});
|
|
87
|
+
it("should allow JSON Schema without records", () => {
|
|
88
|
+
const jsonSchema = {
|
|
89
|
+
type: "object",
|
|
90
|
+
properties: {
|
|
91
|
+
name: { type: "string" },
|
|
92
|
+
age: { type: "number" },
|
|
93
|
+
},
|
|
94
|
+
required: ["name"],
|
|
95
|
+
};
|
|
96
|
+
expect(() => (0, validate_1.assertNoRecordSchema)(jsonSchema)).not.toThrow();
|
|
97
|
+
});
|
|
98
|
+
it("should throw when JSON Schema has record pattern", () => {
|
|
99
|
+
const jsonSchema = {
|
|
100
|
+
type: "object",
|
|
101
|
+
additionalProperties: { type: "string" },
|
|
102
|
+
};
|
|
103
|
+
expect(() => (0, validate_1.assertNoRecordSchema)(jsonSchema)).toThrow('Record types (objects with dynamic keys) are not supported in schema. Found at path "(root)". Replace it with an object using explicit keys.');
|
|
104
|
+
});
|
|
105
|
+
it("should treat JSON Schema with properties and additionalProperties as a record", () => {
|
|
106
|
+
const jsonSchema = {
|
|
107
|
+
type: "object",
|
|
108
|
+
properties: {
|
|
109
|
+
id: { type: "string" },
|
|
110
|
+
},
|
|
111
|
+
additionalProperties: { type: "number" },
|
|
112
|
+
};
|
|
113
|
+
expect(() => (0, validate_1.assertNoRecordSchema)(jsonSchema)).toThrow('Record types (objects with dynamic keys) are not supported in schema. Found at path "(root)". Replace it with an object using explicit keys.');
|
|
114
|
+
});
|
|
115
|
+
it("should throw when nested JSON Schema has record pattern", () => {
|
|
116
|
+
const jsonSchema = {
|
|
117
|
+
type: "object",
|
|
118
|
+
properties: {
|
|
119
|
+
metadata: {
|
|
120
|
+
type: "object",
|
|
121
|
+
additionalProperties: { type: "string" },
|
|
122
|
+
},
|
|
123
|
+
},
|
|
124
|
+
};
|
|
125
|
+
expect(() => (0, validate_1.assertNoRecordSchema)(jsonSchema)).toThrow('Record types (objects with dynamic keys) are not supported in schema. Found at path "metadata". Replace it with an object using explicit keys.');
|
|
126
|
+
});
|
|
127
|
+
});
|
|
128
|
+
//# sourceMappingURL=validate.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"validate.test.js","sourceRoot":"","sources":["../../src/schema/validate.test.ts"],"names":[],"mappings":";;AAAA,uDAAqC;AACrC,yCAAkD;AAElD,QAAQ,CAAC,sBAAsB,EAAE,GAAG,EAAE;IACpC,EAAE,CAAC,4CAA4C,EAAE,GAAG,EAAE;QACpD,MAAM,MAAM,GAAG,oBAAC,CAAC,MAAM,CAAC;YACtB,IAAI,EAAE,oBAAC,CAAC,MAAM,EAAE;YAChB,GAAG,EAAE,oBAAC,CAAC,MAAM,EAAE;YACf,IAAI,EAAE,oBAAC,CAAC,KAAK,CAAC,oBAAC,CAAC,MAAM,EAAE,CAAC;YACzB,OAAO,EAAE,oBAAC,CAAC,MAAM,CAAC;gBAChB,MAAM,EAAE,oBAAC,CAAC,MAAM,EAAE;gBAClB,IAAI,EAAE,oBAAC,CAAC,MAAM,EAAE;aACjB,CAAC;SACH,CAAC,CAAC;QAEH,MAAM,C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,QAAiB;oBACvB,oBAAoB,EAAE,EAAE,IAAI,EAAE,QAAiB,EAAE;iBAClD;aACF;SACF,CAAC;QAEF,MAAM,CAAC,GAAG,EAAE,CAAC,IAAA,+BAAoB,EAAC,UAAU,CAAC,CAAC,CAAC,OAAO,CACpD,gJAAgJ,CACjJ,CAAC;IACJ,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC","sourcesContent":["import { z } from \"zodInternalAlias\";\nimport { assertNoRecordSchema } from \"./validate\";\n\ndescribe(\"assertNoRecordSchema\", () => {\n it(\"should allow valid schemas without records\", () => {\n const schema = z.object({\n name: z.string(),\n age: z.number(),\n tags: z.array(z.string()),\n address: z.object({\n street: z.string(),\n city: z.string(),\n }),\n });\n\n expect(() => assertNoRecordSchema(schema)).not.toThrow();\n });\n\n it(\"should throw when encountering a record at root level\", () => {\n const schema = z.record(z.string(), z.string());\n\n expect(() => assertNoRecordSchema(schema)).toThrow(\n 'Record types (objects with dynamic keys) are not supported in schema. Found at path \"(root)\". Replace it with an object using explicit keys.',\n );\n });\n\n it(\"should throw when encountering a nested record\", () => {\n const schema = z.object({\n name: z.string(),\n metadata: z.record(z.string(), z.string()),\n });\n\n expect(() => assertNoRecordSchema(schema)).toThrow(\n 'Record types (objects with dynamic keys) are not supported in schema. Found at path \"metadata\". Replace it with an object using explicit keys.',\n );\n });\n\n it(\"should throw when encountering a record in an array\", () => {\n const schema = z.object({\n items: z.array(z.record(z.string(), z.string())),\n });\n\n expect(() => assertNoRecordSchema(schema)).toThrow(\n 'Record types (objects with dynamic keys) are not supported in schema. Found at path \"items.[]\". Replace it with an object using explicit keys.',\n );\n });\n\n it(\"should throw when encountering a record in a union\", () => {\n const schema = z.union([z.string(), z.record(z.string(), z.number())]);\n\n expect(() => assertNoRecordSchema(schema)).toThrow(\n 'Record types (objects with dynamic keys) are not supported in schema. Found at path \"|1\". Replace it with an object using explicit keys.',\n );\n });\n\n it(\"should throw when encountering a record in an intersection\", () => {\n const schema = z.intersection(\n z.object({ name: z.string() }),\n z.object({ metadata: z.record(z.string(), z.string()) }),\n );\n\n expect(() => assertNoRecordSchema(schema)).toThrow(\n /Record types \\(objects with dynamic keys\\) are not supported in schema\\. Found at path \"(&1\\.)?metadata\"\\. Replace it with an object using explicit keys\\./,\n );\n });\n\n it(\"should throw when encountering a record in a discriminated union\", () => {\n const schema = z.discriminatedUnion(\"type\", [\n z.object({ type: z.literal(\"string\"), value: z.string() }),\n z.object({\n type: z.literal(\"record\"),\n value: z.record(z.string(), z.number()),\n }),\n ]);\n\n expect(() => assertNoRecordSchema(schema)).toThrow(\n /Record types \\(objects with dynamic keys\\) are not supported in schema\\. Found at path \"\\|1\\.value\"\\. Replace it with an object using explicit keys\\./,\n );\n });\n\n it(\"should throw when encountering a record in an optional field\", () => {\n const schema = z.object({\n optional: z.optional(z.record(z.string(), z.string())),\n });\n\n expect(() => assertNoRecordSchema(schema)).toThrow(\n 'Record types (objects with dynamic keys) are not supported in schema. Found at path \"optional\". Replace it with an object using explicit keys.',\n );\n });\n\n it(\"should throw when encountering a record in a nullable field\", () => {\n const schema = z.object({\n nullable: z.nullable(z.record(z.string(), z.string())),\n });\n\n // Nullable creates a oneOf in JSON Schema, so the path includes the union index\n expect(() => assertNoRecordSchema(schema)).toThrow(\n /Record types \\(objects with dynamic keys\\) are not supported in schema\\. Found at path \"nullable(\\.\\|0)?\"\\. Replace it with an object using explicit keys\\./,\n );\n });\n\n it(\"should throw when encountering a record in a tuple\", () => {\n const schema = z.tuple([z.string(), z.record(z.string(), z.number())]);\n\n expect(() => assertNoRecordSchema(schema)).toThrow(\n 'Record types (objects with dynamic keys) are not supported in schema. Found at path \"1\". Replace it with an object using explicit keys.',\n );\n });\n\n it(\"should use custom context name in error message\", () => {\n const schema = z.record(z.string(), z.string());\n\n expect(() => assertNoRecordSchema(schema, \"mySchema\")).toThrow(\n 'Record types (objects with dynamic keys) are not supported in mySchema. Found at path \"(root)\". Replace it with an object using explicit keys.',\n );\n });\n\n it(\"should handle ZodLazy schemas\", () => {\n const schema = z.lazy(() => z.record(z.string(), z.string()));\n\n expect(() => assertNoRecordSchema(schema)).toThrow(\n 'Record types (objects with dynamic keys) are not supported in schema. Found at path \"(root)\". Replace it with an object using explicit keys.',\n );\n });\n\n it(\"should allow z.function() with valid arguments\", () => {\n const schema = z.function({\n input: [z.string(), z.number()],\n output: z.string(),\n });\n\n // Function schemas can't be converted to JSON Schema directly,\n // so they should not throw (validation happens on the extracted parameters)\n expect(() => assertNoRecordSchema(schema)).not.toThrow();\n });\n\n it(\"should allow JSON Schema without records\", () => {\n const jsonSchema = {\n type: \"object\" as const,\n properties: {\n name: { type: \"string\" as const },\n age: { type: \"number\" as const },\n },\n required: [\"name\"],\n };\n\n expect(() => assertNoRecordSchema(jsonSchema)).not.toThrow();\n });\n\n it(\"should throw when JSON Schema has record pattern\", () => {\n const jsonSchema = {\n type: \"object\" as const,\n additionalProperties: { type: \"string\" as const },\n };\n\n expect(() => assertNoRecordSchema(jsonSchema)).toThrow(\n 'Record types (objects with dynamic keys) are not supported in schema. Found at path \"(root)\". Replace it with an object using explicit keys.',\n );\n });\n\n it(\"should treat JSON Schema with properties and additionalProperties as a record\", () => {\n const jsonSchema = {\n type: \"object\" as const,\n properties: {\n id: { type: \"string\" as const },\n },\n additionalProperties: { type: \"number\" as const },\n };\n\n expect(() => assertNoRecordSchema(jsonSchema)).toThrow(\n 'Record types (objects with dynamic keys) are not supported in schema. Found at path \"(root)\". Replace it with an object using explicit keys.',\n );\n });\n\n it(\"should throw when nested JSON Schema has record pattern\", () => {\n const jsonSchema = {\n type: \"object\" as const,\n properties: {\n metadata: {\n type: \"object\" as const,\n additionalProperties: { type: \"string\" as const },\n },\n },\n };\n\n expect(() => assertNoRecordSchema(jsonSchema)).toThrow(\n 'Record types (objects with dynamic keys) are not supported in schema. Found at path \"metadata\". Replace it with an object using explicit keys.',\n );\n });\n});\n"]}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { ZodFunction, ZodTuple, ZodTupleItems, ZodType, ZodTypeAny } from "zod/v3";
|
|
2
|
+
import { $ZodFunction, $ZodType } from "zod/v4/core";
|
|
3
|
+
/**
|
|
4
|
+
* @returns True if the schema is a Zod 3 function schema
|
|
5
|
+
*/
|
|
6
|
+
export declare function isZod3FunctionSchema(schema: unknown): schema is ZodFunction<ZodTuple<ZodTupleItems, any>, ZodTypeAny>;
|
|
7
|
+
/**
|
|
8
|
+
* @returns True if the schema is a Zod 4 function schema
|
|
9
|
+
*/
|
|
10
|
+
export declare function isZod4FunctionSchema(schema: unknown): schema is $ZodFunction;
|
|
11
|
+
/**
|
|
12
|
+
* Checks if a schema is a Zod function schema (Zod 3 or Zod 4).
|
|
13
|
+
* @param schema - The schema to check
|
|
14
|
+
* @returns True if the schema is a Zod function schema
|
|
15
|
+
*/
|
|
16
|
+
export declare function isZodFunctionSchema(schema: unknown): schema is ZodFunction<ZodTuple<ZodTupleItems, any>, ZodTypeAny> | $ZodFunction<import("zod/v4/core").$ZodFunctionArgs, import("zod/v4/core").$ZodFunctionOut>;
|
|
17
|
+
/**
|
|
18
|
+
* Extracts the args schema from a Zod function schema.
|
|
19
|
+
* @param schema - The Zod function schema
|
|
20
|
+
* @returns The args schema, or undefined if not a Zod function schema
|
|
21
|
+
*/
|
|
22
|
+
export declare function getZodFunctionArgs(schema: unknown): import("zod/v4/core").$ZodFunctionArgs | ZodTuple<ZodTupleItems, any>;
|
|
23
|
+
/**
|
|
24
|
+
* Extracts the return schema from a Zod 3 or Zod 4 function schema.
|
|
25
|
+
* @param schema - The Zod function schema
|
|
26
|
+
* @returns The return schema, or undefined if not a Zod function schema
|
|
27
|
+
*/
|
|
28
|
+
export declare function getZodFunctionReturns(schema: unknown): any;
|
|
29
|
+
/**
|
|
30
|
+
* Handles conversion of Zod schemas to JSON Schema.
|
|
31
|
+
* Supports both Zod 3 (via zod-to-json-schema) and Zod 4 (native).
|
|
32
|
+
* @param schema - The Zod schema to convert
|
|
33
|
+
* @returns The JSON Schema representation
|
|
34
|
+
*/
|
|
35
|
+
export declare function handleZodSchemaToJson(schema: unknown): any;
|
|
36
|
+
/**
|
|
37
|
+
* Checks if a schema is probably a Zod 3 schema.
|
|
38
|
+
* @param schema - The schema to check
|
|
39
|
+
* @returns True if the schema looks like a Zod 3 schema
|
|
40
|
+
*/
|
|
41
|
+
export declare function isZod3Schema(schema: unknown): schema is ZodType;
|
|
42
|
+
/**
|
|
43
|
+
* Detects if a schema is Zod 4 by checking for def.type property.
|
|
44
|
+
* Zod 4 uses `def.type` with lowercase type names like "object", "string".
|
|
45
|
+
* @returns True if the schema appears to be Zod 4 style
|
|
46
|
+
*/
|
|
47
|
+
export declare function isZod4Schema(schema: unknown): schema is $ZodType;
|
|
48
|
+
/**
|
|
49
|
+
* Checks if a schema is a Zod schema
|
|
50
|
+
* @param schema - The schema to check
|
|
51
|
+
* @returns True if the schema is a Zod schema
|
|
52
|
+
*/
|
|
53
|
+
export declare function isZodSchema(schema: unknown): ReturnType<typeof isZod3Schema> | ReturnType<typeof isZod4Schema>;
|
|
54
|
+
//# sourceMappingURL=zod.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"zod.d.ts","sourceRoot":"","sources":["../../src/schema/zod.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,WAAW,EACX,QAAQ,EACR,aAAa,EACb,OAAO,EACP,UAAU,EACX,MAAM,QAAQ,CAAC;AAChB,OAAO,EACL,YAAY,EACZ,QAAQ,EAET,MAAM,aAAa,CAAC;AAGrB;;GAEG;AACH,wBAAgB,oBAAoB,CAClC,MAAM,EAAE,OAAO,GACd,MAAM,IAAI,WAAW,CAAC,QAAQ,CAAC,aAAa,EAAE,GAAG,CAAC,EAAE,UAAU,CAAC,CAMjE;AAED;;GAEG;AACH,wBAAgB,oBAAoB,CAAC,MAAM,EAAE,OAAO,GAAG,MAAM,IAAI,YAAY,CAoB5E;AAED;;;;GAIG;AACH,wBAAgB,mBAAmB,CAAC,MAAM,EAAE,OAAO,iKAElD;AAED;;;;GAIG;AACH,wBAAgB,kBAAkB,CAAC,MAAM,EAAE,OAAO,yEAWjD;AAED;;;;GAIG;AACH,wBAAgB,qBAAqB,CAAC,MAAM,EAAE,OAAO,OAWpD;AAED;;;;;GAKG;AACH,wBAAgB,qBAAqB,CAAC,MAAM,EAAE,OAAO,OAkBpD;AAaD;;;;GAIG;AACH,wBAAgB,YAAY,CAAC,MAAM,EAAE,OAAO,GAAG,MAAM,IAAI,OAAO,CAE/D;AAUD;;;;GAIG;AACH,wBAAgB,YAAY,CAAC,MAAM,EAAE,OAAO,GAAG,MAAM,IAAI,QAAQ,CAEhE;AAED;;;;GAIG;AACH,wBAAgB,WAAW,CACzB,MAAM,EAAE,OAAO,GACd,UAAU,CAAC,OAAO,YAAY,CAAC,GAAG,UAAU,CAAC,OAAO,YAAY,CAAC,CAEnE"}
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isZod3FunctionSchema = isZod3FunctionSchema;
|
|
4
|
+
exports.isZod4FunctionSchema = isZod4FunctionSchema;
|
|
5
|
+
exports.isZodFunctionSchema = isZodFunctionSchema;
|
|
6
|
+
exports.getZodFunctionArgs = getZodFunctionArgs;
|
|
7
|
+
exports.getZodFunctionReturns = getZodFunctionReturns;
|
|
8
|
+
exports.handleZodSchemaToJson = handleZodSchemaToJson;
|
|
9
|
+
exports.isZod3Schema = isZod3Schema;
|
|
10
|
+
exports.isZod4Schema = isZod4Schema;
|
|
11
|
+
exports.isZodSchema = isZodSchema;
|
|
12
|
+
const core_1 = require("zod/v4/core");
|
|
13
|
+
const zodInternalAlias_1 = require("zodInternalAlias");
|
|
14
|
+
/**
|
|
15
|
+
* @returns True if the schema is a Zod 3 function schema
|
|
16
|
+
*/
|
|
17
|
+
function isZod3FunctionSchema(schema) {
|
|
18
|
+
if (!isZod3Schema(schema)) {
|
|
19
|
+
return false;
|
|
20
|
+
}
|
|
21
|
+
return "typeName" in schema._def && schema._def.typeName === "ZodFunction";
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* @returns True if the schema is a Zod 4 function schema
|
|
25
|
+
*/
|
|
26
|
+
function isZod4FunctionSchema(schema) {
|
|
27
|
+
if (!isZod4Schema(schema)) {
|
|
28
|
+
return false;
|
|
29
|
+
}
|
|
30
|
+
// $ZodFunctions in zod 4.0 most certainly do have .def.type === "function"
|
|
31
|
+
// but the type definitions do not reflect this because $ZodFunction does not
|
|
32
|
+
// extend $ZodType. This is corrected in zod 4.1, but zod 3 does not include
|
|
33
|
+
// the types for 4.1 yet (and might not ever).
|
|
34
|
+
// See: https://github.com/colinhacks/zod/blob/463f03eb8183dcdcdf735b180f2bf40883e66220/packages/zod/src/v4/core/function.ts#L48
|
|
35
|
+
if ("def" in schema && typeof schema.def !== "object" && schema.def) {
|
|
36
|
+
if (schema.def?.type === "function") {
|
|
37
|
+
return true;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
// We're casting as string here because zod 4.0 types don't include "function"
|
|
41
|
+
// literal in the union for `_zod.def.type`. The optional chaining is because
|
|
42
|
+
// _zod does not exist on zod 4 objects from zod 3.
|
|
43
|
+
return schema._zod?.def?.type === "function";
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Checks if a schema is a Zod function schema (Zod 3 or Zod 4).
|
|
47
|
+
* @param schema - The schema to check
|
|
48
|
+
* @returns True if the schema is a Zod function schema
|
|
49
|
+
*/
|
|
50
|
+
function isZodFunctionSchema(schema) {
|
|
51
|
+
return isZod3FunctionSchema(schema) || isZod4FunctionSchema(schema);
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Extracts the args schema from a Zod function schema.
|
|
55
|
+
* @param schema - The Zod function schema
|
|
56
|
+
* @returns The args schema, or undefined if not a Zod function schema
|
|
57
|
+
*/
|
|
58
|
+
function getZodFunctionArgs(schema) {
|
|
59
|
+
if (isZod3FunctionSchema(schema)) {
|
|
60
|
+
return schema._def.args;
|
|
61
|
+
}
|
|
62
|
+
if (isZod4FunctionSchema(schema)) {
|
|
63
|
+
// @ts-expect-error -- Error in Zod types for v3 vs v4.0
|
|
64
|
+
return schema?.def?.input ?? schema._zod?.def?.input;
|
|
65
|
+
}
|
|
66
|
+
throw new Error("Unable to determine parameters from zod function schema");
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Extracts the return schema from a Zod 3 or Zod 4 function schema.
|
|
70
|
+
* @param schema - The Zod function schema
|
|
71
|
+
* @returns The return schema, or undefined if not a Zod function schema
|
|
72
|
+
*/
|
|
73
|
+
function getZodFunctionReturns(schema) {
|
|
74
|
+
if (isZod3FunctionSchema(schema)) {
|
|
75
|
+
return schema._def.returns;
|
|
76
|
+
}
|
|
77
|
+
if (isZod4FunctionSchema(schema)) {
|
|
78
|
+
// @ts-expect-error -- Error in Zod types for v3 vs v4
|
|
79
|
+
return schema._zod.def.output;
|
|
80
|
+
}
|
|
81
|
+
throw new Error("Unable to determine return type from zod function schema");
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Handles conversion of Zod schemas to JSON Schema.
|
|
85
|
+
* Supports both Zod 3 (via zod-to-json-schema) and Zod 4 (native).
|
|
86
|
+
* @param schema - The Zod schema to convert
|
|
87
|
+
* @returns The JSON Schema representation
|
|
88
|
+
*/
|
|
89
|
+
function handleZodSchemaToJson(schema) {
|
|
90
|
+
// If Zod4 schema detected, use the toJSONSchema function from "zod/v4/core"
|
|
91
|
+
if (isZod4Schema(schema))
|
|
92
|
+
return (0, core_1.toJSONSchema)(schema);
|
|
93
|
+
try {
|
|
94
|
+
// Dynamic require for optional peer dependency
|
|
95
|
+
// eslint-disable-next-line @typescript-eslint/no-require-imports -- need require because zod-to-json-schema may be missing
|
|
96
|
+
const { zodToJsonSchema } = require("zod-to-json-schema");
|
|
97
|
+
return zodToJsonSchema(schema);
|
|
98
|
+
}
|
|
99
|
+
catch (error) {
|
|
100
|
+
throw new Error("Zod 3 requires 'zod-to-json-schema' package for JSON Schema conversion. " +
|
|
101
|
+
"Install it with: npm install zod-to-json-schema", {
|
|
102
|
+
cause: error,
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
// Zod 3 schemas have _def with typeName and ~standard with vendor "zod"
|
|
107
|
+
// Use looseObject to allow any _def shape (different for each type)
|
|
108
|
+
const zod3SchemaType = zodInternalAlias_1.z.object({
|
|
109
|
+
_def: zodInternalAlias_1.z.looseObject({
|
|
110
|
+
typeName: zodInternalAlias_1.z.string().optional(),
|
|
111
|
+
}),
|
|
112
|
+
"~standard": zodInternalAlias_1.z.looseObject({
|
|
113
|
+
vendor: zodInternalAlias_1.z.literal("zod"),
|
|
114
|
+
}),
|
|
115
|
+
});
|
|
116
|
+
/**
|
|
117
|
+
* Checks if a schema is probably a Zod 3 schema.
|
|
118
|
+
* @param schema - The schema to check
|
|
119
|
+
* @returns True if the schema looks like a Zod 3 schema
|
|
120
|
+
*/
|
|
121
|
+
function isZod3Schema(schema) {
|
|
122
|
+
return zod3SchemaType.safeParse(schema).success;
|
|
123
|
+
}
|
|
124
|
+
const zod4SchemaType = zodInternalAlias_1.z.object({
|
|
125
|
+
def: zodInternalAlias_1.z.object(),
|
|
126
|
+
_zod: zodInternalAlias_1.z.looseObject({
|
|
127
|
+
def: zodInternalAlias_1.z.looseObject({}),
|
|
128
|
+
}),
|
|
129
|
+
"~standard": zodInternalAlias_1.z.looseObject({}),
|
|
130
|
+
});
|
|
131
|
+
/**
|
|
132
|
+
* Detects if a schema is Zod 4 by checking for def.type property.
|
|
133
|
+
* Zod 4 uses `def.type` with lowercase type names like "object", "string".
|
|
134
|
+
* @returns True if the schema appears to be Zod 4 style
|
|
135
|
+
*/
|
|
136
|
+
function isZod4Schema(schema) {
|
|
137
|
+
return zod4SchemaType.safeParse(schema).success;
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Checks if a schema is a Zod schema
|
|
141
|
+
* @param schema - The schema to check
|
|
142
|
+
* @returns True if the schema is a Zod schema
|
|
143
|
+
*/
|
|
144
|
+
function isZodSchema(schema) {
|
|
145
|
+
return isZod3Schema(schema) || isZod4Schema(schema);
|
|
146
|
+
}
|
|
147
|
+
//# sourceMappingURL=zod.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"zod.js","sourceRoot":"","sources":["../../src/schema/zod.ts"],"names":[],"mappings":";;AAiBA,oDAQC;AAKD,oDAoBC;AAOD,kDAEC;AAOD,gDAWC;AAOD,sDAWC;AAQD,sDAkBC;AAkBD,oCAEC;AAeD,oCAEC;AAOD,kCAIC;AAlKD,sCAIqB;AACrB,uDAAqC;AAErC;;GAEG;AACH,SAAgB,oBAAoB,CAClC,MAAe;IAEf,IAAI,CAAC,YAAY,CAAC,MAAM,CAAC,EAAE,CAAC;QAC1B,OAAO,KAAK,CAAC;IACf,CAAC;IAED,OAAO,UAAU,IAAI,MAAM,CAAC,IAAI,IAAI,MAAM,CAAC,IAAI,CAAC,QAAQ,KAAK,aAAa,CAAC;AAC7E,CAAC;AAED;;GAEG;AACH,SAAgB,oBAAoB,CAAC,MAAe;IAClD,IAAI,CAAC,YAAY,CAAC,MAAM,CAAC,EAAE,CAAC;QAC1B,OAAO,KAAK,CAAC;IACf,CAAC;IAED,2EAA2E;IAC3E,6EAA6E;IAC7E,4EAA4E;IAC5E,8CAA8C;IAC9C,gIAAgI;IAChI,IAAI,KAAK,IAAI,MAAM,IAAI,OAAO,MAAM,CAAC,GAAG,KAAK,QAAQ,IAAI,MAAM,CAAC,GAAG,EAAE,CAAC;QACpE,IAAK,MAAM,CAAC,GAAwB,EAAE,IAAI,KAAK,UAAU,EAAE,CAAC;YAC1D,OAAO,IAAI,CAAC;QACd,CAAC;IACH,CAAC;IAED,8EAA8E;IAC9E,6EAA6E;IAC7E,mDAAmD;IACnD,OAAQ,MAAM,CAAC,IAAI,EAAE,GAAG,EAAE,IAAe,KAAK,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\n $ZodType,\n toJSONSchema as zod4ToJSONSchema,\n} from \"zod/v4/core\";\nimport { z } from \"zodInternalAlias\";\n\n/**\n * @returns True if the schema is a Zod 3 function schema\n */\nexport function isZod3FunctionSchema(\n schema: unknown,\n): schema is ZodFunction<ZodTuple<ZodTupleItems, any>, ZodTypeAny> {\n if (!isZod3Schema(schema)) {\n return false;\n }\n\n return \"typeName\" in schema._def && schema._def.typeName === \"ZodFunction\";\n}\n\n/**\n * @returns True if the schema is a Zod 4 function schema\n */\nexport function isZod4FunctionSchema(schema: unknown): schema is $ZodFunction {\n if (!isZod4Schema(schema)) {\n return false;\n }\n\n // $ZodFunctions in zod 4.0 most certainly do have .def.type === \"function\"\n // but the type definitions do not reflect this because $ZodFunction does not\n // extend $ZodType. This is corrected in zod 4.1, but zod 3 does not include\n // the types for 4.1 yet (and might not ever).\n // See: https://github.com/colinhacks/zod/blob/463f03eb8183dcdcdf735b180f2bf40883e66220/packages/zod/src/v4/core/function.ts#L48\n if (\"def\" in schema && typeof schema.def !== \"object\" && schema.def) {\n if ((schema.def as { type: string })?.type === \"function\") {\n return true;\n }\n }\n\n // We're casting as string here because zod 4.0 types don't include \"function\"\n // literal in the union for `_zod.def.type`. The optional chaining is because\n // _zod does not exist on zod 4 objects from zod 3.\n return (schema._zod?.def?.type as string) === \"function\";\n}\n\n/**\n * Checks if a schema is a Zod function schema (Zod 3 or Zod 4).\n * @param schema - The schema to check\n * @returns True if the schema is a Zod function schema\n */\nexport function isZodFunctionSchema(schema: unknown) {\n return isZod3FunctionSchema(schema) || isZod4FunctionSchema(schema);\n}\n\n/**\n * Extracts the args schema from a Zod function schema.\n * @param schema - The Zod function schema\n * @returns The args schema, or undefined if not a Zod function schema\n */\nexport function getZodFunctionArgs(schema: unknown) {\n if (isZod3FunctionSchema(schema)) {\n return schema._def.args;\n }\n\n if (isZod4FunctionSchema(schema)) {\n // @ts-expect-error -- Error in Zod types for v3 vs v4.0\n return schema?.def?.input ?? schema._zod?.def?.input;\n }\n\n throw new Error(\"Unable to determine parameters from zod function schema\");\n}\n\n/**\n * Extracts the return schema from a Zod 3 or Zod 4 function schema.\n * @param schema - The Zod function schema\n * @returns The return schema, or undefined if not a Zod function schema\n */\nexport function getZodFunctionReturns(schema: unknown) {\n if (isZod3FunctionSchema(schema)) {\n return schema._def.returns;\n }\n\n if (isZod4FunctionSchema(schema)) {\n // @ts-expect-error -- Error in Zod types for v3 vs v4\n return schema._zod.def.output;\n }\n\n throw new Error(\"Unable to determine return type from zod function schema\");\n}\n\n/**\n * Handles conversion of Zod schemas to JSON Schema.\n * Supports both Zod 3 (via zod-to-json-schema) and Zod 4 (native).\n * @param schema - The Zod schema to convert\n * @returns The JSON Schema representation\n */\nexport function handleZodSchemaToJson(schema: unknown) {\n // If Zod4 schema detected, use the toJSONSchema function from \"zod/v4/core\"\n if (isZod4Schema(schema)) return zod4ToJSONSchema(schema);\n\n try {\n // Dynamic require for optional peer dependency\n // eslint-disable-next-line @typescript-eslint/no-require-imports -- need require because zod-to-json-schema may be missing\n const { zodToJsonSchema } = require(\"zod-to-json-schema\");\n return zodToJsonSchema(schema);\n } catch (error) {\n throw new Error(\n \"Zod 3 requires 'zod-to-json-schema' package for JSON Schema conversion. \" +\n \"Install it with: npm install zod-to-json-schema\",\n {\n cause: error,\n },\n );\n }\n}\n\n// Zod 3 schemas have _def with typeName and ~standard with vendor \"zod\"\n// Use looseObject to allow any _def shape (different for each type)\nconst zod3SchemaType = z.object({\n _def: z.looseObject({\n typeName: z.string().optional(),\n }),\n \"~standard\": z.looseObject({\n vendor: z.literal(\"zod\"),\n }),\n});\n\n/**\n * Checks if a schema is probably a Zod 3 schema.\n * @param schema - The schema to check\n * @returns True if the schema looks like a Zod 3 schema\n */\nexport function isZod3Schema(schema: unknown): schema is ZodType {\n return zod3SchemaType.safeParse(schema).success;\n}\n\nconst zod4SchemaType = z.object({\n def: z.object(),\n _zod: z.looseObject({\n def: z.looseObject({}),\n }),\n \"~standard\": z.looseObject({}),\n});\n\n/**\n * Detects if a schema is Zod 4 by checking for def.type property.\n * Zod 4 uses `def.type` with lowercase type names like \"object\", \"string\".\n * @returns True if the schema appears to be Zod 4 style\n */\nexport function isZod4Schema(schema: unknown): schema is $ZodType {\n return zod4SchemaType.safeParse(schema).success;\n}\n\n/**\n * Checks if a schema is a Zod schema\n * @param schema - The schema to check\n * @returns True if the schema is a Zod schema\n */\nexport function isZodSchema(\n schema: unknown,\n): ReturnType<typeof isZod3Schema> | ReturnType<typeof isZod4Schema> {\n return isZod3Schema(schema) || isZod4Schema(schema);\n}\n"]}
|
package/dist/testing/tools.d.ts
CHANGED
|
@@ -1,25 +1,39 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { TamboToolWithToolSchema } from "../model/component-metadata";
|
|
2
|
+
import { TamboComponent, TamboTool } from "../providers";
|
|
2
3
|
/**
|
|
3
|
-
* Serializes the registry for testing purposes
|
|
4
|
+
* Serializes the registry for testing purposes.
|
|
5
|
+
* Converts Standard Schema validators to JSON Schema format.
|
|
6
|
+
* Uses the same logic as production code via mapTamboToolToContextTool.
|
|
4
7
|
* @param mockRegistry - The registry to serialize
|
|
5
|
-
* @returns The serialized registry
|
|
8
|
+
* @returns The serialized registry with JSON Schema representations
|
|
6
9
|
*/
|
|
7
10
|
export declare function serializeRegistry(mockRegistry: TamboComponent[]): {
|
|
8
|
-
props: import("
|
|
9
|
-
|
|
10
|
-
definitions?: {
|
|
11
|
-
[key: string]: import("zod-to-json-schema").JsonSchema7Type;
|
|
12
|
-
} | undefined;
|
|
13
|
-
};
|
|
14
|
-
contextTools: {
|
|
15
|
-
parameters: any;
|
|
16
|
-
name: string;
|
|
17
|
-
description: string;
|
|
18
|
-
transformToContent?: ((result: any) => Promise<import("@tambo-ai/typescript-sdk/resources/beta").ChatCompletionContentPart[]> | import("@tambo-ai/typescript-sdk/resources/beta").ChatCompletionContentPart[]) | undefined;
|
|
19
|
-
}[] | undefined;
|
|
11
|
+
props: import("json-schema").JSONSchema7 | undefined;
|
|
12
|
+
contextTools: import("../model/component-metadata").ComponentContextToolMetadata[] | undefined;
|
|
20
13
|
name: string;
|
|
21
14
|
description: string;
|
|
22
15
|
propsDefinition?: any;
|
|
23
16
|
loadingComponent?: import("react").ComponentType<any>;
|
|
24
17
|
}[];
|
|
18
|
+
interface CreateMockToolOptions {
|
|
19
|
+
inputSchema: TamboTool["inputSchema"];
|
|
20
|
+
outputSchema?: TamboTool["outputSchema"];
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Creates a mock TamboTool with the given input schema for testing purposes.
|
|
24
|
+
* Accepts either an inputSchema directly or an options object with inputSchema and outputSchema.
|
|
25
|
+
* @param schemaOrOptions - The input schema or options object
|
|
26
|
+
* @returns A mock TamboTool instance
|
|
27
|
+
* @internal
|
|
28
|
+
*/
|
|
29
|
+
export declare function createMockTool(schemaOrOptions: TamboTool["inputSchema"] | CreateMockToolOptions): TamboTool;
|
|
30
|
+
/**
|
|
31
|
+
* Creates a mock TamboToolWithToolSchema for testing purposes.
|
|
32
|
+
* Does NOT adapt to inputSchema format - preserves the deprecated toolSchema interface.
|
|
33
|
+
* @param toolSchema - The tool schema for the tool
|
|
34
|
+
* @returns A mock TamboToolWithToolSchema instance (NOT adapted)
|
|
35
|
+
* @internal
|
|
36
|
+
*/
|
|
37
|
+
export declare function createMockToolWithToolSchema(toolSchema: TamboToolWithToolSchema["toolSchema"]): TamboToolWithToolSchema;
|
|
38
|
+
export {};
|
|
25
39
|
//# sourceMappingURL=tools.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tools.d.ts","sourceRoot":"","sources":["../../src/testing/tools.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"tools.d.ts","sourceRoot":"","sources":["../../src/testing/tools.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,uBAAuB,EAAE,MAAM,6BAA6B,CAAC;AACtE,OAAO,EAAE,cAAc,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAOzD;;;;;;GAMG;AACH,wBAAgB,iBAAiB,CAAC,YAAY,EAAE,cAAc,EAAE;;;;;;;IAiB/D;AAED,UAAU,qBAAqB;IAC7B,WAAW,EAAE,SAAS,CAAC,aAAa,CAAC,CAAC;IACtC,YAAY,CAAC,EAAE,SAAS,CAAC,cAAc,CAAC,CAAC;CAC1C;AAGD;;;;;;GAMG;AACH,wBAAgB,cAAc,CAC5B,eAAe,EAAE,SAAS,CAAC,aAAa,CAAC,GAAG,qBAAqB,GAChE,SAAS,CAiCX;AAGD;;;;;;GAMG;AACH,wBAAgB,4BAA4B,CAC1C,UAAU,EAAE,uBAAuB,CAAC,YAAY,CAAC,GAChD,uBAAuB,CAOzB"}
|