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.

failing to get parent context in to a CollectionValidator

See original GitHub issue

i’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:closed
  • Created 5 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
mrtristancommented, Jun 21, 2018

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!

0reactions
lock[bot]commented, Aug 20, 2018

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

FluentValidation: Using a parent property value in a child ...
Reusable property validators could be another way of doing it, but for me it'd have to be a reasonably high level/generic case to...
Read more >
Custom Validators — FluentValidation documentation
The recommended way is to make use of the Predicate Validator to write a custom ... the root (parent) object, the property value...
Read more >
Handling Validation Errors - Python JsonSchema
A collections.deque containing the path to the failed keyword within the schema, but always relative to the original schema as opposed to any...
Read more >
Validation
Describes how to validate documents within the content studio.
Read more >
Hibernate Validator 8.0.1.Final - Jakarta Bean ...
The property's getter method has to be annotated, not its setter. That way also read-only properties can be constrained which have no setter ......
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