Skip to content

langium-zod / ZodSchemaGeneratorModule

Variable: ZodSchemaGeneratorModule

ts
const ZodSchemaGeneratorModule: Module<ZodSchemaGeneratorServices, Partial<ZodSchemaGeneratorServices>>;

Defined in: di.ts:188

Langium Module definition that registers DefaultZodSchemaGenerator under shared.ZodSchemaGenerator in the Langium DI container.

Pass this module to inject() alongside your language's own module to make the Zod schema generator available as a shared service.

Remarks

The module uses a factory function (services) => new DefaultZodSchemaGenerator(services) so that the generator receives the same LangiumCoreServices instance as the rest of the container. This means you can use the injected grammar services (e.g. the document builder, workspace manager) from the same container without creating a separate services instance.

Example

ts
import { inject } from 'langium';
import { createLangiumGrammarServices } from 'langium/grammar';
import { NodeFileSystem } from 'langium/node';
import { ZodSchemaGeneratorModule } from 'langium-zod';

const services = inject(
  createLangiumGrammarServices(NodeFileSystem),
  ZodSchemaGeneratorModule
);
const source = services.shared.ZodSchemaGenerator.generate(grammar);

Use When

  • You are building a Langium language service or LSP plugin and want Zod generation available via the standard shared-service pattern.
  • You need the generator to share the same LangiumCoreServices instance (e.g. document builder, URI resolution) as the rest of the language services.

Avoid When

  • You do not use Langium's DI container at all — call generateZodSchemas directly instead.
  • You need the full ZodGeneratorConfig surface — the generate() method exposed by this module only forwards a subset of config fields.

Never

  • NEVER spread ZodSchemaGeneratorModule into a plain object literal and pass it to inject() without the correct TypeScript generic — BECAUSE the as unknown as Module<...> cast inside the constant means TypeScript will not catch shape mismatches if you destructure it.

See

Released under the MIT License.