Aller au contenu principal

generateCoerceStringCheck()

generateCoerceStringCheck(varName, ctx, customMessage?): CodeGenResult

Experimental

Generates inline code for coerce.string().

If the value is already a string, returns true. Otherwise, converts to string using String(value) and returns { coerced: result }.


Parameters

varName: string

The variable name to coerce

ctx: GeneratorContext

The generator context

customMessage?: string

Optional custom error message


Returns: CodeGenResult

Generated code and updated context


Since

2.0.0


Example

const result = generateCoerceStringCheck("value", ctx);
// Generated code:
// if (typeof value === "string") return true;
// try { return { coerced: String(value) }; } catch { return "Cannot coerce to string"; }