Zod v4 schema generation from Langium grammars — integrate Langium DSLs with TypeScript validation pipelines.
⚠️ Pre-1.0 software — APIs are subject to change between minor versions. Pin to exact versions in production. See the CHANGELOG for breaking changes between releases.
📚 Documentation: https://pradeepmouli.github.io/langium-zod/
langium-zod inspects the type system Langium infers from a grammar (interfaces, unions, primitive properties, cross-references) and emits a corresponding set of Zod schemas. The result is a single TypeScript module you can import anywhere you need to validate or parse AST-shaped data at runtime — tooling, import pipelines, language servers, or network boundaries that consume your DSL.
pnpm add langium-zodProgrammatic use:
import { generateZodSchemas } from 'langium-zod';
const zodSource = generateZodSchemas({ grammar, services });CLI use (via the generate entry point or the project's own CLI wiring):
import { generate } from 'langium-zod';
await generate({
grammar: 'src/language/my-dsl.langium',
output: 'src/generated/schemas.ts',
});generateZodSchemas— end-to-end API that takes a Langium grammar plus services and returns Zod sourceextractTypeDescriptors— walk a LangiumAstTypesshape into neutralZodTypeDescriptor/ZodPropertyDescriptorrecordsgenerateZodCode— render descriptors to Zod v4 TypeScript sourcedetectRecursiveTypes— identify cycles so the generator can emitz.lazy(...)where requiredzRef— runtime helper for modelling Langium cross-references in generated schemasDefaultZodSchemaGenerator+ZodSchemaGeneratorModule— Langium DI module so the generator can be injected into a language's services- Configurable output path (
DEFAULT_OUTPUT_PATH) and filtering (FilterConfig,ZodGeneratorConfig) - Structured errors via
ZodGeneratorError
The extractor walks Langium's inferred AstTypes to build a parser-neutral descriptor tree, the recursion detector marks cycles, and the code generator prints Zod v4 schemas (using z.lazy where needed and zRef for cross-references). A Langium DI module is provided so the generator can be registered as a service on a custom language.
pnpm install
pnpm run build
pnpm run test
pnpm run lint
pnpm run type-checkRelease workflow uses Changesets:
pnpm changeset
pnpm changeset:version
pnpm changeset:publishAutomated release is configured in .github/workflows/release.yml.
- Full docs site: https://pradeepmouli.github.io/langium-zod/
- CONTRIBUTING.md
- CHANGELOG.md
| Library | Relationship | npm |
|---|---|---|
| x-to-zod | JSON Schema → Zod conversion (used internally for schema bridging) | |
| rune-langium | DSL toolchain that uses langium-zod for schema generation | |
| zod-to-form | Takes the Zod schemas langium-zod generates and produces React forms |
MIT — see LICENSE.