Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nested RuleForEach: GetParentObjectAndPropertyName fails to find the PropertyInfo #50

Open
CreddenDCIU opened this issue Jun 26, 2024 · 0 comments

Comments

@CreddenDCIU
Copy link

public class DemographicValidator : AbstractValidator<AppContext>
{
    public DemographicValidator() 
    {
        RuleForEach(x => x.Assignments).ChildRules(x =>
        {
            x.RuleForEach(x => x.StaffHistory).ChildRules(x =>
            {
                x.RuleFor(x => x.StartDate).NotEmpty().WithMessage("Start Date is required.");
            });
        }
    }
}

I've omitted most of the rest of the validation for brevity, as that all worked without issue until this one. Once it gets here it throws a null reference error when trying to get property.GetValue(model, null) because property is null.

private void GetParentObjectAndPropertyName(object model, string propertyPath, out object parentObject, out string propertyName)
{
	if (model == null)
	{
		throw new ArgumentNullException("model");
	}
	Queue<string> queue = new Queue<string>(propertyPath.Split('.'));
	Type type = model.GetType();
	while (queue.Count > 1)
	{
		string text = queue.Dequeue();
		string text2 = null;
		int num = text.IndexOf('[');
		if (num > 0)
		{
			text2 = text.Substring(num + 1, text.Length - num - 2);
			text = text.Remove(num);
		}
		PropertyInfo property = type.GetProperty(text);
		model = ((model == null) ? null : property.GetValue(model, null));
		if (text2 == null)
		{
			type = property.PropertyType;
			continue;
		}
		List<object> list = ((IEnumerable<object>)model).ToList();
		int index = int.Parse(text2);
		model = list[index];
	}
	parentObject = model;
	propertyName = queue.Dequeue();
}

It looks like it's trying to get AppContext.StaffHistory instead of Assignment.StaffHistory.

CreddenDCIU added a commit to CreddenDCIU/blazor-validation that referenced this issue Jun 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant