Skip to main content

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