{"id":27966452,"url":"https://github.com/techery/zod-dynamic-schema","last_synced_at":"2026-02-12T05:32:05.418Z","repository":{"id":272319972,"uuid":"916197196","full_name":"techery/zod-dynamic-schema","owner":"techery","description":null,"archived":false,"fork":false,"pushed_at":"2025-01-13T16:43:15.000Z","size":9,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-08-08T22:59:53.482Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/techery.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2025-01-13T16:30:26.000Z","updated_at":"2025-01-13T16:42:27.000Z","dependencies_parsed_at":null,"dependency_job_id":"babb2b33-3a70-4840-b429-8a8ce99b3a02","html_url":"https://github.com/techery/zod-dynamic-schema","commit_stats":null,"previous_names":["techery/zod-dynamic-schema"],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/techery/zod-dynamic-schema","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/techery%2Fzod-dynamic-schema","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/techery%2Fzod-dynamic-schema/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/techery%2Fzod-dynamic-schema/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/techery%2Fzod-dynamic-schema/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/techery","download_url":"https://codeload.github.com/techery/zod-dynamic-schema/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/techery%2Fzod-dynamic-schema/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":273203231,"owners_count":25063275,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-09-01T02:00:09.058Z","response_time":120,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-05-07T20:18:52.454Z","updated_at":"2026-02-12T05:32:00.400Z","avatar_url":"https://github.com/techery.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# @techery/zod-dynamic-schema\n\n[![npm version](https://badge.fury.io/js/@techery%2Fzod-dynamic-schema.svg)](https://www.npmjs.com/package/@techery/zod-dynamic-schema)\n![CI Status](https://github.com/techery/zod-dynamic-schema/actions/workflows/pr-checks.yml/badge.svg?branch=main)\n\n\nType-safe factory functions for creating dynamic Zod schemas with TypeScript inference.\n\nDeveloped by [Techery](https://techery.io).\n\n## Motivation\n\nThis package was created to solve a specific challenge when working with LLM (Large Language Model) structured outputs. When building LLM applications, we often need to:\n\n- Define structured output schemas that are mostly fixed but need certain parts to vary based on runtime state\n- Maintain type safety while allowing dynamic schema modifications\n- Create reusable schema templates that can be adjusted based on external data or application state\n\nThis helper provides a clean, type-safe way to create schema factories that can generate different variations of a base schema while maintaining consistent structure and type inference.\n\n## Features\n\n- Create reusable Zod schema factories with full TypeScript type inference\n- Support for both parameterized and static schema generation\n- Zero runtime overhead\n- Lightweight with minimal dependencies\n- Perfect for LLM structured output validation\n\n## Installation\n\n```bash\nnpm install @techery/zod-dynamic-schema zod\n```\n\n## Usage\n\n### Real-World Example: Dynamic Form Widget\n\nThis example shows how to create a schema for a form widget where the options and validation rules can be dynamically configured based on the context:\n\n```typescript\nimport { z } from 'zod';\nimport { schemaFactory } from '@techery/zod-dynamic-schema';\n\ninterface DynamicOptions {\n  minItems?: number;\n  maxItems?: number;\n  instructions?: string;\n}\n\nconst dynamicOptionsWidgetSchemaFactory = schemaFactory((dynamicOptions: DynamicOptions) =\u003e {\n  return z.object({\n    type: z.literal(\"options\").describe(\"Options widget type\"),\n    options: z\n      .array(\n        z.object({\n          name: z.string().describe(\"Name of the option\"),\n        })\n      )\n      .min(dynamicOptions.minItems ?? 2)\n      .max(dynamicOptions.maxItems ?? 10)\n      .describe(dynamicOptions.instructions),\n    multiSelect: z.boolean().describe(\"Whether the user can select multiple options\"),\n  });\n});\n\n// Create different variants based on context\nconst singleChoiceQuestion = dynamicOptionsWidgetSchemaFactory({\n  minItems: 2,\n  maxItems: 4,\n  instructions: \"Please provide 2-4 options for single choice question\"\n});\n\nconst multipleChoiceQuestion = dynamicOptionsWidgetSchemaFactory({\n  minItems: 3,\n  maxItems: 6,\n  instructions: \"Please provide 3-6 options for multiple choice question\"\n});\n\n// Get the schema type directly from the factory\ntype DynamicOptions = typeof dynamicOptionsWidgetSchemaFactory.$type;\n// Or from the instance\ntype SingleChoiceWidget = z.infer\u003ctypeof singleChoiceQuestion\u003e;\n// Both will give you:\n// {\n//   type: \"options\";\n//   options: { name: string }[];  // with min(2) and max(4) constraints\n//   multiSelect: boolean;\n// }\n```\n\n### LLM Output Schema Example\n\n```typescript\nimport { z } from 'zod';\nimport { schemaFactory } from '@techery/zod-dynamic-schema';\n\n// Define a factory for LLM response schema that varies based on allowed actions\ninterface ActionSchemaParams {\n  allowedActions: string[];\n  requireReasoning: boolean;\n}\n\nconst llmResponseSchema = schemaFactory((params: ActionSchemaParams) =\u003e {\n  const actionEnum = z.enum(params.allowedActions as [string, ...string[]]);\n  \n  return z.object({\n    action: actionEnum,\n    parameters: z.record(z.string(), z.any()),\n    confidence: z.number().min(0).max(1),\n    reasoning: params.requireReasoning \n      ? z.string().min(1)\n      : z.string().optional(),\n  });\n});\n\n// Create different variants based on context\nconst chatbotResponse = llmResponseSchema({\n  allowedActions: ['reply', 'ask_clarification', 'end_conversation'],\n  requireReasoning: true,\n});\n\nconst dataAnalysisResponse = llmResponseSchema({\n  allowedActions: ['analyze_data', 'request_more_data', 'provide_insights'],\n  requireReasoning: false,\n});\n```\n\n### Field Extraction Example\n\n```typescript\nimport { z } from 'zod';\nimport { schemaFactory } from '@techery/zod-dynamic-schema';\n\n// Create a schema factory for structured field extraction\nconst fieldExtractionSchema = schemaFactory((fields: string[]) =\u003e\n  z.object({\n    extracted_fields: z.object(\n      fields.reduce((acc, field) =\u003e ({\n        ...acc,\n        [field]: z.string(),\n      }), {})\n    ),\n    confidence_scores: z.object(\n      fields.reduce((acc, field) =\u003e ({\n        ...acc,\n        [field]: z.number().min(0).max(1),\n      }), {})\n    ),\n  })\n);\n\n// Use with different field sets\nconst nameExtractor = fieldExtractionSchema(['firstName', 'lastName']);\nconst addressExtractor = fieldExtractionSchema([\n  'street',\n  'city',\n  'state',\n  'zipCode'\n]);\n```\n\n## Type Information\n\nThe package exports the following types:\n\n```typescript\ntype SchemaFactory\u003cI, Z extends z.ZodTypeAny\u003e = {\n  $type: z.infer\u003cZ\u003e;\n  (input: I): Z;\n  (): Z;\n};\n```\n\nThe `$type` property allows you to get the inferred type directly from the factory, without creating an instance. This is useful when you need the type but don't have the runtime parameters yet.\n\n## Authors\n\n- [Serge Zenchenko](https://github.com/sergezenchenko) - CTO at [Techery](https://techery.io)\n\n## License\n\nMIT","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftechery%2Fzod-dynamic-schema","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftechery%2Fzod-dynamic-schema","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftechery%2Fzod-dynamic-schema/lists"}