Skip to content

langium-zod / DefaultZodSchemaGenerator

Class: DefaultZodSchemaGenerator

Defined in: di.ts:87

Default implementation of ZodSchemaGenerator.

Wraps the top-level generateZodSchemas function and injects the LangiumCoreServices instance provided by the DI container, so callers do not need to pass services manually on every invocation.

Remarks

This class does not add logic beyond forwarding to generateZodSchemas; it exists to make the generator accessible as a Langium shared service. If you need custom generation logic, extend this class or implement ZodSchemaGenerator from scratch and register your implementation in place of ZodSchemaGeneratorModule.

Only include, exclude, outputPath, and regexOverrides are forwarded from the optional config parameter — other ZodGeneratorConfig fields (e.g. projection, conformance, formMetadata) must be handled by callers that use generateZodSchemas directly.

Use When

  • You are using the Langium DI lifecycle and want the default generation behaviour accessible as services.shared.ZodSchemaGenerator.
  • You want to extend or override the generator within the DI system.

Avoid When

Never

  • NEVER call new DefaultZodSchemaGenerator(services) manually in production code if you are already using the DI container. BECAUSE the container may inject a different instance (e.g. a mock), and constructing a second instance bypasses DI overrides set up for tests.

See

Implements

Constructors

Constructor

ts
new DefaultZodSchemaGenerator(services): DefaultZodSchemaGenerator;

Defined in: di.ts:90

Parameters

ParameterType
servicesLangiumCoreServices

Returns

DefaultZodSchemaGenerator

Methods

generate()

ts
generate(grammar, config?): string;

Defined in: di.ts:103

Generates Zod schemas for the given grammar and returns the TypeScript source string.

Parameters

ParameterTypeDescription
grammarGrammarParsed Langium Grammar AST to generate schemas from.
config?Partial<ZodGeneratorConfig>Optional subset of ZodGeneratorConfig; only include, exclude, outputPath, and regexOverrides are forwarded. Use generateZodSchemas directly for the full config surface.

Returns

string

The generated TypeScript source containing all Zod schema exports.

Implementation of

ZodSchemaGenerator.generate

Released under the MIT License.