jwaliszko / ExpressiveAnnotations

Annotation-based conditional validation library.
MIT License
351 stars 123 forks source link

Client side validation fails when JsonConvert.DefaultSettings are changed #27

Closed ghost closed 10 years ago

ghost commented 10 years ago

I'm using JsonConvert.DefaultSettings in my application to set application-wide default JSON serialization settings (e.g. setting ContractResolver to CamelCasePropertyNamesContractResolver). This causes the client-side validation to fail (specifically in this case due to camel-cased field names ending up in the data- attributes) because ExpressiveAnnotations isn't opting out of using default settings.

Since ExpressiveAnnotations relies on specific JSON formatting for client-side validation, it should follow the recommended approach for libraries by opting out of user-specified default settings. This can be accomplished by creating a JsonSerializer instance instead of using JsonConvert.SerializeObject() as per the Json.NET blog post on the subject:

Because there are cases where JSON should not be customized, e.g. a Facebook or Twitter library, by default JsonSerializer won’t use DefaultSettings, providing an opt-out for those frameworks or for places in your application that shouldn’t use default settings.

This would make it more resilient to unexpected behavior.

jwaliszko commented 10 years ago

Thanks for pointing this out - fixed(https://github.com/JaroslawWaliszko/ExpressiveAnnotations/commit/c59aa6a3289e7309fd9b5c072c3f55136e3d8a19).