validation BUGFIX handle nested when unres

Fixes sysrepo/sysrepo#3334
diff --git a/src/validation.c b/src/validation.c
index 501dd69..a950c54 100644
--- a/src/validation.c
+++ b/src/validation.c
@@ -197,13 +197,14 @@
  * @param[in] np_cont_diff Whether to put NP container into diff or only its children.
  * @param[in,out] node Optional current iteration node, update it if it is deleted.
  * @param[in,out] node_when Optional set with nodes with "when" conditions, may be removed from.
+ * @param[in,out] node_types Optional set with unresolved type nodes, may be removed from.
  * @param[in,out] diff Validation diff.
  * @return 1 if @p node auto-deleted and updated to its next sibling.
  * @return 0 if @p node was not auto-deleted.
  */
 static ly_bool
 lyd_validate_autodel_node_del(struct lyd_node **first, struct lyd_node *del, const struct lys_module *mod,
-        int np_cont_diff, struct lyd_node **node, struct ly_set *node_types, struct lyd_node **diff)
+        int np_cont_diff, struct lyd_node **node, struct ly_set *node_when, struct ly_set *node_types, struct lyd_node **diff)
 {
     struct lyd_node *iter;
     ly_bool node_autodel = 0;
@@ -228,6 +229,16 @@
         }
     }
 
+    if (node_when && node_when->count) {
+        /* remove nested from node_when set */
+        LYD_TREE_DFS_BEGIN(del, iter) {
+            if ((del != iter) && ly_set_contains(node_when, iter, &idx)) {
+                ly_set_rm_index(node_when, idx, NULL);
+            }
+            LYD_TREE_DFS_END(del, iter);
+        }
+    }
+
     if (node_types && node_types->count) {
         /* remove from node_types set */
         LYD_TREE_DFS_BEGIN(del, iter) {
@@ -264,7 +275,7 @@
         uint32_t xpath_options, struct ly_set *node_types, struct lyd_node **diff)
 {
     LY_ERR rc = LY_SUCCESS, r;
-    uint32_t i;
+    uint32_t i, count;
     const struct lysc_when *disabled;
     struct lyd_node *node = NULL;
 
@@ -285,7 +296,12 @@
                 /* when false */
                 if (node->flags & LYD_WHEN_TRUE) {
                     /* autodelete */
-                    lyd_validate_autodel_node_del(tree, node, mod, 1, NULL, node_types, diff);
+                    count = node_when->count;
+                    lyd_validate_autodel_node_del(tree, node, mod, 1, NULL, node_when, node_types, diff);
+                    if (count > node_when->count) {
+                        /* nested nodes removed, we lost the index */
+                        ly_set_contains(node_when, node, &i);
+                    }
                 } else if (val_opts & LYD_VALIDATE_OPERATIONAL) {
                     /* only a warning */
                     LOGWRN(LYD_CTX(node), "When condition \"%s\" not satisfied.", disabled->cond->expr);
@@ -640,7 +656,7 @@
     LYD_LIST_FOR_INST_SAFE(*first, schema, next, iter) {
         if (iter->flags & LYD_DEFAULT) {
             /* default instance found, remove it */
-            if (lyd_validate_autodel_node_del(first, iter, mod, 0, node, NULL, diff)) {
+            if (lyd_validate_autodel_node_del(first, iter, mod, 0, node, NULL, NULL, diff)) {
                 node_autodel = 1;
             }
         }
@@ -685,7 +701,7 @@
         LYD_LIST_FOR_INST_SAFE(*first, schema, next, iter) {
             if (iter->flags & LYD_DEFAULT) {
                 /* default instance, remove it */
-                if (lyd_validate_autodel_node_del(first, iter, mod, 0, node, NULL, diff)) {
+                if (lyd_validate_autodel_node_del(first, iter, mod, 0, node, NULL, NULL, diff)) {
                     node_autodel = 1;
                 }
             }
@@ -695,7 +711,7 @@
         LYD_LIST_FOR_INST(*first, schema, iter) {
             if ((iter->flags & LYD_DEFAULT) && !(iter->flags & LYD_NEW)) {
                 /* old default instance, remove it */
-                if (lyd_validate_autodel_node_del(first, iter, mod, 0, node, NULL, diff)) {
+                if (lyd_validate_autodel_node_del(first, iter, mod, 0, node, NULL, NULL, diff)) {
                     node_autodel = 1;
                 }
                 break;
@@ -753,7 +769,7 @@
     if (!iter) {
         /* there are only default nodes of the case meaning it does not exist and neither should any default nodes
          * of the case, remove this one default node */
-        if (lyd_validate_autodel_node_del(first, *node, mod, 0, node, NULL, diff)) {
+        if (lyd_validate_autodel_node_del(first, *node, mod, 0, node, NULL, NULL, diff)) {
             node_autodel = 1;
         }
     }