Incoming\Outcoming DTO validation (json schema)

hi SS team

I know SS does not implement json validation when it deserialize incoming DTO or serialize outcoming DTO.

Given the very high flexibility of the framework, is there a way to override one or more steps in the SS pipeline to easily implement json schema validation?

The idea is to implement a SS plugin or something similar and use Json.NET Schema - Newtonsoft to achieve the schema validation.

Looking to the order of operations the only one I can see I could use is IAppHost.PreRequestFilters but I don’t want to register a custom binder nor to mark all the app DTO with IRequiresRequestStream.

I’m using SS 5.9.2, any suggestions?

thanks

The RequestStream can only be read once, so if you want to re-read the JSON Request Body you’ll need to buffer the Request Stream:

appHost.PreRequestFilters.Add((httpReq, httpRes) => {
    httpReq.UseBufferedStream = true;  // Buffer Request Input
});

From there you’ll be able to re-read the Request Input Stream from anywhere with access to IRequest with:

string textBody = await httpReq.GetRawBodyAsync(); //read as string

You wont be able to intercept the JSON response since it serializes directly to the Response Stream, you could serialize the DTO to JSON in your Service and validate that but I don’t think you should be validating your outputs at runtime, if you suspect for some reason a serialized DTO doesn’t validate to some schema, this should really be caught with tests not at runtime.