Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enable LYD_VALIDATE_MULTI_ERROR flag #79

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cffi/cdefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,7 @@ LY_ERR lyd_print_all(struct ly_out *, const struct lyd_node *, LYD_FORMAT, uint3
#define LYD_VALIDATE_NO_STATE ...
#define LYD_VALIDATE_PRESENT ...
#define LYD_VALIDATE_OPTS_MASK ...
#define LYD_VALIDATE_MULTI_ERROR ...

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

Expand Down
9 changes: 8 additions & 1 deletion libyang/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,7 @@ def parse_data(
ordered: bool = False,
strict: bool = False,
validate_present: bool = False,
validate_multi_error: bool = False,
) -> Optional[DNode]:
if self.cdata is None:
raise RuntimeError("context already destroyed")
Expand All @@ -338,7 +339,9 @@ def parse_data(
strict=strict,
)
validation_flgs = validation_flags(
no_state=no_state, validate_present=validate_present
no_state=no_state,
validate_present=validate_present,
validate_multi_error=validate_multi_error,
)
fmt = data_format(fmt)
encode = True
Expand Down Expand Up @@ -390,6 +393,7 @@ def parse_data_mem(
ordered: bool = False,
strict: bool = False,
validate_present: bool = False,
validate_multi_error: bool = False,
) -> Optional[DNode]:
return self.parse_data(
fmt,
Expand All @@ -403,6 +407,7 @@ def parse_data_mem(
ordered=ordered,
strict=strict,
validate_present=validate_present,
validate_multi_error=validate_multi_error
)

def parse_data_file(
Expand All @@ -417,6 +422,7 @@ def parse_data_file(
ordered: bool = False,
strict: bool = False,
validate_present: bool = False,
validate_multi_error: bool = False,
) -> Optional[DNode]:
return self.parse_data(
fmt,
Expand All @@ -430,6 +436,7 @@ def parse_data_file(
ordered=ordered,
strict=strict,
validate_present=validate_present,
validate_multi_error=validate_multi_error,
)

def __iter__(self) -> Iterator[Module]:
Expand Down
3 changes: 3 additions & 0 deletions libyang/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,12 +171,15 @@ def data_type(dtype):
def validation_flags(
no_state: bool = False,
validate_present: bool = False,
validate_multi_error: bool = False,
) -> int:
flags = 0
if no_state:
flags |= lib.LYD_VALIDATE_NO_STATE
if validate_present:
flags |= lib.LYD_VALIDATE_PRESENT
if validate_multi_error:
flags |= lib.LYD_VALIDATE_MULTI_ERROR
return flags


Expand Down
22 changes: 22 additions & 0 deletions tests/test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -860,3 +860,25 @@ def test_add_defaults(self):
node = dnode.find_path("/yolo-system:conf/speed")
self.assertIsInstance(node, DLeaf)
self.assertEqual(node.value(), 4321)

XML_CONFIG_MULTI_ERROR = """<conf xmlns="urn:yang:yolo:system">
<hostname>foo</hostname>
<url>
<proto>https</proto>
<path>/CESNET/libyang-python</path>
<enabled>abcd</enabled>
</url>
<number>2000</number>
</conf>
"""

def test_data_parse_config_xml_multi_error(self):
with self.assertRaises(Exception) as cm:
self.ctx.parse_data_mem(self.XML_CONFIG_MULTI_ERROR, "xml",
validate_present=True,
validate_multi_error=True)
self.assertEqual(
str(cm.exception),
'failed to parse data tree: Invalid boolean value "abcd".: '
'List instance is missing its key "host".'
)
Loading