Expose a plugin interface that allows for custom model validation to be performed when the model's clean() method is called. This interface will look similar to the plugin template content injection interface in which plugin developers specify the model they wish to act on. In this case, the class will implement only a single method validate():
class DeviceModelValidator(ModelValidator):
model = 'dcim.device'
def validate(self, obj):
if obj.name == 'cat':
raise ValidationError({
'name': 'No cats allowed on the network!'
})
As you can see, this method can either raise VailidationError or simply return None if the object passes validation. In the code path, these custom validators will be run at the end of the model's standard clean() method.
As users adopt NetBox as a core component in automation solutions, data validity is key. NetBox reports can be used to highlight data consistency and integrity issues, but users must still fix these problems after the fact. By enforcing business logic rules when an object is created or updated, the chances of producing bad data are reduced.
The desire to implement custom validation has come up several times in the community before, but we have not really had a clean enough way to implement this until the plugins framework was born.
See: this comment, https://github.com/netbox-community/netbox/issues/3974, https://github.com/netbox-community/netbox/issues/3163, etc.
None
The plugins framework introduced in 2.8.
I'm going to punt on this for the initial implementation as there's already _a lot_ going on. Let's put a pin in this until we figure out how we're going to handle feature requests for the plugins framework after the v2.8 release.
@jeremystretch can we pick this back up given the relatively light load of plugins related requests? I am happy to take this on.
Blocked by #4640
To further provide some use cases, here are a few I have noted with customers recently
Enforcing Business logic
Many users "expect" the IPAM component to operate in a certain way. While it is clear that the base implementation cannot support that logic in order to support many more use cases, within the custom model validation you can add these capabilities.
Most helpful comment
To further provide some use cases, here are a few I have noted with customers recently
Enforcing Business logic
Many users "expect" the IPAM component to operate in a certain way. While it is clear that the base implementation cannot support that logic in order to support many more use cases, within the custom model validation you can add these capabilities.