Skip to content

langium-zod / ZodSchemaGenerator

Interface: ZodSchemaGenerator

Defined in: di.ts:45

Service interface for generating Zod schemas from a parsed Langium grammar.

Implemented by DefaultZodSchemaGenerator and registered in the Langium dependency-injection container via ZodSchemaGeneratorModule. Consumers that obtain Langium services through the standard DI mechanism can retrieve this service from services.shared.ZodSchemaGenerator.

Remarks

Using this interface rather than calling generateZodSchemas directly makes it easy to swap or mock the generator in tests without changing the call sites, since the DI container injects the service automatically.

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 integrating langium-zod into an existing Langium DI container and want the generator to be treated as a shared service alongside other Langium services.
  • You need to mock or replace the generator in tests via DI overrides.

Avoid When

  • You are doing a one-off generation outside of the Langium DI lifecycle — call generateZodSchemas directly instead.

See

Methods

generate()

ts
generate(grammar, config?): string;

Defined in: di.ts:46

Parameters

ParameterType
grammarGrammar
config?Partial<ZodGeneratorConfig>

Returns

string

Released under the MIT License.