Reject dates not in ISO8601 format

Is there a way I can get the JSON deserializer to reject DateTime’s that aren’t in ISO8601 format?

I’ve set the DateHandler but this just seems to control the serialization. JsConfig.DateHandler = DateHandler.ISO8601;

I’ve also set JsConfig.ThrowOnDeserializationError = true;

The reason I don’t want to allow other formats is that I have JsConfig.SkipDateTimeConversion = true;. This option only works for ISO dates.

There’s no built-in option to restrict the allowed DateTime formats, you’d need to override the DateTime deserialization handling, e.g:

JsConfig<DateTime>.DeSerializeFn = time =>
{
    var dateTime = ...;
    return dateTime;
};
1 Like

Is there a way to intercept the deserializer when the value is passed by Query param? As the JsConfig.DeSerializeFn is not used in this case.

The issue is that the Services and Request QueryString/Form delegate caches are pre-populated on Startup before AppHost.Configure() is called (when Services are initialized) so you’d need to initialize it in your AppHost constructor, e.g:

public AppHost()
    : base(serviceName, typeof(MyServices).GetAssembly())
{
    JsConfig<DateTime>.DeSerializeFn = str => {
        return ...;
    };
}

Also if you’ve got DateTime? properties you’d need to initilaize that as well:

JsConfig<DateTime?>.DeSerializeFn = str => {
    return ...;
};
1 Like

Thanks this works perfectly now. Would you recommend that all the JsConfig init is performed in the constructor?

1 Like

The Services early-binding is the only reason why you’d want to define it before Configure(), so if you need it, you’d need to define it either in the AppHost constructor or outside the AppHost before appHost.Init().

Sorry, what do you mean by Services early-binding?

ServiceStack Services are the only thing that’s initialized and registered in the IOC before AppHost.Configure() is called which is how you’re able to resolve them from the IOC and execute them on Startup.