question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Access WithMessage input from Validator

See original GitHub issue

Consider 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

errormessagesource

Is there a way to retrieve the value of WithMessage?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
erendrakecommented, May 21, 2017

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

0reactions
JeremySkinnercommented, May 22, 2017

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found