Skip to content

Commit

Permalink
core[patch]: Add isZodSchema to core utils
Browse files Browse the repository at this point in the history
  • Loading branch information
bracesproul committed Mar 30, 2024
1 parent 4a5cd29 commit c31a6af
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 0 deletions.
4 changes: 4 additions & 0 deletions langchain-core/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,10 @@ utils/beta_warning.cjs
utils/beta_warning.js
utils/beta_warning.d.ts
utils/beta_warning.d.cts
utils/is_zod_schema.cjs
utils/is_zod_schema.js
utils/is_zod_schema.d.ts
utils/is_zod_schema.d.cts
vectorstores.cjs
vectorstores.js
vectorstores.d.ts
Expand Down
1 change: 1 addition & 0 deletions langchain-core/langchain.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export const config = {
"utils/tiktoken": "utils/tiktoken",
"utils/types": "utils/types",
"utils/beta_warning": "utils/beta_warning",
"utils/is_zod_schema": "utils/is_zod_schema",
vectorstores: "vectorstores",
},
tsConfigPath: resolve("./tsconfig.json"),
Expand Down
13 changes: 13 additions & 0 deletions langchain-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,15 @@
"import": "./utils/beta_warning.js",
"require": "./utils/beta_warning.cjs"
},
"./utils/is_zod_schema": {
"types": {
"import": "./utils/is_zod_schema.d.ts",
"require": "./utils/is_zod_schema.d.cts",
"default": "./utils/is_zod_schema.d.ts"
},
"import": "./utils/is_zod_schema.js",
"require": "./utils/is_zod_schema.cjs"
},
"./vectorstores": {
"types": {
"import": "./vectorstores.d.ts",
Expand Down Expand Up @@ -743,6 +752,10 @@
"utils/beta_warning.js",
"utils/beta_warning.d.ts",
"utils/beta_warning.d.cts",
"utils/is_zod_schema.cjs",
"utils/is_zod_schema.js",
"utils/is_zod_schema.d.ts",
"utils/is_zod_schema.d.cts",
"vectorstores.cjs",
"vectorstores.js",
"vectorstores.d.ts",
Expand Down
18 changes: 18 additions & 0 deletions langchain-core/src/utils/is_zod_schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { type z } from "zod";

/**
* Given either a Zod schema, or plain object, determine if the input is a Zod schema.
*
* @param {z.ZodType<RunOutput> | Record<string, any>} input
* @returns {boolean} Whether or not the provided input is a Zod schema.
*/
export function isZodSchema<
// eslint-disable-next-line @typescript-eslint/no-explicit-any
RunOutput extends Record<string, any> = Record<string, any>
>(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
input: z.ZodType<RunOutput> | Record<string, any>
): input is z.ZodType<RunOutput> {
// Check for a characteristic method of Zod schemas
return typeof (input as z.ZodType<RunOutput>)?.parse === "function";
}

0 comments on commit c31a6af

Please sign in to comment.