nautobot-app-data-validation-engine icon indicating copy to clipboard operation
nautobot-app-data-validation-engine copied to clipboard

Support validating the set of tags on an object

Open progala opened this issue 4 years ago • 5 comments

Environment

  • Python version: 3.8.5
  • Nautobot version: 1.0.1
  • nautobot-plugin-data-validation-engine: 1.0.0-dev

Steps to Reproduce

  1. Create regex rule for Object allowing tags, e.g. ipam.VLAN
  2. Specify "tags" as the Field
  3. Specify simple regex e.g. "data" as Regular expression

Example rule used for testing:

image

Expected Behavior

One of two outcomes should happend:

  1. When new VLAN is created and tag matching regex is added new object should be created without errors.
  2. When new VLAN is created and added tag does NOT match regex a validation error should be raised.

Observed Behavior

TypeError is raised: "expected string or bytes-like object"

image

Full traceback:

Internal Server Error: /ipam/vlans/add/
Traceback (most recent call last):
  File "/opt/nautobot/lib/python3.8/site-packages/django/core/handlers/exception.py", line 47, in inner
    response = get_response(request)
  File "/opt/nautobot/lib/python3.8/site-packages/django/core/handlers/base.py", line 181, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/opt/nautobot/lib/python3.8/site-packages/django/views/generic/base.py", line 70, in view
    return self.dispatch(request, *args, **kwargs)
  File "/opt/nautobot/lib/python3.8/site-packages/nautobot/core/views/generic.py", line 266, in dispatch
    return super().dispatch(request, *args, **kwargs)
  File "/opt/nautobot/lib/python3.8/site-packages/nautobot/utilities/views.py", line 94, in dispatch
    return super().dispatch(request, *args, **kwargs)
  File "/opt/nautobot/lib/python3.8/site-packages/django/views/generic/base.py", line 98, in dispatch
    return handler(request, *args, **kwargs)
  File "/opt/nautobot/lib/python3.8/site-packages/nautobot/core/views/generic.py", line 293, in post
    if form.is_valid():
  File "/opt/nautobot/lib/python3.8/site-packages/django/forms/forms.py", line 177, in is_valid
    return self.is_bound and not self.errors
  File "/opt/nautobot/lib/python3.8/site-packages/django/forms/forms.py", line 172, in errors
    self.full_clean()
  File "/opt/nautobot/lib/python3.8/site-packages/django/forms/forms.py", line 376, in full_clean
    self._post_clean()
  File "/opt/nautobot/lib/python3.8/site-packages/django/forms/models.py", line 405, in _post_clean
    self.instance.full_clean(exclude=exclude, validate_unique=False)
  File "/opt/nautobot/lib/python3.8/site-packages/django/db/models/base.py", line 1216, in full_clean
    self.clean()
  File "/opt/nautobot/lib/python3.8/site-packages/nautobot/extras/plugins/validators.py", line 34, in wrapper
    custom_validator(model_instance).clean()
  File "/opt/nautobot/lib/python3.8/site-packages/nautobot_data_validation_engine/custom_validators.py", line 35, in clean
    if not re.match(rule.regular_expression, getattr(obj, rule.field)):
  File "/usr/lib/python3.8/re.py", line 191, in match
    return _compile(pattern, flags).match(string)
TypeError: expected string or bytes-like object

progala avatar May 14 '21 16:05 progala

Thanks. I have addressed this immediate bug by not allowing manager-type fields to be used with regex rules (they wouldn't work this way anyway). The use case however, is very interesting and we can create a new rule type with allows for constraining the set of tags that can be added to an object.

lampwins avatar May 24 '21 07:05 lampwins

I played with the idea of a "tag list" validation rule, but the major roadblock is that tags are a many-to-many relationship and thus we can't access them on an object in the clean() method. I will brainstorm some ways we can deal with this.

lampwins avatar May 28 '21 06:05 lampwins

Is this issue still relevant or should it be closed?

DistantVoyager avatar May 24 '23 17:05 DistantVoyager

I think this should be reopened - to the best of my knowledge M2M validation isn't a solved problem yet in this App?

glennmatthews avatar May 24 '24 14:05 glennmatthews

Given that it's almost a year since the last update, I'm chiming in to echo my support for addressing this issue. I see it present with several different M2M scenarios which include tags and VLANs, and I suspect that as I ramp up further compliance for devices that I am fully automating configuration for that I could see this also with IP addresses on interfaces, and likely other areas in Nautobot or apps for Nautobot as there are many areas that use M2M type relationships.

The tricky thing right now is that one can write the compliance rule as enforce = True, and it doesn't enforce the first time but then you can get stuck as you can't subsequently update the object in question as the correction hasn't yet been applied and you are stuck removing the rule. Being fair, the example compliance rule I'm talking about likely shouldn't ever exist in production as enforce = True until this issue is addressed.

grahamjohnston avatar Apr 07 '25 17:04 grahamjohnston