I'm using automatic model validation (see "Better Input Processing") to keep my controllers clean; so:
[HttpPost]
[ProducesResponseType(typeof(Product), 201)]
public IActionResult Post([FromBody] Product product)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
product = _repository.AddProduct(product);
return CreatedAtAction(nameof(Get), new { id = product.Id }, product);
}
becomes:
[HttpPost]
[ProducesResponseType(201)]
public ActionResult<Product> Post(Product product)
{
_repository.AddProduct(product);
return CreatedAtAction(nameof(Get), new { id = product.Id }, product);
}
However, I do have a few models that have a phonenumber property. I would like to 'normalize' these before the model validation is invoked. What I mean is that I want to normalize these properties (of type string
) from all kinds of input like:
- +31 23 456 7890
- (023) 4567890
- 023 - 4567 890
- ...
To E.164 notation:
- +31234567890
So in whatever form a user enters a phonenumber, before validation is invoked I want to be sure it's always in E.164 form ('normalized'). How this normalization is done is irrelevant (I use libphonenumber if you insist). As a second, maybe less convoluted, example I can imagine a string to be always upper-/lowercased before validation is invoked.
What would be the correct, or best, way to invoke my normalization process before the validation is invoked? Would I have to write some middleware?
Also relevant: my models contain attributes so the normalizer knows which properties to normalize (and how):
class ExampleModel {
public int Id { get; set; }
public string Name { get; set; }
[NormalizedNumber(NumberFormat.E164)]
public string Phonenumber { get; set; }
}
I guess the middleware(? or whatever the solution is going to be) can then take a model, figure out if any of the properties (recursively) have the attribute and invoke the normalizer if needed.