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