Skip to main content

generateCoerceDateCheck()

generateCoerceDateCheck(varName, ctx, customMessage?): CodeGenResult

Experimental

Generates inline code for coerce.date().

If the value is already a valid Date, returns true. Otherwise, attempts to convert to Date with special handling for:

  • null: returns error (cannot convert null to Date)
  • undefined: returns error (cannot convert undefined to Date)
  • number: new Date(value)
  • string: new Date(value)
  • boolean: new Date(value ? 1 : 0)
  • other: new Date(String(value))

Returns { coerced: result } for successful coercions, error string otherwise.


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 = generateCoerceDateCheck("value", ctx);
// Generated code handles various input types and validates the resulting Date