parser UPDATE eventTime validation using a compiled leaf
Alternative to compiling a parsed type.
diff --git a/src/parser_common.c b/src/parser_common.c
index c164fdf..6dbac08 100644
--- a/src/parser_common.c
+++ b/src/parser_common.c
@@ -3,7 +3,7 @@
* @author Michal Vasko <mvasko@cesnet.cz>
* @brief libyang common parser functions.
*
- * Copyright (c) 2015 - 2022 CESNET, z.s.p.o.
+ * Copyright (c) 2015 - 2024 CESNET, z.s.p.o.
*
* This source code is licensed under BSD 3-Clause License (the "License").
* You may not use this file except in compliance with the License.
@@ -71,33 +71,47 @@
LY_ERR rc = LY_SUCCESS;
struct ly_ctx *ctx = (struct ly_ctx *)LYD_CTX(node);
struct lysc_ctx cctx;
- const struct lys_module *mod;
+ const struct lys_module *mod1, *mod2;
+ const struct lysc_node *schema;
LY_ARRAY_COUNT_TYPE u;
struct ly_err_item *err = NULL;
struct lysp_type *type_p = NULL;
struct lysc_pattern **patterns = NULL;
const char *value;
- LYSC_CTX_INIT_CTX(cctx, ctx);
+ /* find the used modules, we will either use a compiled leaf or compile the relevant type ourselves */
+ mod1 = ly_ctx_get_module_implemented(ctx, "notifications");
+ mod2 = ly_ctx_get_module_latest(ctx, "ietf-yang-types");
+ assert(mod2);
- /* get date-and-time parsed type */
- mod = ly_ctx_get_module_latest(ctx, "ietf-yang-types");
- assert(mod);
- LY_ARRAY_FOR(mod->parsed->typedefs, u) {
- if (!strcmp(mod->parsed->typedefs[u].name, "date-and-time")) {
- type_p = &mod->parsed->typedefs[u].type;
- break;
+ if (mod1 || !mod2->parsed) {
+ /* get date-and-time leaf */
+ schema = lys_find_path(LYD_CTX(node), NULL, "/notifications:notification/eventTime", 0);
+ LY_CHECK_RET(!schema, LY_ENOTFOUND);
+
+ /* validate the value */
+ value = lyd_get_value(node);
+ LY_CHECK_RET(lyd_value_validate(LYD_CTX(node), schema, value, strlen(value), NULL, NULL, NULL));
+ } else {
+ LYSC_CTX_INIT_CTX(cctx, ctx);
+
+ /* get date-and-time parsed type */
+ LY_ARRAY_FOR(mod2->parsed->typedefs, u) {
+ if (!strcmp(mod2->parsed->typedefs[u].name, "date-and-time")) {
+ type_p = &mod2->parsed->typedefs[u].type;
+ break;
+ }
}
+ assert(type_p);
+
+ /* compile patterns */
+ assert(type_p->patterns);
+ LY_CHECK_GOTO(rc = lys_compile_type_patterns(&cctx, type_p->patterns, NULL, &patterns), cleanup);
+
+ /* validate */
+ value = lyd_get_value(node);
+ rc = lyplg_type_validate_patterns(patterns, value, strlen(value), &err);
}
- assert(type_p);
-
- /* compile patterns */
- assert(type_p->patterns);
- LY_CHECK_GOTO(rc = lys_compile_type_patterns(&cctx, type_p->patterns, NULL, &patterns), cleanup);
-
- /* validate */
- value = lyd_get_value(node);
- rc = lyplg_type_validate_patterns(patterns, value, strlen(value), &err);
cleanup:
FREE_ARRAY(&cctx.free_ctx, patterns, lysc_pattern_free);