Skip to content

Commit 3e3af68

Browse files
smark28samuel-gauthier
authored andcommitted
context: add support for LYD_VALIDATE_MULTI_ERROR
Thanks to the LYD_VALIDATE_MULTI_ERROR, the validation does not stop at the first error, but generates all the detected errors. Add a dedicated test. Fixes: CESNET#79 Signed-off-by: Sergei Markov <semarkov@cisco.com> Signed-off-by: Samuel Gauthier <samuel.gauthier@6wind.com>
1 parent b62fb0d commit 3e3af68

File tree

4 files changed

+37
-1
lines changed

4 files changed

+37
-1
lines changed

cffi/cdefs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,7 @@ LY_ERR lyd_print_all(struct ly_out *, const struct lyd_node *, LYD_FORMAT, uint3
314314
#define LYD_VALIDATE_NO_STATE ...
315315
#define LYD_VALIDATE_PRESENT ...
316316
#define LYD_VALIDATE_OPTS_MASK ...
317+
#define LYD_VALIDATE_MULTI_ERROR ...
317318

318319
LY_ERR lyd_parse_data_mem(const struct ly_ctx *, const char *, LYD_FORMAT, uint32_t, uint32_t, struct lyd_node **);
319320

libyang/context.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,7 @@ def parse_data(
339339
ordered: bool = False,
340340
strict: bool = False,
341341
validate_present: bool = False,
342+
validate_multi_error: bool = False,
342343
) -> Optional[DNode]:
343344
if self.cdata is None:
344345
raise RuntimeError("context already destroyed")
@@ -351,7 +352,9 @@ def parse_data(
351352
strict=strict,
352353
)
353354
validation_flgs = validation_flags(
354-
no_state=no_state, validate_present=validate_present
355+
no_state=no_state,
356+
validate_present=validate_present,
357+
validate_multi_error=validate_multi_error,
355358
)
356359
fmt = data_format(fmt)
357360
encode = True
@@ -403,6 +406,7 @@ def parse_data_mem(
403406
ordered: bool = False,
404407
strict: bool = False,
405408
validate_present: bool = False,
409+
validate_multi_error: bool = False,
406410
) -> Optional[DNode]:
407411
return self.parse_data(
408412
fmt,
@@ -416,6 +420,7 @@ def parse_data_mem(
416420
ordered=ordered,
417421
strict=strict,
418422
validate_present=validate_present,
423+
validate_multi_error=validate_multi_error,
419424
)
420425

421426
def parse_data_file(
@@ -430,6 +435,7 @@ def parse_data_file(
430435
ordered: bool = False,
431436
strict: bool = False,
432437
validate_present: bool = False,
438+
validate_multi_error: bool = False,
433439
) -> Optional[DNode]:
434440
return self.parse_data(
435441
fmt,
@@ -443,6 +449,7 @@ def parse_data_file(
443449
ordered=ordered,
444450
strict=strict,
445451
validate_present=validate_present,
452+
validate_multi_error=validate_multi_error,
446453
)
447454

448455
def __iter__(self) -> Iterator[Module]:

libyang/data.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,12 +171,15 @@ def data_type(dtype):
171171
def validation_flags(
172172
no_state: bool = False,
173173
validate_present: bool = False,
174+
validate_multi_error: bool = False,
174175
) -> int:
175176
flags = 0
176177
if no_state:
177178
flags |= lib.LYD_VALIDATE_NO_STATE
178179
if validate_present:
179180
flags |= lib.LYD_VALIDATE_PRESENT
181+
if validate_multi_error:
182+
flags |= lib.LYD_VALIDATE_MULTI_ERROR
180183
return flags
181184

182185

tests/test_data.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,31 @@ def test_data_parse_config_xml(self):
256256
finally:
257257
dnode.free()
258258

259+
XML_CONFIG_MULTI_ERROR = """<conf xmlns="urn:yang:yolo:system">
260+
<hostname>foo</hostname>
261+
<url>
262+
<proto>https</proto>
263+
<path>/CESNET/libyang-python</path>
264+
<enabled>abcd</enabled>
265+
</url>
266+
<number>2000</number>
267+
</conf>
268+
"""
269+
270+
def test_data_parse_config_xml_multi_error(self):
271+
with self.assertRaises(Exception) as cm:
272+
self.ctx.parse_data_mem(
273+
self.XML_CONFIG_MULTI_ERROR,
274+
"xml",
275+
validate_present=True,
276+
validate_multi_error=True,
277+
)
278+
self.assertEqual(
279+
str(cm.exception),
280+
'failed to parse data tree: Invalid boolean value "abcd".: '
281+
'List instance is missing its key "host".',
282+
)
283+
259284
XML_STATE = """<state xmlns="urn:yang:yolo:system">
260285
<hostname>foo</hostname>
261286
<url>

0 commit comments

Comments
 (0)