Aller au contenu principal

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

Interface

Experimental

Options for creating a new generator context.


Since

2.0.0


Properties

debug?: boolean

Experimental

Enable debug mode for readable code

ExternalValue

Type

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()

Function

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()

Function

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()

Function

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()

Function

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()

Function

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()

Function

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()

Function

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()

Function

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()

Function

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()

Function

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()

Function

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"