nautobot / nautobot-app-data-validation-engine

Data validation rules engine app to easily codify corporate data standards
https://docs.nautobot.com/projects/data-validation/en/latest/
Other
18 stars 6 forks source link

Support validating the set of tags on an object #1

Open progala opened 3 years ago

progala commented 3 years ago

Environment

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
lampwins commented 3 years ago

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 commented 3 years ago

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.

DistantVoyager commented 1 year ago

Is this issue still relevant or should it be closed?

glennmatthews commented 5 months ago

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