Access WithMessage input from Validator
See original GitHub issueConsider the validator below:
public class SingleRuleWithMessageValidator : AbstractValidator<Person>
{
public SingleRuleWithMessageValidator()
{
RuleFor(p => p.FirstName).NotEmpty().WithMessage("Specify a first name");
}
}
Having extracted the NotEmptyValidator I was hoping to see the value of WithMessage within the exposed ErrorMessageSource, however the debugger shows this to be be null
Is there a way to retrieve the value of WithMessage?
Issue Analytics
- State:
- Created 6 years ago
- Comments:5 (1 by maintainers)
Top Results From Across the Web
Access WithMessage input from Validator · Issue #470
Based on my experimentation it seems ErrorMessageSource is null unless WithMessage is supplied as part of the validator setup. Would that be a ......
Read more >MVC FluentValidation WithMessage using a property
When attempting to use a property in the WithMessage part of a fluent validation rule, the string property isn't used, and instead it...
Read more >Overriding the Message — FluentValidation documentation
You can override the default error message for a validator by calling the WithMessage method on a validator definition: RuleFor(customer => customer.
Read more >Custom Validators
withMessage takes message as the first argument, and a validator as the second argument, returning a version of that validator with the customised...
Read more >How to access the FluentValidation ErrorCodes?
I'm trying to access the FluentValidation ErrorCodes with the following code, which is hosted in my Controller.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
looking at this file https://github.com/JeremySkinner/FluentValidation/blob/master/src/FluentValidation/Resources/StaticStringSource.cs
it looks like you could call ErrorMessageSource.GetString(null); and it would give you the message? this wont work for all of the overrides of IStringSource but it does for the StaticStringSource
The ErrorMessageSource is simply a way of getting a raw string from one of several locations (could be a constant value, could be from a localization resource, or from a database etc). Calling GetString always returns the original, unaltered string (this is true for StaticStringSource - LazyStringSource could of course be doing any kind of manipulation of the string first) .
Filling in the placeholders requires contextual information about the object being validated, so can only happen during a call to Validate (specifically, the PropertyValidator class’s Validate method calls into the MessageFormatter to construct the final message). When calling GetString outside of this process, the information to fill in the placeholders simply isn’t available.