Skip to content

Commit 33f69eb

Browse files
committed
improve example
1 parent 43a7a1e commit 33f69eb

File tree

1 file changed

+19
-41
lines changed

1 file changed

+19
-41
lines changed

src/examples/strictTypesExample.ts

Lines changed: 19 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
/**
2-
* Example showing the difference between regular types and strict types
2+
* Example showing the difference between extensible types and safe types
3+
*
4+
* - Extensible types (types.js): Use .passthrough() - keep all fields
5+
* - Safe types (strictTypes.js): Use .strip() - remove unknown fields
36
*/
47

5-
import { ToolSchema as OpenToolSchema } from "../types.js";
6-
import { ToolSchema as StrictToolSchema } from "../strictTypes.js";
8+
import { ToolSchema as ExtensibleToolSchema } from "../types.js";
9+
import { ToolSchema } from "../strictTypes.js";
710

8-
// With regular (open) types - this is valid
9-
const openTool = OpenToolSchema.parse({
11+
const toolData = {
1012
name: "get-weather",
1113
description: "Get weather for a location",
1214
inputSchema: {
@@ -15,43 +17,19 @@ const openTool = OpenToolSchema.parse({
1517
location: { type: "string" }
1618
}
1719
},
18-
// Extra properties are allowed
19-
customField: "This is allowed in open types",
20-
anotherExtra: 123
21-
});
20+
// Extra properties that aren't in the schema
21+
customField: "This is an extension",
22+
};
2223

23-
console.log("Open tool accepts extra properties:", openTool);
24+
// With extensible types - ALL fields are preserved
25+
const extensibleTool = ExtensibleToolSchema.parse(toolData);
2426

25-
// With strict types - this would throw an error
26-
try {
27-
StrictToolSchema.parse({
28-
name: "get-weather",
29-
description: "Get weather for a location",
30-
inputSchema: {
31-
type: "object",
32-
properties: {
33-
location: { type: "string" }
34-
}
35-
},
36-
// Extra properties cause validation to fail
37-
customField: "This is NOT allowed in strict types",
38-
anotherExtra: 123
39-
});
40-
} catch (error) {
41-
console.log("Strict tool rejects extra properties:", error instanceof Error ? error.message : String(error));
42-
}
27+
console.log("Extensible tool keeps ALL properties:");
28+
console.log("- name:", extensibleTool.name);
29+
console.log("- customField:", (extensibleTool as any).customField); // "This is an extension"
4330

44-
// Correct usage with strict types
45-
const strictToolCorrect = StrictToolSchema.parse({
46-
name: "get-weather",
47-
description: "Get weather for a location",
48-
inputSchema: {
49-
type: "object",
50-
properties: {
51-
location: { type: "string" }
52-
}
53-
}
54-
// No extra properties
55-
});
31+
// With safe types - unknown fields are silently stripped
32+
const safeTool = ToolSchema.parse(toolData);
5633

57-
console.log("Strict tool with no extra properties:", strictToolCorrect);
34+
console.log("\nSafe tool strips unknown properties:");
35+
console.log("- customField:", (safeTool as any).customField); // undefined (stripped)

0 commit comments

Comments
 (0)