Skip to content

langium-zod / ZodGeneratorConfig

Interface: ZodGeneratorConfig ​

Defined in: config.ts:122

Full configuration object for generateZodSchemas.

At least one of grammar or astTypes must be provided:

  • grammar — a parsed Langium Grammar AST (or array for multi-grammar projects); the extractor calls Langium's collectAst() internally.
  • astTypes — a pre-built AstTypesLike descriptor, useful when the caller already has the type model and wants to skip grammar parsing.

All other fields are optional and control output path, include/exclude filtering, projection, strip-internals, cross-reference validation, conformance artifact generation, regex overrides, form metadata emission, and object style.

Remarks ​

ZodGeneratorConfig extends FilterConfig, so include and exclude are top-level fields. When both grammar and astTypes are supplied, astTypes is used and grammar is ignored — this allows callers that have already run collectAst() to skip a redundant parse.

The objectStyle field defaults to 'loose' (emits z.looseObject), which allows extra properties in validated objects. Set it to 'strict' to emit z.object(...) if you want Zod's standard stripping behaviour.

Example ​

ts
import { generateZodSchemas } from 'langium-zod';

generateZodSchemas({
  grammar: parsedGrammar,
  outputPath: 'src/generated/zod-schemas.ts',
  include: ['Greeting', 'Person'],
  stripInternals: true,
  objectStyle: 'strict',
  regexOverrides: {
    BigDecimal: String.raw`^[+-]?(\.[0-9]+|[0-9]+\.?[0-9]*)([eE][+-]?[0-9]+)?$`
  }
});

Use When ​

  • You need to configure output path, filtering, or projection in a single object.
  • You are building a config-file-driven pipeline and want type-safe config loading.
  • You need conformance artifacts and must specify conformance.astTypesPath.

Avoid When ​

  • You only need a quick one-liner with defaults — pass only { grammar } inline.
  • You are using the CLI — the langium-zod.config.js file maps to LangiumZodConfig, not directly to ZodGeneratorConfig.

Never ​

  • NEVER set conformance without outputPath. BECAUSE the conformance module derives the conformance output path from the schema file's directory; without outputPath the function throws before writing any output.
  • NEVER mix grammar (multi-grammar array) with astTypes — if astTypes is set it takes priority and the grammar field is silently ignored. BECAUSE the code path short-circuits to the pre-built descriptor immediately.
  • NEVER assume objectStyle: 'strict' rejects unknown properties. BECAUSE Zod's z.object() strips (not rejects) unknown properties by default; call .strict() on the generated schema if hard rejection is required.

Config ​

See ​

Extends ​

Properties ​

PropertyTypeDescriptionInherited fromDefined in
astTypes?AstTypesLike--config.ts:126
conformance?{ astTypesPath?: string; outputPath?: string; }--config.ts:130
conformance.astTypesPath?string--config.ts:131
conformance.outputPath?string--config.ts:132
crossRefValidation?boolean--config.ts:129
domainOnly?booleanWhen true (CLI --domain-only), emit ONLY the domain surface and skip Zod-schema generation.-config.ts:172
domainOutputPath?stringOutput path for the generated domain surface (domain.ts).-config.ts:174
domainOverlays?DomainOverlayConfigProject-specific semantic overlays (renames + read-only merges) supplied by the consumer for the domain target.-config.ts:176
emitDomain?booleanWhen true, the CLI also emits the domain surface to domainOutputPath.-config.ts:170
exclude?string[]-FilterConfig.excludeconfig.ts:54
formMetadata?booleanWhen true, emit .meta({ title, description? }) on generated Zod schemas using humanized property/type names as title and JSDoc comments from the grammar as description. The description field is only included when a JSDoc/grammar comment exists for the corresponding type or property. Useful for zod-to-forms integrations that derive field labels from metadata.-config.ts:158
grammar?Grammar | Grammar[]--config.ts:123
include?string[]-FilterConfig.includeconfig.ts:53
namespaceOps?booleanWhen true (CLI --namespace-ops), also emit the namespace-ops surface alongside other targets.-config.ts:178
namespaceOpsIdentity?Record<string, string>Element-type → identity field path map for namespace-ops removeX emission. e.g. { Attribute: 'name', ChoiceOption: 'typeCall.type.$refText' }.-config.ts:185
namespaceOpsOutputPath?stringOutput path for the generated namespace-ops surface (domain-ops.ts).-config.ts:180
namespaceOpsRepository?{ elementTypes?: string[]; }Top-level element types included in the generated domain repository (AnyDomain union). e.g. { elementTypes: ['Data', 'Choice'] }.-config.ts:190
namespaceOpsRepository.elementTypes?string[]--config.ts:190
objectStyle?"loose" | "strict"Controls how object schemas are emitted. - 'loose' (default): emits z.looseObject(...) which allows extra properties to pass through unchanged. - 'strict': emits z.object(...) (the standard Zod object). This strips unknown properties by default instead of rejecting them with a validation error. Consumers can call .strict() on the emitted schema if they need hard rejection of unknown properties.-config.ts:168
outputPath?string--config.ts:125
projection?ProjectionConfig--config.ts:127
regexOverrides?Record<string, string>Override the generated schema for specific type names. Use this for parser-based datatype rules (e.g. BigDecimal returns string: ... INT ...) whose structure cannot be expressed as a regex automatically by Langium. The value is a raw regex pattern string (without surrounding / slashes). The named type will emit z.string().regex(new RegExp("...")) instead of z.string(). Example regexOverrides: { BigDecimal: String.raw[1]?(.[0-9]+[0-9]+.?[0-9]*)([eE][+-]?[0-9]+)?$ }-
services?LangiumCoreServices--config.ts:124
stripInternals?boolean--config.ts:128

  1. +- ↩︎

Released under the MIT License.