It allowed for a parent category and a sub-category.
I think the condition is inverted - this should be
proposed_nesting <= max_category_nesting
No, that is correct. I just tried this again and it is all working as expected.
The idea is to ensure that the parent category (top) is on a maximum level of max_category_nesting - 1.
Oh, the loop is running “while invalid”. OK