Skip to content

langium-zod / ZodGeneratorConfig

Interface: ZodGeneratorConfig

Defined in: config.ts:121

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:125
conformance?{ astTypesPath?: string; outputPath?: string; }--config.ts:129
conformance.astTypesPath?string--config.ts:130
conformance.outputPath?string--config.ts:131
crossRefValidation?boolean--config.ts:128
exclude?string[]-FilterConfig.excludeconfig.ts:53
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:157
grammar?Grammar | Grammar[]--config.ts:122
include?string[]-FilterConfig.includeconfig.ts:52
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:167
outputPath?string--config.ts:124
projection?ProjectionConfig--config.ts:126
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^[+-]?(.[0-9]+[0-9]+.?[0-9]*)([eE][+-]?[0-9]+)?$ }-
services?LangiumCoreServices--config.ts:123
stripInternals?boolean--config.ts:127

Released under the MIT License.