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"