fluentvalidation error message contains c# property name and not client side json property name? -


i have c# webapi project , using fluentvalidation.webapi package validation of client inputs.

below model class code has c# property named "ispremium". same property has json name "isluxury" clients.

[serializable, jsonobject, validator(typeof(productvalidator))] public class product {     [jsonproperty("isluxury")]     public bool? ispremium { get; set; } } 

and validator class looks like:

public class productvalidator : abstractvalidator<product>     {         public productvalidator()         {             rulefor(product => product.ispremium).notnull();         }     } 

so request like: http://localhost:52664/api/product

request body:{ "isluxury": "" }

i following error:

{   "message": "the request invalid.",   "modelstate": {     "product.ispremium": [       "'is premium' must not empty."     ]   } } 

fluent here picking c# property name makes no sense client knows "isluxury". how can force fluent pick names json property , not c# property give better validations "'isluxury' must not empty."?

if not possible, have rename c# properties have same name these json exposed clients. please suggest if have other better way solve problem.

modifying validator class worked me.

public class productvalidator : abstractvalidator<product>     {         public productvalidator()         {             rulefor(product => product.ispremium).notnull().overridepropertyname("isluxury");         }     } 

referring: https://github.com/jeremyskinner/fluentvalidation/wiki/d.-configuring-a-validator#overriding-the-default-property-name

you can replace property name calling withname.if want rename property, can use overridepropertyname method instead.


Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -