I'm returning BadRequest(ModelState) from an ASP.NET controller action, like this:
public class Person{
[MaxLength(40)]
public string Name{get;set;}
public double Height{get;set;}
}
public Put([FromBody]Person item){
if(!ModelState.IsValid) return BadRequest(ModelState);
//save to db
return Ok(item);
}
This works, however casing seems inconsistent when the BadRequest is serialized to JSON. First, the object serializes as camelCase, as expected:
GET /api
{ name:'foo',
height: 11
}
If the error is a failure in model binding, then the casing is camel cased:
PUT /api {name:'bob',height:'foo'}
{
height:['Could not convert 'foo' to double']
}
If the error is a failure in validation, the casing is PascalCased:
PUT /api {name:'morethan40characters', height:12}
{
Name:['Name should be between 0 and 40 characters']
}
I think I understand why this is: the model binding is starting from the posted JSON which is camelCased, while the validation is working against the .NET class directly, which is PascalCased.
However, API consumers won't care about that distinction and will expect consistent casing. Especially in the client UI with something like Vue or Angular, it's cleaner to keep everything camelCased, or at least consistent:
http.put('/api',item).then(x=>{...},e=>{ this.error=e.response.data });
...
<ul>
<li v-for="message in error.height">{{message}}</li>
<li v-for="message in error.Height">{{message}}</li>
<!-- line above smells -->
</ul>
<input type="text" v-model="item.height">
Obviously I could massage the error response client-side, but is there a flag or some middleware I can do server-side to just make this consistent. Preferably, consistently camelCase?