GeneratorContext
Experimental
Context used during code generation.
This interface defines the state maintained while generating validation code for a schema.
Sinceโ
2.0.0
Propertiesโ
varCounter: numberโ
Experimental
Counter for generating unique variable names
indent: numberโ
Experimental
Current indentation level (for debug mode)
path: readonly string[]โ
Experimental
Current path in the object (for error messages)
externals: Map<string, ExternalValue>โ
Experimental
Map of external values (refinements or regex patterns) to be injected
debug: booleanโ
Experimental
Whether debug mode is enabled (readable code with comments)
visited: WeakSet<GenericSchema>โ
Experimental
Set of schemas already visited (for cycle detection)
GeneratorContextOptionsโ
Experimental
Options for creating a new generator context.
Sinceโ
2.0.0
Propertiesโ
debug?: booleanโ
Experimental
Enable debug mode for readable code
ExternalValueโ
ExternalValue = (
value) =>boolean|string|RegExp
Experimental
Type for external values that can be stored in the context. Can be either a refinement function or a RegExp pattern.
Sinceโ
2.0.0
createGeneratorContext()โ
createGeneratorContext(
options?):GeneratorContext
Experimental
Creates a new generator context with default values.
Parametersโ
options?: GeneratorContextOptionsโ
Optional configuration
Returns: GeneratorContextโ
A new GeneratorContext instance
Sinceโ
2.0.0
Exampleโ
const ctx = createGeneratorContext({ debug: true });
const varName = nextVar(ctx); // "v_0"
nextVar()โ
nextVar(
ctx): [string,GeneratorContext]
Experimental
Generates the next unique variable name and returns updated context.
Parametersโ
ctx: GeneratorContextโ
The current context
Returns: [string, GeneratorContext]โ
A tuple of [variableName, updatedContext]
Sinceโ
2.0.0
Exampleโ
const [varName, newCtx] = nextVar(ctx);
// varName = "v_0", newCtx.varCounter = 1
pushPath()โ
pushPath(
ctx,segment):GeneratorContext
Experimental
Pushes a path segment and returns updated context.
Used to track the current location in nested objects for error messages.
Parametersโ
ctx: GeneratorContextโ
The current context
segment: stringโ
The path segment to add (property name or index)
Returns: GeneratorContextโ
Updated context with the new path
Sinceโ
2.0.0
Exampleโ
const ctx1 = pushPath(ctx, "user");
const ctx2 = pushPath(ctx1, "address");
// ctx2.path = ["user", "address"]
popPath()โ
popPath(
ctx):GeneratorContext
Experimental
Pops the last path segment and returns updated context.
Parametersโ
ctx: GeneratorContextโ
The current context
Returns: GeneratorContextโ
Updated context with the last path segment removed
Sinceโ
2.0.0
Exampleโ
// ctx.path = ["user", "address"]
const newCtx = popPath(ctx);
// newCtx.path = ["user"]
addExternal()โ
addExternal(
ctx,value): [string,GeneratorContext]
Experimental
Adds an external value (refinement function or regex pattern) and returns its reference name.
External values are stored in the context and injected at runtime. This is necessary because refinement functions and regex patterns cannot be inlined.
Parametersโ
ctx: GeneratorContextโ
The current context
value: ExternalValueโ
The external value to add (function or RegExp)
Returns: [string, GeneratorContext]โ
A tuple of [referenceName, updatedContext]
Sinceโ
2.0.0
Exampleโ
// Adding a refinement function
const isValidSlug = (value: string) => /^[a-z0-9-]+$/.test(value);
const [refName, newCtx] = addExternal(ctx, isValidSlug);
// refName = "ref_0"
// Generated code: `if (!externals.get("ref_0")(value)) return "...";`
// Adding a regex pattern
const [refName2, newCtx2] = addExternal(ctx, /^[a-z]+$/);
// Generated code: `if (!externals.get("ref_0").test(value)) return "...";`
markVisited()โ
markVisited(
ctx,schema):GeneratorContext
Experimental
Marks a schema as visited for cycle detection.
Note: Since WeakSet is not iterable, we cannot create a copy. Instead, we mutate the existing WeakSet. This is acceptable because the visited set is only used during a single compilation pass.
Parametersโ
ctx: GeneratorContextโ
The current context
schema: GenericSchemaโ
The schema being visited
Returns: GeneratorContextโ
Updated context with the schema marked as visited
Sinceโ
2.0.0
hasVisited()โ
hasVisited(
ctx,schema):boolean
Experimental
Checks if a schema has already been visited (cycle detection).
Parametersโ
ctx: GeneratorContextโ
The current context
schema: GenericSchemaโ
The schema to check
Returns: booleanโ
true if the schema was already visited
Sinceโ
2.0.0
increaseIndent()โ
increaseIndent(
ctx):GeneratorContext
Experimental
Increases the indentation level (for debug mode).
Parametersโ
ctx: GeneratorContextโ
The current context
Returns: GeneratorContextโ
Updated context with increased indentation
Sinceโ
2.0.0
decreaseIndent()โ
decreaseIndent(
ctx):GeneratorContext
Experimental
Decreases the indentation level (for debug mode).
Parametersโ
ctx: GeneratorContextโ
The current context
Returns: GeneratorContextโ
Updated context with decreased indentation
Sinceโ
2.0.0
getIndent()โ
getIndent(
ctx):string
Experimental
Gets the current indentation string (for debug mode).
Parametersโ
ctx: GeneratorContextโ
The current context
Returns: stringโ
Indentation string (2 spaces per level)
Sinceโ
2.0.0
formatPath()โ
formatPath(
ctx):string
Experimental
Formats the current path as a string for error messages.
Parametersโ
ctx: GeneratorContextโ
The current context
Returns: stringโ
Formatted path string
Sinceโ
2.0.0
Exampleโ
// ctx.path = ["user", "address", "city"]
formatPath(ctx); // "user.address.city"
// ctx.path = ["items", "0", "name"]
formatPath(ctx); // "items[0].name"