bartoszlenar / Validot

Validot is a performance-first, compact library for advanced model validation. Using a simple declarative fluent interface, it efficiently handles classes, structs, nested members, collections, nullables, plus any relation or combination of them. It also supports translations, custom logic extensions with tests, and DI containers.
MIT License
310 stars 19 forks source link

Validated value in the error message #6

Closed bartoszlenar closed 2 years ago

bartoszlenar commented 4 years ago

Feature description

Feature in action

m => m.EqualTo(666).WithMessage("The value {_model} is not equal to 666");
validator.Validate(555).ToString();
// The value 555 is not equal to 666

Feature details

Questions

That's technically, possible, but why implement this? Is this really needed?

The fact that it's possible doesn't mean it's easy. And would certainly affect the perfromance.

JeremySkinner commented 4 years ago

If you do decide to go ahead with this, here's some things I've learned from doing this in FV:

bartoszlenar commented 4 years ago

@JeremySkinner, I'm still wondering whether I should prioritize this one high. It looks like a great feature, but how often in real life users insert full content of the validated value into the error message?

I see several potential risks here:

One thing that makes it a tough decision for me is that Validot's performance comes from the aggressive caching strategy. All error messages (in all languages, all the paths, etc.) are prepared upfront and delivered to the end report based on the validation logic. Inserting validated values into the error messages means creating them during each validation process. Thus, it instantly kills the main selling point that Validot has - being fast and memory efficient.

Also, relying so massively on caching makes the WithMessage(tokens => $"The value {tokens.Value} is not equal to 666") virtually impossible, as WithMessage evaluation and message constructing takes place during Validator initialization, before any real validation. It needs to be another layer or some trick that I need to came up with.

I'm torn apart really when it comes to this idea. I think I'll postpone it till I have a more extensive user base, and people start upvoting this thread.

bartoszlenar commented 2 years ago

FYI @JeremySkinner ultimately, I'm closing this down. The reasons as basically here in this thread for 2 years now. So far I haven't found justification for messing up so badly with the performance.

The only way I see it possible is at the very end, maybe in a form of a helper method to IValidationResult that would somehow correlate the value, the path and the message. Something more like this

User.Name ("elizabeth"): Must start with a capital letter

Where elizabeth is extracted from model and injected into the final printing.

But that's another story, nor another ticket. This one I'm closing down.