valit-stack/Valit

Add support for variable messages at runtime

Closed this issue · 4 comments

E.g. for translation of the error messages depending on user's culture.

Basic usage would be something along the lines of
.WithMessage(()=> _translationService.GetError("Missing_Email"))

Can you tell me what is the advantage of this solution over message provider? I guess, I simply miss something :D

It is a lot simpler, since you do not need to define a separate class for just getting translated texts - and it is extremely versatile since you can put in as much logic as you'd like. I'll look into expanding it to support using the entity under test as a parameter, so you can go: .WithMessage(person => $"{person.Email} is not a valid email")

Now I get it :D

I think that it could be achieved in different fashion but the utility this solution provides is higher than the cost.

But I would go with convention where you provide a current property value first and then separate overload with value and "entity under test"/"model" as second parameter just as I suggested in #114. So it would go like this:
.WithMessage(v => $"{v} is not a valid email")
.WithMessage((endDate, m) => $"{endDate} predates {m.StartDate} for whatever reason, lol")
So the difference between your proposal and mine is to always have same argument at same position, no matter how many overloads.