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.

Best way to get a property index in ErrorMessage

See original GitHub issue

I’m somewhat struggling with getting the path and in case of collections index of a validated property in the ErrorMessage. I’m using this wonderful library to validate requests coming into Azure functions and I very much do want to return the whole JSON path and index instead of just a property name.

I’ve managed to do so with path using some extension methods to extract the JsonProperty attribute (or fall back to a reflected property name) and passing that to the constructors of my validators and to WithName/WithMessage like this .WithName(x => CreatePath(x.GetPropertyJsonName(nameof(x.Id)))).

I do have a problem with extracting indexes from collections though. Having a parent validator PermissionValidator with a rule:

RuleForEach(x => x.Interests)
               .SetValidator(x => new InterestValidator(CreatePath(x.GetPropertyJsonName(nameof(x.Interests)))));

Where Interests is of type List<Interest> defined like

[Serializable]
[DataContract]
public class Interest
{
    [DataMember, JsonProperty("id")]
    public string Id { get; set; }

    [DataMember, JsonProperty("name")]
    public string Name { get; set; }
}

Is it possible to include the index of the currently validated Interest object in the message coming from InterestValidator? So instead of getting a message saying permission.interest.name cannot be empty it would be permission.interest[0].name cannot be empty.

Sorry if it’s a dumb question, this is my first time using FluentValidation and I can’t really find any examples of this behavior.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:11 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
JeremySkinnercommented, Jan 24, 2019

So I was thinking something like this:

public static IRuleBuilderOptions<T,TProperty> CustomPlaceholder<T,TProperty>(this IRuleBuilderOptions<T, TProperty> ruleBuilder, string name, Func<T, object> selector) {
	return ruleBuilder.Configure(cfg => {

		var originalMessageBuilder = cfg.MessageBuilder;
		
		cfg.MessageBuilder = context => {
			context.MessageFormatter.AppendArgument(name, selector((T) context.Instance));
			return originalMessageBuilder != null ? originalMessageBuilder(context) : context.GetDefaultMessage();
		};

	});
}
RuleFor(x => x.Name).NotNull().WithMessage("{SomePlaceholder}").CustomPlaceholder("SomePlaceholder", x => x.SomeProp);
1reaction
JeremySkinnercommented, Jan 24, 2019

And just to clarify, the instance being validated is accessible from the MessageBuilder’s context (context.Instance)

Read more comments on GitHub >

github_iconTop Results From Across the Web

TypeError: Cannot read property 'index' of undefined
My url seems to be fine, http://localhost:63342/Angular-CRUD/#/edit/2 It seems that id is passing correctly from view, but why am i getting ...
Read more >
Error message, index property bigger than number of items ...
Its great that you are using Log to display the Items value. Oh! You stated "From my point of view the index property...
Read more >
PropertyInfo.GetValue Method (System.Reflection)
Returns the property value of a specified object. ... The following example shows how to get the value of an indexed property. The...
Read more >
Index() or dot notation?
I personally prefer to use property() to retrieve dot properties, and reserve index() for retrieving indexes (i.e. from an array) -- even though...
Read more >
Documentation - Indexed Access Types
Indexed Access Types. We can use an indexed access type to look up a specific property on another type:.
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