When posting JSON to Web API, not only does it get deserialized automatically, one can also use model validation like
// ItemPostRequest
class ItemPostRequest {
[Required] // this will automatically be validated and errors created if it is missing
public string Description { get; set; }
}
However in my case, I only have a string containing JSON. I can deserialize it using JsonSerializer.Deserialize<ItemPostRequest>(myJsonString);
but that's missing the validation.
How to use the validation / how to manually deserialize and validate JSON like Web API does it internally?
In my case the JSON string is part of form-data with keys like file
and json
, but the form-data formatter only cares about splitting the form-data into key-value pairs, it doesn't care about deserializing the json and the model validation for it. So I have to do this manually - but how?