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"; }