Skip to content

Instantly share code, notes, and snippets.

@lampwins
Last active March 25, 2021 14:20
Show Gist options
  • Select an option

  • Save lampwins/089f2de121f1cfd5a38145c3753a5401 to your computer and use it in GitHub Desktop.

Select an option

Save lampwins/089f2de121f1cfd5a38145c3753a5401 to your computer and use it in GitHub Desktop.
"""custom validator to enforce naming convention for site."""
from nautobot.extras.plugins import PluginCustomValidator
class SiteCustomValidator(PluginCustomValidator):
"""
Custom validator to enforce naming convention for site.
Example of a PluginCustomValidator that checks if the name of a Site object.
contains an invalid characters and if so, raises a ValidationError.
"""
model = "dcim.site"
def clean(self):
"""Apply custom model validation logic."""
obj = self.context["object"]
invalid_caracters = ["!", "#", ":", ";"]
for char in invalid_caracters:
if char in obj.name:
self.validation_error({"name": f"Site name can't contain '{char}'"})
custom_validators = [SiteCustomValidator]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment