Skip to content

Implement _value_to_dict validation attribute#3434

Open
ipspace wants to merge 4 commits into
devfrom
value-to-dict
Open

Implement _value_to_dict validation attribute#3434
ipspace wants to merge 4 commits into
devfrom
value-to-dict

Conversation

@ipspace
Copy link
Copy Markdown
Owner

@ipspace ipspace commented Jun 3, 2026

The _value_to_dict validation attribute transforms a non-dict value into a dictionary using the value of the _value_to_dict key as the template dictionary. You can use it whenever you have a dict attribute that can accept non-dict values.

Sample use case included in the PR: BGP communities

The _value_to_dict validation attribute transforms a non-dict value
into a dictionary using the value of the _value_to_dict key as the
template dictionary. You can use it whenever you have a dict attribute
that can accept non-dict values.

Sample use case included in the PR: BGP communities
@ipspace
Copy link
Copy Markdown
Owner Author

ipspace commented Jun 3, 2026

@jbemmel -- I think this is exactly what you need for #3429

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR introduces a new validation schema capability (_value_to_dict) that allows non-dict values to be transformed into dicts using a template mapping, and applies it to simplify BGP community handling by moving conversion logic from Python into the data model.

Changes:

  • Added _value_to_dict support in the core validation engine and documented the new schema key.
  • Updated BGP module attribute schema to use _value_to_dict for bgp.community, removing the bespoke transformation/validation code from bgp.py.
  • Added/updated coverage and error-output fixtures to validate the new behavior (including BGP communities and a custom datatype example).

Reviewed changes

Copilot reviewed 4 out of 7 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
tests/errors/bgp-community.log Updates expected validation errors for BGP community inputs under the new schema-driven handling.
tests/coverage/input/value-as-dict.yml New coverage topology exercising _value_to_dict (custom type + BGP community).
tests/coverage/expected/value-as-dict.yml New expected transformed output for the _value_to_dict coverage topology.
netsim/modules/bgp.yml Switches bgp.community to schema-driven transformation using _value_to_dict.
netsim/modules/bgp.py Removes old imperative bgp.community transformation/validation logic now handled by the validator/schema.
netsim/data/validate.py Implements _value_to_dict transformation support in the validation engine.
docs/dev/validation.md Documents _value_to_dict with examples (including BGP community).

Comment thread netsim/data/validate.py
Comment thread netsim/data/validate.py Outdated
Comment thread docs/dev/validation.md Outdated
ipspace and others added 3 commits June 3, 2026 10:32
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
@ipspace ipspace requested a review from jbemmel June 3, 2026 08:38
Comment thread netsim/data/validate.py
if not isinstance(data,dict) and '_value_to_dict' in data_type and parent is not None:
try:
parent[key] = transform_value_to_dict(data,data_type._value_to_dict)
except Exception as ex:
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When does transform_value_to_dict throw?

Comment thread docs/dev/validation.md
attributes:
node:
role:
name:
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could add _required for name

Copy link
Copy Markdown
Collaborator

@jbemmel jbemmel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Elegant solution

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants