failing to get parent context in to a CollectionValidator
See original GitHub issuei’m trying multiple things and failing to figure out how to get a root level object property value in to some sort of context that a collection validator can look to in determining how to validate what it’s responsible for. i am not manually calling validate. validation is happening automagically in that i’m calling .AddFluentValidation()
in my startup. ImplicitlyValidateChildProperties
is set to false. all validator instantiation is done by DI. running core 2.1 and FV 7.6.102,
picture the following:
{
"Type": 1
"LineItems": [
"Quantity": 1
]
}
I want to validate that quantity is some particular value based on that root object type value. the root object is validated by my BulkOrderRequestValidator
and i’m calling
RuleFor(x => x.LineItems)
.NotEmpty()
.SetCollectionValidator(lineItemValidator);
when using a lambda above to set a property of x on the lineItemValidator
, i was finding that the lineItemValidator was executing its own rules prior to the property being set in the root validator.
what is the proper approach to access that root type from within the line item validator?
i’ve tried looking at the context from within the child validator by way of a custom rule.
RuleFor(x => x.Quantity)
.Custom((req, context) =>
{
// context here has no information about anything from the root object.
//IsChildCollectionContext shows as false
//IsChildContext shows as true
//RootContextData only has InvokedByMvc being true, no other values
});
my initial research and fighting with this has pointed me towards this: https://github.com/JeremySkinner/FluentValidation/issues/651 is it at all possible that this is still “a thing”?
apologies if you feel that this belongs on stack overflow. i’m currently of the opinion that the DI / automagic wiring flow in which i’m currently working needs some more love in the documentation arena, specifically this: https://github.com/JeremySkinner/FluentValidation/wiki/d.-Configuring-a-Validator#root-context-data
i feel this is what i need, but the wiring mechanism that i’ve employed doesnt seem to lend itself to using that functionality that you’ve included. it’s not clear to me where the “hook” is for modifying that store in this request pipeline that i’m not initiating.
thanks!
Issue Analytics
- State:
- Created 5 years ago
- Comments:5 (3 by maintainers)
Top GitHub Comments
appreciate the quick response! that’s where my head was going when i collapsed last night. glad i’m not completely off-base.
thanks for the great library!
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.