Skip to content

Commit 3adb304

Browse files
committed
validation UPDATE improve list min/max log path
Fixes #2353
1 parent 02126d7 commit 3adb304

File tree

3 files changed

+50
-26
lines changed

3 files changed

+50
-26
lines changed

src/validation.c

Lines changed: 45 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1140,13 +1140,17 @@ static LY_ERR
11401140
lyd_validate_minmax(const struct lyd_node *first, const struct lyd_node *parent, const struct lysc_node *snode,
11411141
uint32_t min, uint32_t max, uint32_t val_opts)
11421142
{
1143+
LY_ERR rc = LY_SUCCESS;
11431144
uint32_t count = 0;
1144-
struct lyd_node *iter;
1145+
struct lyd_node *iter, *last_iter = NULL;
11451146
const struct lysc_when *disabled;
1147+
char *log_path;
1148+
int r;
11461149

11471150
assert(min || max);
11481151

11491152
LYD_LIST_FOR_INST(first, snode, iter) {
1153+
last_iter = iter;
11501154
++count;
11511155

11521156
if (min && (count == min)) {
@@ -1182,32 +1186,52 @@ lyd_validate_minmax(const struct lyd_node *first, const struct lyd_node *parent,
11821186
max = 0;
11831187
}
11841188

1185-
if (min) {
1186-
if (val_opts & LYD_VALIDATE_OPERATIONAL) {
1187-
/* only a warning */
1188-
LOG_LOCSET(snode, NULL);
1189-
LOGWRN(snode->module->ctx, "Too few \"%s\" instances.", snode->name);
1190-
LOG_LOCBACK(1, 0);
1189+
if (min || max) {
1190+
/* set log path */
1191+
if (last_iter) {
1192+
/* standard data path */
1193+
LOG_LOCSET(NULL, last_iter);
11911194
} else {
1192-
LOG_LOCSET(snode, NULL);
1193-
LOGVAL_APPTAG(snode->module->ctx, "too-few-elements", LY_VCODE_NOMIN, snode->name);
1194-
LOG_LOCBACK(1, 0);
1195-
return LY_EVALID;
1195+
/* data path with last schema node name or only the schema node if !parent */
1196+
if (lyd_node_module(parent) != snode->module) {
1197+
r = asprintf(&log_path, "/%s:%s", snode->module->name, snode->name);
1198+
} else {
1199+
r = asprintf(&log_path, "/%s", snode->name);
1200+
}
1201+
if (r == -1) {
1202+
LOGMEM_RET(snode->module->ctx);
1203+
}
1204+
ly_log_location(NULL, parent, log_path, NULL);
1205+
free(log_path);
11961206
}
1197-
} else if (max) {
1198-
if (val_opts & LYD_VALIDATE_OPERATIONAL) {
1199-
/* only a warning */
1200-
LOG_LOCSET(NULL, iter);
1201-
LOGWRN(snode->module->ctx, "Too many \"%s\" instances.", snode->name);
1207+
1208+
if (min) {
1209+
if (val_opts & LYD_VALIDATE_OPERATIONAL) {
1210+
/* only a warning */
1211+
LOGWRN(snode->module->ctx, "Too few \"%s\" instances.", snode->name);
1212+
} else {
1213+
LOGVAL_APPTAG(snode->module->ctx, "too-few-elements", LY_VCODE_NOMIN, snode->name);
1214+
rc = LY_EVALID;
1215+
}
1216+
} else if (max) {
1217+
if (val_opts & LYD_VALIDATE_OPERATIONAL) {
1218+
/* only a warning */
1219+
LOGWRN(snode->module->ctx, "Too many \"%s\" instances.", snode->name);
1220+
} else {
1221+
LOGVAL_APPTAG(snode->module->ctx, "too-many-elements", LY_VCODE_NOMAX, snode->name);
1222+
rc = LY_EVALID;
1223+
}
1224+
}
1225+
1226+
/* revert log path */
1227+
if (last_iter) {
12021228
LOG_LOCBACK(0, 1);
12031229
} else {
1204-
LOG_LOCSET(NULL, iter);
1205-
LOGVAL_APPTAG(snode->module->ctx, "too-many-elements", LY_VCODE_NOMAX, snode->name);
1206-
LOG_LOCBACK(0, 1);
1207-
return LY_EVALID;
1230+
ly_log_location_revert(0, parent ? 1 : 0, 1, 0);
12081231
}
12091232
}
1210-
return LY_SUCCESS;
1233+
1234+
return rc;
12111235
}
12121236

12131237
/**

tests/utests/data/test_validation.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -277,12 +277,12 @@ test_minmax(void **state)
277277
CHECK_PARSE_LYD_PARAM("<l xmlns=\"urn:tests:c\">mate</l>"
278278
"<d xmlns=\"urn:tests:c\"/>",
279279
LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree);
280-
CHECK_LOG_CTX_APPTAG("Too few \"l\" instances.", "/c:choic/b/l", 0, "too-few-elements");
280+
CHECK_LOG_CTX_APPTAG("Too few \"l\" instances.", "/c:l[.='mate']", 0, "too-few-elements");
281281

282282
CHECK_PARSE_LYD_PARAM("<l xmlns=\"urn:tests:c\">val1</l>"
283283
"<l xmlns=\"urn:tests:c\">val2</l>",
284284
LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree);
285-
CHECK_LOG_CTX_APPTAG("Too few \"l\" instances.", "/c:choic/b/l", 0, "too-few-elements");
285+
CHECK_LOG_CTX_APPTAG("Too few \"l\" instances.", "/c:l[.='val2']", 0, "too-few-elements");
286286

287287
LYD_TREE_CREATE("<l xmlns=\"urn:tests:c\">val1</l>"
288288
"<l xmlns=\"urn:tests:c\">val2</l>"
@@ -1259,7 +1259,7 @@ test_multi_error(void **state)
12591259
CHECK_PARSE_LYD_PARAM(data, LYD_JSON, LYD_PARSE_ONLY, 0, LY_SUCCESS, tree);
12601260
assert_int_equal(LY_EVALID, lyd_validate_all(&tree, NULL, LYD_VALIDATE_PRESENT | LYD_VALIDATE_MULTI_ERROR, NULL));
12611261
lyd_free_tree(tree);
1262-
CHECK_LOG_CTX_APPTAG("Too few \"ll\" instances.", "/ii:cont/ll", 0, "too-few-elements");
1262+
CHECK_LOG_CTX_APPTAG("Too few \"ll\" instances.", "/ii:cont/ll[.='25']", 0, "too-few-elements");
12631263
CHECK_LOG_CTX_APPTAG("l leaf is not left", "/ii:cont/l3", 0, "not-left");
12641264
CHECK_LOG_CTX_APPTAG("Must condition \"../l = 'right'\" not satisfied.", "/ii:cont/l2", 0, "must-violation");
12651265
CHECK_LOG_CTX_APPTAG("Duplicate instance of \"l\".", "/ii:cont/l", 0, NULL);

tests/utests/node/list.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1051,7 +1051,7 @@ test_xml(void **state)
10511051
"</user>";
10521052
CHECK_PARSE_LYD_PARAM(data, LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree);
10531053
assert_null(tree);
1054-
CHECK_LOG_CTX("Too few \"user\" instances.", "/T2:user", 0);
1054+
CHECK_LOG_CTX("Too few \"user\" instances.", "/T2:user[uid='1']", 0);
10551055

10561056
data =
10571057
"<user xmlns=\"urn:tests:T2\">"
@@ -1347,7 +1347,7 @@ test_json(void **state)
13471347
"]}";
13481348
CHECK_PARSE_LYD_PARAM(data, LYD_JSON, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree);
13491349
assert_null(tree);
1350-
CHECK_LOG_CTX("Too few \"user\" instances.", "/T2:user", 0);
1350+
CHECK_LOG_CTX("Too few \"user\" instances.", "/T2:user[uid='4']", 0);
13511351

13521352
data =
13531353
"{\"T2:user\": ["

0 commit comments

Comments
 (0)