[PR #17247] [MERGED] 17219 fix custom validator display if function #15036

Closed
opened 2025-12-30 00:19:35 +01:00 by adam · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/netbox-community/netbox/pull/17247
Author: @arthanson
Created: 8/22/2024
Status: Merged
Merged: 8/27/2024
Merged by: @jeremystretch

Base: developHead: 17219-custom-validator


📝 Commits (5)

  • 92e1f14 17219 fix custom validator display if function
  • 67e4b8c 17219 fix custom validator display if function
  • 6d84241 Merge branch 'develop' into 17219-custom-validator
  • 3c2af97 17219 use custom json encoder
  • ffbc8b4 Fix system config export

📊 Changes

3 files changed (+22 additions, -2 deletions)

View changed files

📝 netbox/core/views.py (+7 -1)
📝 netbox/templates/core/inc/config_data.html (+1 -1)
📝 netbox/utilities/json.py (+14 -0)

📄 Description

Fixes: #17219

Tested with function custom validator:

from extras.validators import CustomValidator

class UniqueSerial(CustomValidator):

    def validate(self, instance):
        from dcim.models import Device

        if instance.serial and Device.objects.exclude(pk=instance.pk).filter(serial=instance.serial).exists():
            self.fail("The Serial number already exists in NetBox", field='serial')

CUSTOM_VALIDATORS = {
    'dcim.device': (
        UniqueSerial(),
    )
}

System | NetBox 2024-08-22 11-39-30

and with dictionary based:

CUSTOM_VALIDATORS = {
    "dcim.device": [
        {
            "name": {
                "regex": "[a-z]+\d{3}"
            },
            "asset_tag": {
                "required": True
            }
        }
    ]
}

System | NetBox 2024-08-22 11-38-56


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/netbox-community/netbox/pull/17247 **Author:** [@arthanson](https://github.com/arthanson) **Created:** 8/22/2024 **Status:** ✅ Merged **Merged:** 8/27/2024 **Merged by:** [@jeremystretch](https://github.com/jeremystretch) **Base:** `develop` ← **Head:** `17219-custom-validator` --- ### 📝 Commits (5) - [`92e1f14`](https://github.com/netbox-community/netbox/commit/92e1f14d8166ab10b2485420d04514e3a13dd249) 17219 fix custom validator display if function - [`67e4b8c`](https://github.com/netbox-community/netbox/commit/67e4b8c8c6863315eab4531668507c469148ec4d) 17219 fix custom validator display if function - [`6d84241`](https://github.com/netbox-community/netbox/commit/6d84241db99d94823853a99503485f2eec837ff9) Merge branch 'develop' into 17219-custom-validator - [`3c2af97`](https://github.com/netbox-community/netbox/commit/3c2af9782379e19f6a70f55fc0b076348bb5f9eb) 17219 use custom json encoder - [`ffbc8b4`](https://github.com/netbox-community/netbox/commit/ffbc8b4b41afc63283128e98c679b54beee374ee) Fix system config export ### 📊 Changes **3 files changed** (+22 additions, -2 deletions) <details> <summary>View changed files</summary> 📝 `netbox/core/views.py` (+7 -1) 📝 `netbox/templates/core/inc/config_data.html` (+1 -1) 📝 `netbox/utilities/json.py` (+14 -0) </details> ### 📄 Description ### Fixes: #17219 Tested with function custom validator: ``` from extras.validators import CustomValidator class UniqueSerial(CustomValidator): def validate(self, instance): from dcim.models import Device if instance.serial and Device.objects.exclude(pk=instance.pk).filter(serial=instance.serial).exists(): self.fail("The Serial number already exists in NetBox", field='serial') CUSTOM_VALIDATORS = { 'dcim.device': ( UniqueSerial(), ) } ``` ![System | NetBox 2024-08-22 11-39-30](https://github.com/user-attachments/assets/f0b2ef46-b9ff-4273-9814-3f21d70eda35) and with dictionary based: ``` CUSTOM_VALIDATORS = { "dcim.device": [ { "name": { "regex": "[a-z]+\d{3}" }, "asset_tag": { "required": True } } ] } ``` ![System | NetBox 2024-08-22 11-38-56](https://github.com/user-attachments/assets/ec81253e-bfa4-4aca-b7cb-604e246eb5ab) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
adam added the pull-request label 2025-12-30 00:19:35 +01:00
adam closed this issue 2025-12-30 00:19:35 +01:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#15036