elsa-workflows/elsa-core · error · ArgumentException

Invalid value type.

Error message

Invalid value type.

What it means

ConvertAnyDateType handles DateTime targets: it accepts DateTime, DateTimeOffset, and DateOnly source values and throws ArgumentException "Invalid value type." for anything else. This prevents silently converting incompatible values (e.g. strings that were not pre-parsed) into DateTime.

Solutions

  1. Pre-parse strings to DateTime before conversion: DateTime.Parse(value, CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind)
  2. Convert numeric timestamps explicitly with DateTimeOffset.FromUnixTimeSeconds/Milliseconds
  3. Pass DateOnly/DateTimeOffset/DateTime typed values instead of strings where possible
  4. Catch ArgumentException and return a validation error to the user of the workflow

Example fix

// before
var date = (DateTime)ObjectConverter.ConvertTo("2024-01-01", typeof(DateTime));
// after
var date = DateTime.Parse("2024-01-01", CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind);
var result = ObjectConverter.ConvertTo(date, typeof(DateTime));
Defensive patterns

Strategy: validation

Validate before calling

bool isDateConvertible(object? v) => v is DateTime or DateTimeOffset or DateOnly;

Type guard

bool IsDateTimeSource(object? v) => v is DateTime or DateTimeOffset or DateOnly;

Try / catch

try { d = (DateTime)ObjectConverter.ConvertTo(v, typeof(DateTime)); }
catch (ArgumentException ex) when (ex.Message == "Invalid value type.") { d = DateTime.MinValue; }

Prevention

When it happens

Trigger: Calling ConvertTo with a target of typeof(DateTime) while the value is not DateTime, DateTimeOffset, or DateOnly — e.g. a raw string, long timestamp, or TimeOnly value.

Common situations: Workflow inputs as ISO-8601 strings fed into DateTime-typed variables without prior parsing, or passing unix timestamps expecting automatic conversion.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


AI-assisted analysis of elsa-workflows/elsa-core@fe9217bdfa (2026-09-13). Data as JSON: /api/errors/2956c33d08c417ee. Report an issue: GitHub.

Appendix: source

Thrown at src/modules/Elsa.Expressions/Helpers/ObjectConverter.cs:358

    }

    /// <summary>
    /// Converts any date type to the specified target type.
    /// </summary>
    /// <param name="value">Any of <see cref="DateTime"/>, <see cref="DateTimeOffset"/> or <see cref="DateOnly"/>.</param>
    /// <param name="targetType">Any of <see cref="DateTime"/>, <see cref="DateTimeOffset"/> or <see cref="DateOnly"/>.</param>
    /// <returns>The converted value.</returns>
    /// <exception cref="ArgumentException">Thrown if <paramref name="value"/> is not of type <see cref="DateTime"/>, <see cref="DateTimeOffset"/> or <see cref="DateOnly"/>.</exception>
    private static object ConvertAnyDateType(object value, Type targetType)
    {
        return targetType switch
        {
            { } t when t == typeof(DateTime) => value switch
            {
                DateTime dateTime => dateTime,
                DateTimeOffset dateTimeOffset => dateTimeOffset.DateTime,
                DateOnly date => new(date.Year, date.Month, date.Day),
                _ => throw new ArgumentException("Invalid value type.")
            },
            { } t when t == typeof(DateTimeOffset) => value switch
            {
                DateTime dateTime => new(dateTime),
                DateTimeOffset dateTimeOffset => dateTimeOffset,
                DateOnly date => new(date.Year, date.Month, date.Day, 0, 0, 0, TimeSpan.Zero),
                _ => throw new ArgumentException("Invalid value type.")
            },
            { } t when t == typeof(DateOnly) => value switch
            {
                DateTime dateTime => new(dateTime.Year, dateTime.Month, dateTime.Day),
                DateTimeOffset dateTimeOffset => new(dateTimeOffset.Year, dateTimeOffset.Month, dateTimeOffset.Day),
                DateOnly date => date,
                _ => throw new ArgumentException("Invalid value type.")
            },
            _ => throw new ArgumentException("Invalid target type.")
        };
    }

View on GitHub (pinned to fe9217bdfa)