CommandLineUtils
CommandLineUtils copied to clipboard
Incorrect value of ValidationContext.ObjectInstance
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.
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
Putting this into the 3.0 milestone as something to consider.
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.
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.
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.