CommandLineUtils icon indicating copy to clipboard operation
CommandLineUtils copied to clipboard

Incorrect value of ValidationContext.ObjectInstance

Open ridercz opened this issue 5 years ago • 3 comments

Describe the bug

The ValidationContext.ObjectInstance property is expect to contain the entire entity being validated (the class on which the properties Minimum and Maximum are defined). Instead it gets instance of McMaster.Extensions.CommandLineUtils.CommandArgument.

To Reproduce

Have the following properties and validate them:

[Argument(0, Name = "min", Description = "Minimum value")]
public int Minimum { get; set; }

[Argument(1, Name = "max", Description = "Maximum value")]
[Compare(nameof(Minimum))]
public int Maximum { get; set; }

Expected behavior Correct validation.

Screenshots Error message Could not find a property named Minimum.

Additional context The above example does not make logical sense (I'm requiring that min and max values are the same). I'm actually using my own [GreaterThan] attribute from my Altairis.ValidationToolkit project. But the problem is valid for any property-level attribute which needs value of other property than the one it validates, so I'm demoing it on [Compare] which does not require additional code.

ridercz avatar May 09 '19 16:05 ridercz

Fixing this will be tricky to do without breaking anyone who has come to depend on existing behavior, but I think it should be possible. Basically, you would need to make CommandLineValidationContextFactory aware of when the attribute-based API has been used, as opposed to the builder API.

See https://github.com/natemcmaster/CommandLineUtils/blob/f498cc7383b27730fd24486510573ab61ccab9d6/src/CommandLineUtils/Internal/CommandLineValidationContext.cs#L18-L25

natemcmaster avatar May 16 '19 13:05 natemcmaster

Putting this into the 3.0 milestone as something to consider.

natemcmaster avatar Jul 16 '19 18:07 natemcmaster

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Please comment if you believe this should remain open, otherwise it will be closed in 14 days. Thank you for your contributions to this project.

stale[bot] avatar Jul 21 '21 02:07 stale[bot]

This issue has been automatically marked as stale because it has no recent activity. It will be closed if no further activity occurs. Please comment if you believe this should remain open, otherwise it will be closed in 14 days. Thank you for your contributions to this project.

github-actions[bot] avatar Nov 15 '22 02:11 github-actions[bot]

Closing due to inactivity. If you are looking at this issue in the future and think it should be reopened, please make a commented here and mention natemcmaster so he sees the notification.

github-actions[bot] avatar Nov 30 '22 02:11 github-actions[bot]