-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Updated ModelValidationResult and ModelValidationError #1318
Updated ModelValidationResult and ModelValidationError #1318
Conversation
It will not use ModelValidationResult.Valid, instead of newing up a blank result, when no result has explicitly been assigned to the property.
Instead of errors being represented by a simple IList<ModelValidationError> structure, it is not stored in an IDictionary<string, IList<ModelValidationError>> structure. The key is the name of the property that the error represents. The error message formatter has also been updated from Func<string, string> to Func<IEnumerable<string>, string> since MemberNames can contain multiple values Last, the ModelValidationError can now implicitly be cast to a string
What if the validation is across multiple properties? Like "if (country == usa && age >22) then Model is valid"? I'm building a fairly large app with hundreds of individual models that have rules like this. |
Looks good. I assume the Func is for performance? I don't really like it would, prefer just a string but I think I get why its there |
@jchannon I assume you mean for the We could add a string overload perhaps? |
@GiscardBiamby could you please elaborate you scenario? I am not sure if you are worried that the changes (in this pull-request) is going to break your existing code or if you would like additional changes to make your use case easier to manage? Interested in hearing about this! :P |
@thecodejunkie just not sure why its a func. An error should have a string key for the model property name and then a IEnumerable for error messages associated to it. Why do you need something called GetMessage to get the error message. Should just be able to do something like
|
@jchannon it was originally created that way because the data annotations provide an error message formatter function, so the approach was mirrored in the Nancy validation abstraction and it worked well for fluent validation. However, the more I think about it, we should change it to a string and have the various wrappers conform to this. So what it means for (example) data annotations is that we invoke the func and take the result and stick in our type. |
Sounds like a plan |
It now takes a string as the error message, instead of a Func<string>. Each validation adapter is now resposible to format the error message instead of providing a formatter. This change makes it easier to use for custom errors.
…rovements Updated ModelValidationResult and ModelValidationError
On ModelValidationError you have this:
Any reason we don't have an override for ToString() that does the same thing? The reason I'm asking is because this:
Is giving me the typename, and not the error message, I still have to do
|
Could you please ellaborate on the intended usage for As I see it now, we have a: What is supposed to go in the key, and what is to go in the MemberName? I see there is a helper method to Add an error taking key+error message, which creates a ModelValidationError using the ctor taking 1 membername, but there is no helper method to add using multiple memberNames I assume the idea was to have the key of the dictionary be the single property in error? But what if an error applies to 2 properties, for example the error "Wrong combination of email and password", involves 2 properties. It doesn't seem logical to add 2 entries in the Dictionary, because iterating them will spew out 2 identical errors. I thought to just add an error to the dictionary with key "InvalidPassword" and have the ModelValidationError then take 2 memberNames. This is how I am doing it now, but it feels very convulted So, I'd love to see some reasoning behind intended usage please :) |
I'm not even sure if this overload is still being used somewhere now?
|
@CumpsD the idea is to group the errors per field that they belonged to. Earlier they were just a random set of rules that had fields assigned to them. Do you could have
This change was made to facilitate the desire we have to soon be able to provide an adapter layer for client-side validation frameworks. The adapters will use the rules provided by Nancy and map them to the corresponding stuff in client-side code (what ever js framework you want to use) We're definitely not against the idea of making further modifications to the structure. Happy to discuss what this would look like and how it would function from a rule perspective. |
@CumpsD oh and we can definitely overload |
@thecodejunkie Yeah, I thought that was the idea, key: firstname, value: rules that are broken for firstname How would you deal with a rule that checks firstname and lastname together, and would only throw one error? Add it twice? Then you can't just iterate over the keys anymore to print out an error list, or you end up with duplicate eg: Username: Invalid combination username and password It feels like it is sending a confusing message, instead of SDHP. ModelValidationError has an array of memberNames, but the key of the validation results dictionary is always about 1 member. |
@CumpsD, I asked that same question in the first comment of this thread. I haven't looked at it since, but I've been thinking that maybe I'd handle it by using a special value as the dictionary key to indicate that the errors are on the model itself rather than on a single property. Either the name would be the name of the type, or "MODEL" , or something like that. |
Yes, for now I am doing something similar: https://github.com/CumpsD/CC.TheBench/blob/master/src/CC.TheBench.Frontend.Web/Utilities/Extensions/NancyExtensions/AddValidationErrorExtension.cs But it's not very clean imho. I guess there are 2 use cases:
The first to be used for client side highlighting of input fields in state of error (where you need all of them), and the second used for ValidationSummary-style overviews |
Don't bother looking at that file anymore, just refactored it :) https://github.com/CumpsD/CC.TheBench/commit/6227e612cdb7bb847f00d2ec20dbe43b3f6ea366 The approach I'm using now is to keep it as is in Nancy and have a helper method to give me unique error messages to use in a ValidationSummary, while the errors will have multiple entries for all UI fields involved in the combined error |
Like I said, I recognize the limitation/problem and am open for suggestion on how we re-design the validation result class to support this. Suggestions? Thanks |
Instead of errors being represented by a simple
IList<ModelValidationError>
structure, it is not stored in anIDictionary<string, IList<ModelValidationError>>
structure. The key is thename of the property that the error represents.
The error message formatter has also been updated from
Func<string, string
tostring
Last, the
ModelValidationError
can now implicitly be cast to a string