From cc3b95bdb072883d417632dfee8026951f769729 Mon Sep 17 00:00:00 2001 From: jeremystretch Date: Tue, 2 May 2023 09:28:15 -0400 Subject: [PATCH] Fixes #12400: Validate default values for object and multi-object custom fields --- docs/release-notes/version-3.5.md | 1 + netbox/extras/models/customfields.py | 13 +++++++++++++ 2 files changed, 14 insertions(+) diff --git a/docs/release-notes/version-3.5.md b/docs/release-notes/version-3.5.md index f0ad05a9a64..d6e7bdb5309 100644 --- a/docs/release-notes/version-3.5.md +++ b/docs/release-notes/version-3.5.md @@ -8,6 +8,7 @@ * [#12384](https://github.com/netbox-community/netbox/issues/12384) - Add a three-second timeout for RSS reader widget * [#12395](https://github.com/netbox-community/netbox/issues/12395) - Fix "create & add another" action for objects with custom fields * [#12396](https://github.com/netbox-community/netbox/issues/12396) - Provider account should not be a required field in REST API serializer +* [#12400](https://github.com/netbox-community/netbox/issues/12400) - Validate default values for object and multi-object custom fields * [#12401](https://github.com/netbox-community/netbox/issues/12401) - Support the creation of front ports without a pre-populated device ID * [#12405](https://github.com/netbox-community/netbox/issues/12405) - Fix filtering for VLAN groups displayed under site view * [#12412](https://github.com/netbox-community/netbox/issues/12412) - Device/VM interface MAC addresses can be nullified via REST API diff --git a/netbox/extras/models/customfields.py b/netbox/extras/models/customfields.py index 18430300f6c..439d15edc56 100644 --- a/netbox/extras/models/customfields.py +++ b/netbox/extras/models/customfields.py @@ -606,5 +606,18 @@ def validate(self, value): f"Invalid choice(s) ({', '.join(value)}). Available choices are: {', '.join(self.choices)}" ) + # Validate selected object + elif self.type == CustomFieldTypeChoices.TYPE_OBJECT: + if type(value) is not int: + raise ValidationError(f"Value must be an object ID, not {type(value).__name__}") + + # Validate selected objects + elif self.type == CustomFieldTypeChoices.TYPE_MULTIOBJECT: + if type(value) is not list: + raise ValidationError(f"Value must be a list of object IDs, not {type(value).__name__}") + for id in value: + if type(id) is not int: + raise ValidationError(f"Found invalid object ID: {id}") + elif self.required: raise ValidationError("Required field cannot be empty.")