Last active
March 25, 2021 14:20
-
-
Save lampwins/089f2de121f1cfd5a38145c3753a5401 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """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