validation CHANGE validation tasks order fixes

All nodes must be autodeleted before
types can be resolved and final validation
can happen.
diff --git a/src/parser_xml.c b/src/parser_xml.c
index be516d4..30c358c 100644
--- a/src/parser_xml.c
+++ b/src/parser_xml.c
@@ -209,7 +209,7 @@
  * @return LY_ERR value.
  */
 static LY_ERR
-lydxml_nodes(struct lyd_xml_ctx *ctx, struct lyd_node_inner *parent, const char **data, struct lyd_node **node)
+lydxml_nodes(struct lyd_xml_ctx *ctx, struct lyd_node_inner *parent, const char **data, struct lyd_node **first)
 {
     LY_ERR ret = LY_SUCCESS;
     const char *prefix, *name;
@@ -344,8 +344,12 @@
             }
 
             if (!(ctx->options & LYD_OPT_PARSE_ONLY)) {
+                /* new node validation, autodelete CANNOT occur, all nodes are new */
+                ret = lyd_validate_new(lyd_node_children_p(cur), snode, NULL);
+                LY_CHECK_GOTO(ret, cleanup);
+
                 /* add any missing default children */
-                ret = lyd_validate_defaults_r((struct lyd_node_inner *)cur, lyd_node_children_p(cur), cur->schema, NULL,
+                ret = lyd_validate_defaults_r((struct lyd_node_inner *)cur, lyd_node_children_p(cur), NULL, NULL,
                                               &ctx->incomplete_type_validation, &ctx->when_check);
                 LY_CHECK_GOTO(ret, cleanup);
             }
@@ -431,7 +435,7 @@
         attr = NULL;
 
         /* insert */
-        lyd_insert_node((struct lyd_node *)parent, node, cur);
+        lyd_insert_node((struct lyd_node *)parent, first, cur);
 
         cur = NULL;
     }
@@ -449,42 +453,68 @@
         }
     }
     ly_set_erase(&attrs_data, free);
-    if (ret && *node) {
-        lyd_free_siblings(*node);
-        *node = NULL;
+    if (ret && *first) {
+        lyd_free_siblings(*first);
+        *first = NULL;
     }
     return ret;
 }
 
 LY_ERR
-lyd_parse_xml(struct ly_ctx *ctx, const char *data, int options, struct lyd_node **result)
+lyd_parse_xml(struct ly_ctx *ctx, const char *data, int options, struct lyd_node **tree)
 {
     LY_ERR ret = LY_SUCCESS;
     struct lyd_xml_ctx xmlctx = {0};
+    uint32_t i = 0;
+    const struct lys_module *mod;
+    struct lyd_node *first, *next, **first2;
 
     xmlctx.options = options;
     xmlctx.ctx = ctx;
     xmlctx.line = 1;
 
     /* init */
-    *result = NULL;
+    *tree = NULL;
 
-    ret = lydxml_nodes(&xmlctx, NULL, &data, result);
+    /* parse XML data */
+    ret = lydxml_nodes(&xmlctx, NULL, &data, tree);
     LY_CHECK_GOTO(ret, cleanup);
 
     if (!(options & LYD_OPT_PARSE_ONLY)) {
-        /* add top-level default nodes */
-        ret = lyd_validate_defaults_top(result, NULL, 0, ctx, &xmlctx.incomplete_type_validation, &xmlctx.when_check, options);
-        LY_CHECK_GOTO(ret, cleanup);
+        next = *tree;
+        while (1) {
+            if (options & LYD_VALOPT_DATA_ONLY) {
+                mod = lyd_data_next_module(&next, &first);
+            } else {
+                mod = lyd_mod_next_module(next, NULL, 0, ctx, &i, &first);
+            }
+            if (!mod) {
+                break;
+            }
+            if (first == *tree) {
+                /* make sure first2 changes are carried to tree */
+                first2 = tree;
+            } else {
+                first2 = &first;
+            }
 
-        /* finish incompletely validated terminal values/attributes and when conditions */
-        ret = lyd_validate_unres(&xmlctx.incomplete_type_validation, &xmlctx.incomplete_type_validation_attrs,
-                                 &xmlctx.when_check, LYD_XML, lydxml_resolve_prefix, ctx, *result);
-        LY_CHECK_GOTO(ret, cleanup);
+            /* validate new top-level nodes, autodelete CANNOT occur, all nodes are new */
+            ret = lyd_validate_new(first2, NULL, mod);
+            LY_CHECK_GOTO(ret, cleanup);
 
-        /* context node and other validation tasks that depend on other data nodes */
-        ret = lyd_validate_data(result, NULL, 0, ctx, options);
-        LY_CHECK_GOTO(ret, cleanup);
+            /* add all top-level defaults for this module */
+            ret = lyd_validate_defaults_r(NULL, first2, NULL, mod, &xmlctx.incomplete_type_validation, &xmlctx.when_check);
+            LY_CHECK_GOTO(ret, cleanup);
+
+            /* finish incompletely validated terminal values/attributes and when conditions */
+            ret = lyd_validate_unres(tree, &xmlctx.when_check, &xmlctx.incomplete_type_validation,
+                                     &xmlctx.incomplete_type_validation_attrs, LYD_XML, lydxml_resolve_prefix, ctx);
+            LY_CHECK_GOTO(ret, cleanup);
+
+            /* perform final validation that assumes the data tree is final */
+            ret = lyd_validate_siblings_r(*first2, NULL, mod, options);
+            LY_CHECK_GOTO(ret, cleanup);
+        }
     }
 
 cleanup:
@@ -497,8 +527,8 @@
     ly_set_erase(&xmlctx.when_check, NULL);
     lyxml_context_clear((struct lyxml_context *)&xmlctx);
     if (ret) {
-        lyd_free_all(*result);
-        *result = NULL;
+        lyd_free_all(*tree);
+        *tree = NULL;
     }
     return ret;
 }
diff --git a/src/tree_data.c b/src/tree_data.c
index bf3402c..ae5818f 100644
--- a/src/tree_data.c
+++ b/src/tree_data.c
@@ -1,5 +1,5 @@
 /**
- * @file tree_schema.c
+ * @file tree_data.c
  * @author Radek Krejci <rkrejci@cesnet.cz>
  * @brief Schema tree implementation
  *
@@ -451,10 +451,6 @@
     if ((schema->nodetype != LYS_LIST) || (schema->flags & LYS_KEYLESS)) {
         lyd_hash((struct lyd_node *)in);
     }
-    if ((schema->nodetype == LYS_CONTAINER) && !(schema->flags & LYS_PRESENCE)) {
-        /* NP cotnainer always a default */
-        in->flags |= LYD_DEFAULT;
-    }
 
     *node = (struct lyd_node *)in;
     return LY_SUCCESS;
@@ -921,14 +917,19 @@
             lyd_insert_last_node(parent, node);
         }
     } else if (*first_sibling) {
-        /* top-level siblings, find the last one from this module, or simply the last */
+        /* top-level siblings */
         anchor = (*first_sibling)->prev;
         while (anchor->prev->next && (lyd_top_node_module(anchor) != lyd_top_node_module(node))) {
             anchor = anchor->prev;
         }
 
-        /* insert */
-        lyd_insert_after_node(anchor, node);
+        if (lyd_top_node_module(anchor) == lyd_top_node_module(node)) {
+            /* insert after last sibling from this module */
+            lyd_insert_after_node(anchor, node);
+        } else {
+            /* no data from this module, insert at the last position */
+            lyd_insert_after_node((*first_sibling)->prev, node);
+        }
     } else {
         /* the only sibling */
         *first_sibling = node;
@@ -1006,6 +1007,33 @@
 }
 
 API LY_ERR
+lyd_insert_sibling(struct lyd_node *sibling, struct lyd_node *node)
+{
+    struct lyd_node *iter;
+
+    LY_CHECK_ARG_RET(NULL, sibling, node, LY_EINVAL);
+
+    LY_CHECK_RET(lyd_insert_check_schema(sibling->schema->parent, node->schema));
+
+    if (node->schema->flags & LYS_KEY) {
+        LOGERR(sibling->schema->module->ctx, LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
+        return LY_EINVAL;
+    }
+
+    if (node->parent || node->prev->next) {
+        lyd_unlink_tree(node);
+    }
+
+    while (node) {
+        iter = node->next;
+        lyd_unlink_tree(node);
+        lyd_insert_node(NULL, &sibling, node);
+        node = iter;
+    }
+    return LY_SUCCESS;
+}
+
+API LY_ERR
 lyd_insert_before(struct lyd_node *sibling, struct lyd_node *node)
 {
     struct lyd_node *iter;
diff --git a/src/tree_data.h b/src/tree_data.h
index 4943b49..e9ef1f7 100644
--- a/src/tree_data.h
+++ b/src/tree_data.h
@@ -519,13 +519,25 @@
 /**
  * @brief Fully validate a data tree.
  *
- * @param[in] ctx libyang context. Can be NULL if @p node is set.
- * @param[in,out] node Root data tree node to recursively validate. May be changed by validation.
+ * @param[in,out] tree Data tree to recursively validate. May be changed by validation.
+ * @param[in] ctx libyang context. Can be NULL if @p tree is set.
  * @param[in] val_opts Validation options (@ref datavalidationoptions).
  * @return LY_SUCCESS on success.
  * @return LY_ERR error on error.
  */
-LY_ERR lyd_validate(const struct ly_ctx *ctx, struct lyd_node **node, int val_opts);
+LY_ERR lyd_validate(struct lyd_node **tree, const struct ly_ctx *ctx, int val_opts);
+
+/**
+ * @brief Fully validate a data tree.
+ *
+ * @param[in,out] tree Data tree to recursively validate. May be changed by validation.
+ * @param[in] modules Array of modules to validate.
+ * @param[in] mod_count Number of @p modules.
+ * @param[in] val_opts Validation options (@ref datavalidationoptions).
+ * @return LY_SUCCESS on success.
+ * @return LY_ERR error on error.
+ */
+LY_ERR lyd_validate_modules(struct lyd_node **tree, const struct lys_module **modules, int mod_count, int val_opts);
 
 /**
  * @brief Create a new inner node in a data tree.
@@ -605,6 +617,19 @@
 LY_ERR lyd_insert(struct lyd_node *parent, struct lyd_node *node);
 
 /**
+ * @brief Insert a node into siblings. It is inserted as the last sibling.
+ *
+ * - if the node is part of some other tree, it is automatically unlinked.
+ * - if the node is the first node of a node list (with no parent), all the subsequent nodes are also inserted.
+ *
+ * @param[in] sibling Siblings to insert into.
+ * @param[in] node Node to insert.
+ * @return LY_SUCCESS on success.
+ * @return LY_ERR error on error.
+ */
+LY_ERR lyd_insert_sibling(struct lyd_node *sibling, struct lyd_node *node);
+
+/**
  * @brief Insert a node before another node that is its schema sibling.
  *
  * - if the node is part of some other tree, it is automatically unlinked.
diff --git a/src/tree_data_helpers.c b/src/tree_data_helpers.c
index 6e6ee2e..4765b75 100644
--- a/src/tree_data_helpers.c
+++ b/src/tree_data_helpers.c
@@ -64,3 +64,61 @@
     for (schema = node->schema; schema->parent; schema = schema->parent);
     return schema->module;
 }
+
+const struct lys_module *
+lyd_mod_next_module(struct lyd_node *tree, const struct lys_module **modules, int mod_count, const struct ly_ctx *ctx,
+                    uint32_t *i, struct lyd_node **first)
+{
+    struct lyd_node *iter;
+    const struct lys_module *mod;
+
+    /* get the next module */
+    if (modules && mod_count) {
+        if (*i < (unsigned)mod_count) {
+            mod = modules[(*i)++];
+        } else {
+            mod = NULL;
+        }
+    } else {
+        do {
+            mod = ly_ctx_get_module_iter(ctx, i);
+        } while (mod && !mod->implemented);
+    }
+
+    /* find its data */
+    *first = NULL;
+    if (mod) {
+        LY_LIST_FOR(tree, iter) {
+            if (lyd_top_node_module(iter) == mod) {
+                *first = iter;
+                break;
+            }
+        }
+    }
+
+    return mod;
+}
+
+const struct lys_module *
+lyd_data_next_module(struct lyd_node **next, struct lyd_node **first)
+{
+    const struct lys_module *mod;
+
+    if (!*next) {
+        /* all data traversed */
+        *first = NULL;
+        return NULL;
+    }
+
+    *first = *next;
+
+    /* prepare next */
+    mod = lyd_top_node_module(*next);
+    LY_LIST_FOR(*next, *next) {
+        if (lyd_top_node_module(*next) != mod) {
+            break;
+        }
+    }
+
+    return mod;
+}
diff --git a/src/tree_data_internal.h b/src/tree_data_internal.h
index 4da2d49..8825d0f 100644
--- a/src/tree_data_internal.h
+++ b/src/tree_data_internal.h
@@ -19,6 +19,14 @@
 #include "plugins_types.h"
 
 /**
+ * @brief Check whether a node to be deleted is the first top-level sibling.
+ *
+ * @param[in] first First sibling.
+ * @param[in] to_del Node to be deleted.
+ */
+#define LYD_DEL_IS_ROOT(first, to_del) (((first) == (to_del)) && !(first)->parent && !(first)->prev->next)
+
+/**
  * @brief Get address of a node's child pointer if any.
  *
  * @param[in] node Node to check.
@@ -199,10 +207,10 @@
  * @param[in] ctx libyang context
  * @param[in] data Pointer to the XML string representation of the YANG data to parse.
  * @param[in] options @ref dataparseroptions
- * @param[out] result Resulting list of the parsed data trees. Note that NULL can be a valid result.
+ * @param[out] tree Parsed data tree. Note that NULL can be a valid result.
  * @reutn LY_ERR value.
  */
-LY_ERR lyd_parse_xml(struct ly_ctx *ctx, const char *data, int options, struct lyd_node **result);
+LY_ERR lyd_parse_xml(struct ly_ctx *ctx, const char *data, int options, struct lyd_node **tree);
 
 /**
  * @defgroup datahash Data nodes hash manipulation
@@ -286,4 +294,29 @@
  */
 const struct lys_module *lyd_top_node_module(const struct lyd_node *node);
 
+/**
+ * @brief Iterate over implemented modules for functions that accept specific modules or the whole context.
+ *
+ * @param[in] tree Data tree.
+ * @param[in] modules Selected modules, NULL for all.
+ * @param[in] mod_count Count of @p modules.
+ * @param[in] ctx Context, NULL for selected modules.
+ * @param[in,out] i Iterator, set to 0 on first call.
+ * @param[out] first First sibling of the returned module.
+ * @return Next module.
+ * @return NULL if all modules were traversed.
+ */
+const struct lys_module *lyd_mod_next_module(struct lyd_node *tree, const struct lys_module **modules, int mod_count,
+                                             const struct ly_ctx *ctx, uint32_t *i, struct lyd_node **first);
+
+/**
+ * @brief Iterate over modules for functions that want to traverse all the top-level data.
+ *
+ * @param[in,out] next Pointer to the next module data, set to first top-level sibling on first call.
+ * @param[out] first First sibling of the returned module.
+ * @return Next module.
+ * @return NULL if all modules were traversed.
+ */
+const struct lys_module *lyd_data_next_module(struct lyd_node **next, struct lyd_node **first);
+
 #endif /* LY_TREE_DATA_INTERNAL_H_ */
diff --git a/src/validation.c b/src/validation.c
index 96246cd..970401c 100644
--- a/src/validation.c
+++ b/src/validation.c
@@ -74,15 +74,15 @@
 /**
  * @brief Evaluate a single "when" condition.
  *
- * @param[in] when When to evaluate.
+ * @param[in,out] tree Data tree, is updated if some nodes are autodeleted.
  * @param[in] node Node whose existence depends on this when.
- * @param[in] tree Data tree.
+ * @param[in] when When to evaluate.
  * @return LY_ERR value (LY_EINCOMPLETE if a referenced node does not have its when evaluated)
  */
 static LY_ERR
-lyd_val_when(struct lysc_when *when, struct lyd_node *node, const struct lyd_node *tree)
+lyd_val_when(struct lyd_node **tree, struct lyd_node *node, struct lysc_when *when)
 {
-    LY_ERR ret;
+    LY_ERR ret = LY_SUCCESS;
     const struct lyd_node *ctx_node;
     struct lyxp_set xp_set;
 
@@ -97,7 +97,7 @@
 
     /* evaluate when */
     ret = lyxp_eval(when->cond, LYD_UNKNOWN, when->module, ctx_node, ctx_node ? LYXP_NODE_ELEM : LYXP_NODE_ROOT_CONFIG,
-                    tree, &xp_set, LYXP_SCHEMA);
+                    *tree, &xp_set, LYXP_SCHEMA);
     lyxp_set_cast(&xp_set, LYXP_SET_BOOLEAN);
 
     /* return error or LY_EINCOMPLETE for dependant unresolved when */
@@ -107,6 +107,9 @@
     if (!xp_set.val.bool) {
         if (node->flags & LYD_WHEN_TRUE) {
             /* autodelete */
+            if (LYD_DEL_IS_ROOT(*tree, node)) {
+                *tree = (*tree)->next;
+            }
             lyd_free_tree(node);
         } else {
             /* invalid data */
@@ -122,107 +125,319 @@
 }
 
 LY_ERR
-lyd_validate_unres(struct ly_set *node_types, struct ly_set *attr_types, struct ly_set *node_when, LYD_FORMAT format,
-                   ly_clb_resolve_prefix get_prefix_clb, void *parser_data, const struct lyd_node *tree)
+lyd_validate_unres(struct lyd_node **tree, struct ly_set *node_when, struct ly_set *node_types, struct ly_set *attr_types,
+                   LYD_FORMAT format, ly_clb_resolve_prefix get_prefix_clb, void *parser_data)
 {
     LY_ERR ret = LY_SUCCESS;
     uint32_t u;
 
-    /* finish incompletely validated terminal values */
-    for (u = 0; node_types && (u < node_types->count); u++) {
-        struct lyd_node_term *node = (struct lyd_node_term *)node_types->objs[u];
+    if (node_when) {
+        /* evaluate all when conditions */
+        uint32_t prev_count;
+        do {
+            prev_count = node_when->count;
+            u = 0;
+            while (u < node_when->count) {
+                /* evaluate all when expressions that affect this node's existence */
+                struct lyd_node *node = (struct lyd_node *)node_when->objs[u];
+                const struct lysc_node *schema = node->schema;
+                int unres_when = 0;
 
-        /* validate and store the value of the node */
-        ret = lyd_value_parse(node, node->value.original, strlen(node->value.original), 0, 1, get_prefix_clb,
-                            parser_data, format, tree);
-        LY_CHECK_RET(ret);
-    }
-
-    /* ... and attribute values */
-    for (u = 0; attr_types && (u < attr_types->count); u++) {
-        struct lyd_attr *attr = (struct lyd_attr *)attr_types->objs[u];
-
-        /* validate and store the value of the node */
-        ret = lyd_value_parse_attr(attr->parent->schema->module->ctx, attr, attr->value.original,
-                                   strlen(attr->value.original), 0, 1, get_prefix_clb, parser_data, format, NULL, tree);
-        LY_CHECK_RET(ret);
-    }
-
-    /* no when conditions */
-    if (!node_when || !node_when->count) {
-        return ret;
-    }
-
-    /* evaluate all when conditions */
-    uint32_t prev_count;
-    do {
-        prev_count = node_when->count;
-        u = 0;
-        while (u < node_when->count) {
-            /* evaluate all when expressions that affect this node's existence */
-            struct lyd_node *node = (struct lyd_node *)node_when->objs[u];
-            const struct lysc_node *schema = node->schema;
-            int unres_when = 0;
-
-            do {
-                uint32_t i;
-                LY_ARRAY_FOR(schema->when, i) {
-                    ret = lyd_val_when(schema->when[i], node, tree);
-                    if (ret) {
-                        break;
+                do {
+                    uint32_t i;
+                    LY_ARRAY_FOR(schema->when, i) {
+                        ret = lyd_val_when(tree, node, schema->when[i]);
+                        if (ret) {
+                            break;
+                        }
                     }
-                }
-                if (ret == LY_EINCOMPLETE) {
-                    /* could not evaluate this when */
-                    unres_when = 1;
-                    break;
-                } else if (ret) {
-                    /* error */
-                    return ret;
-                }
-                schema = schema->parent;
-            } while (schema && (schema->nodetype & (LYS_CASE | LYS_CHOICE)));
+                    if (ret == LY_EINCOMPLETE) {
+                        /* could not evaluate this when */
+                        unres_when = 1;
+                        break;
+                    } else if (ret) {
+                        /* error */
+                        return ret;
+                    }
+                    schema = schema->parent;
+                } while (schema && (schema->nodetype & (LYS_CASE | LYS_CHOICE)));
 
-            if (unres_when) {
-                /* keep in set and go to the next node */
-                ++u;
-            } else {
-                /* remove this node from the set */
-                ly_set_rm_index(node_when, u, NULL);
+                if (unres_when) {
+                    /* keep in set and go to the next node */
+                    ++u;
+                } else {
+                    /* remove this node from the set */
+                    ly_set_rm_index(node_when, u, NULL);
+                }
             }
-        }
 
-    /* there must have been some when conditions resolved */
-    } while (prev_count > node_when->count);
+        /* there must have been some when conditions resolved */
+        } while (prev_count > node_when->count);
 
-    /* there could have been no cyclic when dependencies, checked during compilation */
-    assert(!node_when->count);
+        /* there could have been no cyclic when dependencies, checked during compilation */
+        assert(!node_when->count);
+    }
+
+    if (node_types && node_types->count) {
+        /* finish incompletely validated terminal values (traverse from the end for efficient set removal) */
+        u = node_types->count;
+        do {
+            --u;
+
+            struct lyd_node_term *node = (struct lyd_node_term *)node_types->objs[u];
+
+            /* validate and store the value of the node */
+            ret = lyd_value_parse(node, node->value.original, strlen(node->value.original), 0, 1, get_prefix_clb,
+                                  parser_data, format, *tree);
+            LY_CHECK_RET(ret);
+
+            /* remove this node from the set */
+            ly_set_rm_index(node_types, u, NULL);
+        } while (u);
+    }
+
+    if (attr_types && attr_types->count) {
+        /* ... and attribute values */
+        u = attr_types->count;
+        do {
+            --u;
+
+            struct lyd_attr *attr = (struct lyd_attr *)attr_types->objs[u];
+
+            /* validate and store the value of the node */
+            ret = lyd_value_parse_attr(attr->parent->schema->module->ctx, attr, attr->value.original,
+                                    strlen(attr->value.original), 0, 1, get_prefix_clb, parser_data, format, NULL, *tree);
+            LY_CHECK_RET(ret);
+
+            /* remove this attr from the set */
+            ly_set_rm_index(attr_types, u, NULL);
+        } while (u);
+    }
 
     return ret;
 }
 
-static const struct lys_module *
-lyd_val_next_module(const struct lys_module **modules, int mod_count, struct ly_ctx *ctx, uint32_t *i)
+static LY_ERR
+lyd_validate_duplicates(const struct lyd_node *first, const struct lyd_node *node)
 {
-    if (modules && mod_count) {
-        return modules[(*i)++];
+    struct lyd_node **match_p;
+    int fail = 0;
+
+    if ((node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) && (node->schema->flags & LYS_CONFIG_R)) {
+        /* duplicate instances allowed */
+        return LY_SUCCESS;
     }
 
-    return ly_ctx_get_module_iter(ctx, i);
+    /* find exactly the same next instance using hashes if possible */
+    if (node->parent && node->parent->children_ht) {
+        if (!lyht_find_next(node->parent->children_ht, &node, node->hash, (void **)&match_p)) {
+            fail = 1;
+        }
+    } else {
+        for (; first; first = first->next) {
+            if (first == node) {
+                continue;
+            }
+
+            if (node->schema->nodetype & (LYD_NODE_ANY | LYS_LEAF)) {
+                if (first->schema == node->schema) {
+                    fail = 1;
+                    break;
+                }
+            } else if (!lyd_compare(first, node, 0)) {
+                fail = 1;
+                break;
+            }
+        }
+    }
+
+    if (fail) {
+        LOGVAL(node->schema->module->ctx, LY_VLOG_LYD, node, LY_VCODE_DUP, node->schema->name);
+        return LY_EVALID;
+    }
+    return LY_SUCCESS;
 }
 
 static LY_ERR
-lyd_validate_mandatory(const struct lysc_node *snode, struct lyd_node *sibling)
+lyd_validate_cases(struct lyd_node **first, const struct lysc_node_choice *choic)
+{
+    const struct lysc_node *scase, *iter, *old_case = NULL, *new_case = NULL;
+    struct lyd_node *match, *to_del;
+    int found;
+
+    LY_LIST_FOR((struct lysc_node *)choic->cases, scase) {
+        found = 0;
+        iter = NULL;
+        match = NULL;
+        while ((match = lys_getnext_data(match, *first, &iter, scase, NULL))) {
+            if (match->flags & LYD_NEW) {
+                /* a new case data found, nothing more to look for */
+                found = 2;
+                break;
+            } else {
+                /* and old case data found */
+                if (found == 0) {
+                    found = 1;
+                }
+            }
+        }
+
+        if (found == 1) {
+            /* there should not be 2 old cases */
+            if (old_case) {
+                /* old data from 2 cases */
+                LOGVAL(choic->module->ctx, LY_VLOG_LYSC, choic, LY_VCODE_DUPCASE, old_case->name, scase->name);
+                return LY_EVALID;
+            }
+
+            /* remember an old existing case */
+            old_case = scase;
+        } else if (found == 2) {
+            if (new_case) {
+                /* new data from 2 cases */
+                LOGVAL(choic->module->ctx, LY_VLOG_LYSC, choic, LY_VCODE_DUPCASE, new_case->name, scase->name);
+                return LY_EVALID;
+            }
+
+            /* remember a new existing case */
+            new_case = scase;
+        }
+    }
+
+    if (old_case && new_case) {
+        /* auto-delete old case */
+        iter = NULL;
+        match = NULL;
+        to_del = NULL;
+        while ((match = lys_getnext_data(match, *first, &iter, old_case, NULL))) {
+            if (LYD_DEL_IS_ROOT(*first, to_del)) {
+                *first = (*first)->next;
+            }
+            lyd_free_tree(to_del);
+            to_del = match;
+        }
+        if (LYD_DEL_IS_ROOT(*first, to_del)) {
+            *first = (*first)->next;
+        }
+        lyd_free_tree(to_del);
+    }
+
+    return LY_SUCCESS;
+}
+
+static int
+lyd_val_has_default(const struct lysc_node *schema)
+{
+    switch (schema->nodetype) {
+    case LYS_LEAF:
+        if (((struct lysc_node_leaf *)schema)->dflt) {
+            return 1;
+        }
+        break;
+    case LYS_LEAFLIST:
+        if (((struct lysc_node_leaflist *)schema)->dflts) {
+            return 1;
+        }
+        break;
+    case LYS_CONTAINER:
+        if (!(schema->flags & LYS_PRESENCE)) {
+            return 1;
+        }
+        break;
+    default:
+        break;
+    }
+
+    return 0;
+}
+
+static void
+lyd_validate_autodel_dup(struct lyd_node **first, struct lyd_node *node, struct lyd_node **next_p)
+{
+    struct lyd_node *match, *next;
+
+    if (lyd_val_has_default(node->schema)) {
+        assert(node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_CONTAINER));
+        if (node->schema->nodetype == LYS_LEAFLIST) {
+            lyd_find_sibling_next2(*first, node->schema, NULL, 0, &match);
+        } else {
+            lyd_find_sibling_val(*first, node->schema, NULL, 0, &match);
+        }
+
+        while (match) {
+            next = match->next;
+            if ((match->flags & LYD_DEFAULT) && !(match->flags & LYD_NEW)) {
+                /* default instance found, remove it */
+                if (LYD_DEL_IS_ROOT(*first, match)) {
+                    *first = (*first)->next;
+                }
+                if (match == *next_p) {
+                    *next_p = (*next_p)->next;
+                }
+                lyd_free_tree(match);
+
+                /* remove only a single container/leaf default instance, if there are more, it is an error */
+                if (node->schema->nodetype & (LYS_LEAF | LYS_CONTAINER)) {
+                    break;
+                }
+            }
+
+            lyd_find_sibling_next2(next, node->schema, NULL, 0, &match);
+        }
+    }
+}
+
+LY_ERR
+lyd_validate_new(struct lyd_node **first, const struct lysc_node *sparent, const struct lys_module *mod)
+{
+    struct lyd_node *next, *node;
+    const struct lysc_node *snode = NULL;
+
+    assert(first && (sparent || mod));
+
+    while (*first && (snode = lys_getnext(snode, sparent, mod ? mod->compiled : NULL, LYS_GETNEXT_WITHCHOICE))) {
+        /* check case duplicites */
+        if (snode->nodetype == LYS_CHOICE) {
+            LY_CHECK_RET(lyd_validate_cases(first, (struct lysc_node_choice *)snode));
+        }
+    }
+
+    LY_LIST_FOR_SAFE(*first, next, node) {
+        if (mod && (lyd_top_node_module(node) != mod)) {
+            /* all top-level data from this module checked */
+            break;
+        }
+
+        if (!(node->flags & LYD_NEW)) {
+            /* check only new nodes */
+            continue;
+        }
+
+        /* remove old default(s) if it exists */
+        lyd_validate_autodel_dup(first, node, &next);
+
+        /* then check new node instance duplicities */
+        LY_CHECK_RET(lyd_validate_duplicates(*first, node));
+
+        /* this node is valid */
+        node->flags &= ~LYD_NEW;
+    }
+
+    return LY_SUCCESS;
+}
+
+static LY_ERR
+lyd_validate_mandatory(const struct lyd_node *first, const struct lysc_node *snode)
 {
     if (snode->nodetype == LYS_CHOICE) {
         /* some data of a choice case exist */
-        if (lys_getnext_data(NULL, sibling, NULL, snode, NULL)) {
+        if (lys_getnext_data(NULL, first, NULL, snode, NULL)) {
             return LY_SUCCESS;
         }
     } else {
         assert(snode->nodetype & (LYS_LEAF | LYS_CONTAINER | LYD_NODE_ANY));
 
-        if (!lyd_find_sibling_val(sibling, snode, NULL, 0, NULL)) {
+        if (!lyd_find_sibling_val(first, snode, NULL, 0, NULL)) {
             /* data instance found */
             return LY_SUCCESS;
         }
@@ -234,14 +449,14 @@
 }
 
 static LY_ERR
-lyd_validate_minmax(const struct lysc_node *snode, uint32_t min, uint32_t max, struct lyd_node *sibling)
+lyd_validate_minmax(const struct lyd_node *first, const struct lysc_node *snode, uint32_t min, uint32_t max)
 {
     uint32_t count = 0;
-    struct lyd_node *iter;
+    const struct lyd_node *iter;
 
     assert(min || max);
 
-    LY_LIST_FOR(sibling, iter) {
+    LY_LIST_FOR(first, iter) {
         if (iter->schema == snode) {
             ++count;
 
@@ -406,9 +621,9 @@
 }
 
 static LY_ERR
-lyd_validate_unique(const struct lysc_node *snode, struct lysc_node_leaf ***uniques, struct lyd_node *sibling)
+lyd_validate_unique(const struct lyd_node *first, const struct lysc_node *snode, struct lysc_node_leaf ***uniques)
 {
-    struct lyd_node *diter;
+    const struct lyd_node *diter;
     struct ly_set *set;
     uint32_t i, j, n = 0;
     LY_ERR ret = LY_SUCCESS;
@@ -424,9 +639,9 @@
     /* get all list instances */
     set = ly_set_new();
     LY_CHECK_ERR_RET(!set, LOGMEM(ctx), LY_EMEM);
-    LY_LIST_FOR(sibling, diter) {
+    LY_LIST_FOR(first, diter) {
         if (diter->schema == snode) {
-            ly_set_add(set, diter, LY_SET_OPT_USEASLIST);
+            ly_set_add(set, (void *)diter, LY_SET_OPT_USEASLIST);
         }
     }
 
@@ -517,71 +732,7 @@
 }
 
 static LY_ERR
-lyd_validate_cases(const struct lysc_node_choice *choic, struct lyd_node **sibling)
-{
-    const struct lysc_node *scase, *iter, *old_case = NULL, *new_case = NULL;
-    struct lyd_node *match, *to_del;
-    int found;
-
-    LY_LIST_FOR((struct lysc_node *)choic->cases, scase) {
-        found = 0;
-        iter = NULL;
-        match = NULL;
-        while ((match = lys_getnext_data(match, *sibling, &iter, scase, NULL))) {
-            if (match->flags & LYD_NEW) {
-                /* a new case data found, nothing more to look for */
-                found = 2;
-                break;
-            } else {
-                /* and old case data found */
-                if (found == 0) {
-                    found = 1;
-                }
-            }
-        }
-
-        if (found == 1) {
-            /* there cannot be 2 old cases */
-            assert(!old_case);
-
-            /* remember an old existing case */
-            old_case = scase;
-        } else if (found == 2) {
-            if (new_case) {
-                /* new data from 2 cases */
-                LOGVAL(choic->module->ctx, LY_VLOG_LYSC, choic, LY_VCODE_DUPCASE, new_case->name, scase->name);
-                return LY_EVALID;
-            }
-
-            /* remember a new existing case */
-            new_case = scase;
-        }
-    }
-
-    if (old_case && new_case) {
-        /* auto-delete old case */
-        iter = NULL;
-        match = NULL;
-        to_del = NULL;
-        while ((match = lys_getnext_data(match, *sibling, &iter, old_case, NULL))) {
-            if ((*sibling == to_del) && !(*sibling)->parent) {
-                *sibling = (*sibling)->next;
-            }
-            lyd_free_tree(to_del);
-            to_del = match;
-        }
-        if ((*sibling == to_del) && !(*sibling)->parent) {
-            *sibling = (*sibling)->next;
-        }
-        lyd_free_tree(to_del);
-    }
-
-    return LY_SUCCESS;
-}
-
-static LY_ERR
-lyd_validate_siblings_schema_r(struct lyd_node **sibling, const struct lysc_node *sparent, const struct lysc_module *mod,
-                               int options)
+lyd_validate_siblings_schema_r(const struct lyd_node *first, const struct lysc_node *sparent, const struct lysc_module *mod)
 {
     const struct lysc_node *snode = NULL;
     struct lysc_node_list *slist;
@@ -592,109 +743,42 @@
         if (snode->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
             slist = (struct lysc_node_list *)snode;
             if (slist->min || slist->max) {
-                LY_CHECK_RET(lyd_validate_minmax(snode, slist->min, slist->max, *sibling));
+                LY_CHECK_RET(lyd_validate_minmax(first, snode, slist->min, slist->max));
             }
 
         /* check generic mandatory existence */
         } else if (snode->flags & LYS_MAND_TRUE) {
-            LY_CHECK_RET(lyd_validate_mandatory(snode, *sibling));
+            LY_CHECK_RET(lyd_validate_mandatory(first, snode));
         }
 
         /* check unique */
         if (snode->nodetype == LYS_LIST) {
             slist = (struct lysc_node_list *)snode;
             if (slist->uniques) {
-                LY_CHECK_RET(lyd_validate_unique(snode, slist->uniques, *sibling));
+                LY_CHECK_RET(lyd_validate_unique(first, snode, slist->uniques));
             }
         }
 
-        /* check case duplicites */
-        if (snode->nodetype == LYS_CHOICE) {
-            LY_CHECK_RET(lyd_validate_cases((struct lysc_node_choice *)snode, sibling));
-        }
-
         if (snode->nodetype & (LYS_CHOICE | LYS_CASE)) {
             /* go recursively for schema-only nodes */
-            LY_CHECK_RET(lyd_validate_siblings_schema_r(sibling, snode, mod, options));
+            LY_CHECK_RET(lyd_validate_siblings_schema_r(first, snode, mod));
         }
     }
 
     return LY_SUCCESS;
 }
 
-static LY_ERR
-lyd_validate_dup_nodes(struct lyd_node *sibling, struct lyd_node *node)
+LY_ERR
+lyd_validate_siblings_r(struct lyd_node *first, const struct lysc_node *sparent, const struct lys_module *mod,
+                        int val_opts)
 {
-    struct lyd_node **match_p;
-    int fail = 0;
-
-    if ((node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) && (node->schema->flags & LYS_CONFIG_R)) {
-        /* duplicate instances allowed */
-        return LY_SUCCESS;
-    }
-
-    /* find exactly the same next instance using hashes if possible */
-    if (node->parent && node->parent->children_ht) {
-        if (!lyht_find_next(node->parent->children_ht, &node, node->hash, (void **)&match_p)) {
-            fail = 1;
-        }
-    } else {
-        for (; sibling; sibling = sibling->next) {
-            if (sibling == node) {
-                continue;
-            }
-
-            if (node->schema->nodetype & (LYD_NODE_ANY | LYS_LEAF)) {
-                if (sibling->schema == node->schema) {
-                    fail = 1;
-                    break;
-                }
-            } else if (!lyd_compare(sibling, node, 0)) {
-                fail = 1;
-                break;
-            }
-        }
-    }
-
-    if (fail) {
-        LOGVAL(node->schema->module->ctx, LY_VLOG_LYD, node, LY_VCODE_DUP, node->schema->name);
-        return LY_EVALID;
-    }
-    return LY_SUCCESS;
-}
-
-static LY_ERR
-lyd_validate_siblings_r(struct lyd_node **sibling, const struct lysc_node *sparent, const struct lysc_module *mod, int options)
-{
-    struct lyd_node *next, *node, *match;
-
-    assert(sibling);
+    struct lyd_node *next, *node;
 
     /* validate all restrictions of nodes themselves */
-    LY_LIST_FOR_SAFE(*sibling, next, node) {
-        if (node->flags & LYD_NEW) {
-            /* new node instance duplicities */
-            LY_CHECK_RET(lyd_validate_dup_nodes(*sibling, node));
-        } else if (node->flags & LYD_DEFAULT) {
-            /* remove default if there is an explicit instance */
-            assert(node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_CONTAINER));
-            if (node->schema->nodetype == LYS_LEAFLIST) {
-                lyd_find_sibling_next2(*sibling, node->schema, NULL, 0, &match);
-            } else {
-                lyd_find_sibling_val(*sibling, node->schema, NULL, 0, &match);
-            }
-            while (match && (match->flags & LYD_DEFAULT) && !(match->flags & LYD_NEW)) {
-                lyd_find_sibling_next2(match->next, node->schema, NULL, 0, &match);
-            }
-
-            if (match) {
-                /* non-default (or at least new for container) instance found, remove the default one */
-                if ((*sibling == node) && !(*sibling)->parent) {
-                    *sibling = (*sibling)->next;
-                }
-                lyd_free_tree(node);
-                continue;
-            }
+    LY_LIST_FOR_SAFE(first, next, node) {
+        if (mod && (lyd_top_node_module(node) != mod)) {
+            /* all top-level data from this module checked */
+            break;
         }
 
         /* TODO node's must */
@@ -705,42 +789,22 @@
     }
 
     /* validate schema-based restrictions */
-    LY_CHECK_RET(lyd_validate_siblings_schema_r(sibling, sparent, mod, options));
+    LY_CHECK_RET(lyd_validate_siblings_schema_r(first, sparent, mod ? mod->compiled : NULL));
 
-    LY_LIST_FOR(*sibling, node) {
-        /* this sibling is valid */
-        node->flags &= ~LYD_NEW;
-
+    LY_LIST_FOR(first, node) {
         /* validate all children recursively */
-        if (node->schema->nodetype & LYD_NODE_INNER) {
-            LY_CHECK_RET(lyd_validate_siblings_r(lyd_node_children_p(node), node->schema, mod, options));
-        }
-    }
+        LY_CHECK_RET(lyd_validate_siblings_r((struct lyd_node *)lyd_node_children(node), node->schema, NULL, val_opts));
 
-    return LY_SUCCESS;
-}
-
-LY_ERR
-lyd_validate_data(struct lyd_node **tree, const struct lys_module **modules, int mod_count, struct ly_ctx *ctx,
-                  int val_opts)
-{
-    uint32_t i = 0;
-    const struct lys_module *mod;
-
-    if (val_opts & LYD_VALOPT_DATA_ONLY) {
-        if (*tree) {
-            /* TODO all modules, not just first */
-            /* validate all top-level nodes and then inner nodes recursively */
-            LY_CHECK_RET(lyd_validate_siblings_r(tree, NULL, (*tree)->schema->module->compiled, val_opts));
-        }
-    } else {
-        while ((mod = lyd_val_next_module(modules, mod_count, ctx, &i))) {
-            if (!mod->implemented) {
-                continue;
+        /* set default for containers */
+        if ((node->schema->nodetype == LYS_CONTAINER) && !(node->schema->flags & LYS_PRESENCE)) {
+            LY_LIST_FOR((struct lyd_node *)lyd_node_children(node), next) {
+                if (!(next->flags & LYD_DEFAULT)) {
+                    break;
+                }
             }
-
-            /* validate all top-level nodes and then inner nodes recursively */
-            LY_CHECK_RET(lyd_validate_siblings_r(tree, NULL, mod->compiled, val_opts));
+            if (!next) {
+                node->flags |= LYD_DEFAULT;
+            }
         }
     }
 
@@ -748,17 +812,22 @@
 }
 
 LY_ERR
-lyd_validate_defaults_r(struct lyd_node_inner *parent, struct lyd_node **first, const struct lysc_node *schema,
-                        const struct lysc_module *mod, struct ly_set *node_types, struct ly_set *node_when)
+lyd_validate_defaults_r(struct lyd_node *parent, struct lyd_node **first, const struct lysc_node *sparent,
+                        const struct lys_module *mod, struct ly_set *node_types, struct ly_set *node_when)
 {
+    LY_ERR ret;
     const struct lysc_node *iter = NULL;
     struct lyd_node *node;
     struct lyd_value **dflts;
     size_t i;
 
-    assert(first && (schema || mod) && node_types && node_when);
+    assert(first && (parent || sparent || mod) && node_types && node_when);
 
-    while ((iter = lys_getnext(iter, schema, mod, LYS_GETNEXT_WITHCHOICE))) {
+    if (!sparent && parent) {
+        sparent = parent->schema;
+    }
+
+    while ((iter = lys_getnext(iter, sparent, mod ? mod->compiled : NULL, LYS_GETNEXT_WITHCHOICE))) {
         switch (iter->nodetype) {
         case LYS_CHOICE:
             if (((struct lysc_node_choice *)iter)->dflt && !lys_getnext_data(NULL, *first, NULL, iter, NULL)) {
@@ -769,22 +838,32 @@
             break;
         case LYS_CONTAINER:
             if (!(iter->flags & LYS_PRESENCE) && lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) {
-                /* create default NP container (default flag automatically set) */
+                /* create default NP container */
                 LY_CHECK_RET(lyd_create_inner(iter, &node));
-                lyd_insert_node((struct lyd_node *)parent, first, node);
+                node->flags = LYD_DEFAULT;
+                lyd_insert_node(parent, first, node);
 
                 if (iter->when) {
                     /* remember to resolve when */
                     ly_set_add(node_when, node, LY_SET_OPT_USEASLIST);
                 }
+
+                /* create any default children */
+                LY_CHECK_RET(lyd_validate_defaults_r(node, lyd_node_children_p(node), NULL, NULL, node_types, node_when));
             }
             break;
         case LYS_LEAF:
             if (((struct lysc_node_leaf *)iter)->dflt && lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) {
                 /* create default leaf */
-                LY_CHECK_RET(lyd_create_term2(iter, ((struct lysc_node_leaf *)iter)->dflt, &node));
-                node->flags |= LYD_DEFAULT;
-                lyd_insert_node((struct lyd_node *)parent, first, node);
+                ret = lyd_create_term2(iter, ((struct lysc_node_leaf *)iter)->dflt, &node);
+                if (ret == LY_EINCOMPLETE) {
+                    /* remember to resolve type */
+                    ly_set_add(node_types, node, LY_SET_OPT_USEASLIST);
+                } else if (ret) {
+                    return ret;
+                }
+                node->flags = LYD_DEFAULT;
+                lyd_insert_node(parent, first, node);
 
                 if (iter->when) {
                     /* remember to resolve when */
@@ -797,9 +876,15 @@
                 /* create all default leaf-lists */
                 dflts = ((struct lysc_node_leaflist *)iter)->dflts;
                 LY_ARRAY_FOR(dflts, i) {
-                    LY_CHECK_RET(lyd_create_term2(iter, dflts[i], &node));
-                    node->flags |= LYD_DEFAULT;
-                    lyd_insert_node((struct lyd_node *)parent, first, node);
+                    ret = lyd_create_term2(iter, dflts[i], &node);
+                    if (ret == LY_EINCOMPLETE) {
+                        /* remember to resolve type */
+                        ly_set_add(node_types, node, LY_SET_OPT_USEASLIST);
+                    } else if (ret) {
+                        return ret;
+                    }
+                    node->flags = LYD_DEFAULT;
+                    lyd_insert_node(parent, first, node);
 
                     if (iter->when) {
                         /* remember to resolve when */
@@ -817,92 +902,102 @@
     return LY_SUCCESS;
 }
 
-LY_ERR
-lyd_validate_defaults_top(struct lyd_node **first, const struct lys_module **modules, int mod_count, struct ly_ctx *ctx,
-                          struct ly_set *node_types, struct ly_set *node_when, int val_opts)
-{
-    uint32_t i = 0;
-    const struct lys_module *mod;
-    struct lyd_node *sibling;
-
-    assert(node_types && node_when);
-
-    if (val_opts & LYD_VALOPT_DATA_ONLY) {
-        mod = NULL;
-        LY_LIST_FOR(*first, sibling) {
-            if (lyd_top_node_module(sibling) != mod) {
-                /* remember this module */
-                mod = lyd_top_node_module(sibling);
-
-                /* add all top-level defaults for this module */
-                LY_CHECK_RET(lyd_validate_defaults_r(NULL, first, NULL, mod->compiled, node_types, node_when));
-            }
-        }
-    } else {
-        while ((mod = lyd_val_next_module(modules, mod_count, ctx, &i))) {
-            if (!mod->implemented) {
-                continue;
-            }
-
-            /* add all top-level defaults for this module */
-            LY_CHECK_RET(lyd_validate_defaults_r(NULL, first, NULL, mod->compiled, node_types, node_when));
-        }
-    }
-
-    return LY_SUCCESS;
-}
-
-API LY_ERR
-lyd_validate(const struct ly_ctx *ctx, struct lyd_node **tree, int val_opts)
+static LY_ERR
+_lyd_validate(struct lyd_node **tree, const struct lys_module **modules, int mod_count, const struct ly_ctx *ctx,
+              int val_opts)
 {
     LY_ERR ret = LY_SUCCESS;
-    struct lyd_node *root, *next, *elem;
+    struct lyd_node *first, *next, *node, **first2;
+    const struct lys_module *mod;
     const struct lyd_attr *attr;
     struct ly_set type_check = {0}, type_attr_check = {0}, when_check = {0};
+    uint32_t i = 0;
 
-    LY_CHECK_ARG_RET(ctx, tree, ctx || *tree, LY_EINVAL);
+    LY_CHECK_ARG_RET(NULL, tree, *tree || ctx || (modules && mod_count), LY_EINVAL);
 
-    if (!ctx) {
-        ctx = (*tree)->schema->module->ctx;
-    }
-
-    /* add nested defaults, collect all types and nodes with when condition */
-    LY_LIST_FOR(*tree, root) {
-        LYD_TREE_DFS_BEGIN(root, next, elem) {
-            LY_LIST_FOR(elem->attr, attr) {
-                ly_set_add(&type_attr_check, (void *)attr, LY_SET_OPT_USEASLIST);
-            }
-            if (elem->schema->nodetype & LYD_NODE_TERM) {
-                ly_set_add(&type_check, (void *)elem, LY_SET_OPT_USEASLIST);
-            } else if (elem->schema->nodetype & LYD_NODE_INNER) {
-                ret = lyd_validate_defaults_r((struct lyd_node_inner *)elem, lyd_node_children_p((struct lyd_node *)elem),
-                                              elem->schema, NULL, &type_check, &when_check);
-                LY_CHECK_GOTO(ret, cleanup);
-            }
-            if (!(elem->schema->nodetype & (LYS_ACTION | LYS_NOTIF)) && elem->schema->when) {
-                ly_set_add(&when_check, (void *)elem, LY_SET_OPT_USEASLIST);
-            }
-
-            LYD_TREE_DFS_END(root, next, elem);
+    next = *tree;
+    while (1) {
+        if (val_opts & LYD_VALOPT_DATA_ONLY) {
+            mod = lyd_data_next_module(&next, &first);
+        } else {
+            mod = lyd_mod_next_module(next, modules, mod_count, ctx, &i, &first);
         }
+        if (!mod) {
+            break;
+        }
+        if (first == *tree) {
+            /* make sure first2 changes are carried to tree */
+            first2 = tree;
+        } else {
+            first2 = &first;
+        }
+
+        /* validate new top-level nodes of this module, autodelete */
+        ret = lyd_validate_new(first2, NULL, mod);
+        LY_CHECK_GOTO(ret, cleanup);
+
+        /* add all top-level defaults for this module */
+        ret = lyd_validate_defaults_r(NULL, first2, NULL, mod, &type_check, &when_check);
+        LY_CHECK_GOTO(ret, cleanup);
+
+        /* process nested nodes */
+        LY_LIST_FOR(*first2, first) {
+            LYD_TREE_DFS_BEGIN(first, next, node) {
+                /* skip added default nodes */
+                if ((node->flags & (LYD_DEFAULT | LYD_NEW)) != (LYD_DEFAULT | LYD_NEW)) {
+                    LY_LIST_FOR(node->attr, attr) {
+                        /* attribute type resolution */
+                        ly_set_add(&type_attr_check, (void *)attr, LY_SET_OPT_USEASLIST);
+                    }
+
+                    if (node->schema->nodetype & LYD_NODE_TERM) {
+                        /* node type resolution */
+                        ly_set_add(&type_check, (void *)node, LY_SET_OPT_USEASLIST);
+                    } else if (node->schema->nodetype & LYD_NODE_INNER) {
+                        /* new node validation, autodelete */
+                        ret = lyd_validate_new(lyd_node_children_p((struct lyd_node *)node), node->schema, NULL);
+                        LY_CHECK_GOTO(ret, cleanup);
+
+                        /* add nested defaults */
+                        ret = lyd_validate_defaults_r(node, lyd_node_children_p((struct lyd_node *)node), NULL, NULL, &type_check,
+                                                    &when_check);
+                        LY_CHECK_GOTO(ret, cleanup);
+                    }
+
+                    if (!(node->schema->nodetype & (LYS_ACTION | LYS_NOTIF)) && node->schema->when) {
+                        /* when evaluation */
+                        ly_set_add(&when_check, (void *)node, LY_SET_OPT_USEASLIST);
+                    }
+                }
+
+                LYD_TREE_DFS_END(first, next, node);
+            }
+        }
+
+        /* finish incompletely validated terminal values/attributes and when conditions */
+        ret = lyd_validate_unres(tree, &when_check, &type_check, &type_attr_check, LYD_JSON, lydjson_resolve_prefix, NULL);
+        LY_CHECK_GOTO(ret, cleanup);
+
+        /* perform final validation that assumes the data tree is final */
+        ret = lyd_validate_siblings_r(*first2, NULL, mod, val_opts);
+        LY_CHECK_GOTO(ret, cleanup);
     }
 
-    /* add top-level default nodes */
-    ret = lyd_validate_defaults_top(tree, NULL, 0, (struct ly_ctx *)ctx, &type_check, &when_check, val_opts);
-    LY_CHECK_GOTO(ret, cleanup);
-
-    /* finish incompletely validated terminal values/attributes and when conditions */
-    ret = lyd_validate_unres(&type_check, &type_attr_check, &when_check, LYD_JSON, lydjson_resolve_prefix, NULL,
-                             (const struct lyd_node *)tree);
-    LY_CHECK_GOTO(ret, cleanup);
-
-    /* context node and other validation tasks that depend on other data nodes */
-    ret = lyd_validate_data(tree, NULL, 0, (struct ly_ctx *)ctx, val_opts);
-    LY_CHECK_GOTO(ret, cleanup);
-
 cleanup:
     ly_set_erase(&type_check, NULL);
     ly_set_erase(&type_attr_check, NULL);
     ly_set_erase(&when_check, NULL);
     return ret;
 }
+
+API LY_ERR
+lyd_validate(struct lyd_node **tree, const struct ly_ctx *ctx, int val_opts)
+{
+    return _lyd_validate(tree, NULL, 0, ctx, val_opts);
+}
+
+API LY_ERR
+lyd_validate_modules(struct lyd_node **tree, const struct lys_module **modules, int mod_count, int val_opts)
+{
+    return _lyd_validate(tree, modules, mod_count, NULL, val_opts);
+}
diff --git a/src/validation.h b/src/validation.h
index d8b62a4..c755a13 100644
--- a/src/validation.h
+++ b/src/validation.h
@@ -19,60 +19,58 @@
 #include "tree_data.h"
 
 /**
- * @brief Finish validation of nodes and attributes. Specifically, type and when validation.
+ * @brief Finish validation of nodes and attributes. Specifically, when (is processed first) and type validation.
  *
+ * !! It is assumed autodeleted nodes cannot be in the unresolved node type set !!
+ *
+ * @param[in,out] tree Data tree, is updated if some nodes are autodeleted.
+ * @param[in] node_when Set with nodes with "when" conditions, can be NULL.
  * @param[in] node_types Set with nodes with unresolved types, can be NULL
  * @param[in] attr_types Set with attributes with unresolved types, can be NULL.
- * @param[in] node_when Set with nodes with "when" conditions, can be NULL.
  * @param[in] format Format of the unresolved data.
  * @param[in] get_prefix_clb Format-specific getter to resolve prefixes.
  * @param[in] parser_data Parser's data for @p get_prefix_clb.
- * @param[in] tree Data tree.
  * @return LY_ERR value.
  */
-LY_ERR lyd_validate_unres(struct ly_set *node_types, struct ly_set *attr_types, struct ly_set *node_when, LYD_FORMAT format,
-                          ly_clb_resolve_prefix get_prefix_clb, void *parser_data, const struct lyd_node *tree);
+LY_ERR lyd_validate_unres(struct lyd_node **tree, struct ly_set *node_when, struct ly_set *node_types, struct ly_set *attr_types,
+                          LYD_FORMAT format, ly_clb_resolve_prefix get_prefix_clb, void *parser_data);
 
 /**
- * @brief Perform all validation tasks, the data tree must be complete when calling this function.
+ * @brief Validate new siblings. Specifically, check duplicated instances, autodelete default values and cases.
  *
- * @param[in,out] tree Data tree.
- * @param[in] modules Array of modules that should be validated, NULL for all modules.
- * @param[in] mod_count Modules count.
- * @param[in] ctx Context if all modules should be validated, NULL for only selected modules.
+ * !! It is assumed autodeleted nodes cannot yet be in the unresolved node type set !!
+ *
+ * @param[in,out] first First sibling.
+ * @param[in] sparent Schema parent of the siblings, NULL for top-level siblings.
+ * @param[in] mod Module of the siblings, NULL for nested siblings.
+ * @return LY_ERR value.
+ */
+LY_ERR lyd_validate_new(struct lyd_node **first, const struct lysc_node *sparent, const struct lys_module *mod);
+
+/**
+ * @brief Perform all remaining validation tasks, the data tree must be final when calling this function.
+ *
+ * @param[in] first First sibling.
+ * @param[in] sparent Schema parent of the siblings, NULL for top-level siblings.
+ * @param[in] mod Module of the siblings, NULL for nested siblings.
  * @param[in] val_opts Validation options (@ref datavalidationoptions).
  * @return LY_ERR value.
  */
-LY_ERR lyd_validate_data(struct lyd_node **tree, const struct lys_module **modules, int mod_count,
-                         struct ly_ctx *ctx, int val_opts);
+LY_ERR lyd_validate_siblings_r(struct lyd_node *first, const struct lysc_node *sparent, const struct lys_module *mod,
+                               int val_opts);
 
 /**
  * @brief Check the existence and create any non-existing default siblings, recursively for the created nodes.
  *
- * @param[in] parent Parent of the potential default values.
+ * @param[in] parent Parent of the potential default values, NULL for top-level siblings.
  * @param[in,out] first First sibling.
- * @param[in] schema Schema parent of the default values, NULL for top-level siblings.
- * @param[in] mod Module of the default values, NULL for nested (non top-level) siblings.
+ * @param[in] sparent Schema parent of the siblings, NULL if schema of @p parent can be used.
+ * @param[in] mod Module of the default values, NULL for nested siblings.
  * @param[in] node_types Set to add nodes with unresolved types into.
  * @param[in] node_when Set to add nodes with "when" conditions into.
  * @return LY_ERR value.
  */
-LY_ERR lyd_validate_defaults_r(struct lyd_node_inner *parent, struct lyd_node **first, const struct lysc_node *schema,
-                               const struct lysc_module *mod, struct ly_set *node_types, struct ly_set *node_when);
-
-/**
- * @brief Check the existence and create any non-existing default top-level nodes.
- *
- * @param[in,out] first First top-level sibling. There may be no explicit nodes.
- * @param[in] modules Array of modules that should be considered, NULL for all modules.
- * @param[in] mod_count Modules count.
- * @param[in] ctx Context if all modules should be considered, NULL for only selected modules.
- * @param[in] node_types Set to add nodes with unresolved types into, can be NULL if not needed.
- * @param[in] node_when Set to add nodes with "when" conditions into, can be NULL if not needed.
- * @param[in] val_opts Relevant validation options (#LYD_VALOPT_DATA_ONLY).
- * @return LY_ERR value.
- */
-LY_ERR lyd_validate_defaults_top(struct lyd_node **first, const struct lys_module **modules, int mod_count,
-                                 struct ly_ctx *ctx, struct ly_set *node_types, struct ly_set *node_when, int val_opts);
+LY_ERR lyd_validate_defaults_r(struct lyd_node_inner *parent, struct lyd_node **first, const struct lysc_node *sparent,
+                               const struct lys_module *mod, struct ly_set *node_types, struct ly_set *node_when);
 
 #endif /* LY_VALIDATION_H_ */