Enable accessing related object attributes via dotted path when defining custom validation rules #9374

Closed
opened 2025-12-29 20:49:06 +01:00 by adam · 2 comments
Owner

Originally created by @jeremystretch on GitHub (Mar 20, 2024).

Originally assigned to: @jeremystretch on GitHub.

NetBox version

v3.7.4

Feature type

New functionality

Proposed functionality

When defining custom validation rules as JSON, it is currently possible to access only direct attributes on an object. For example:

CUSTOM_VALIDATORS = {
    "dcim.site": [
        {
            "name": {
                "min_length": 5,
            }
        }
    ]
}

It is not possible to reference an attribute of a related object; this would require creating a CustomValidator subclass in Python with a custom validate() method as discussed here.

This issue proposes a mechanism for referencing attributes of related objects, identified by a dotted path. For example, the following would reference a the name of a site's assigned region:

CUSTOM_VALIDATORS = {
    "dcim.site": [
        {
            "region.name": {
                "eq": "North Carolina",
            }
        }
    ]
}

This could be achieved by employing Python's operator.attrgetter() utility.

Use case

This enhancement will allow users to reach "deeper" into an object when validating it without the need to write a custom subclass in Python.

Database changes

None

External dependencies

None

Originally created by @jeremystretch on GitHub (Mar 20, 2024). Originally assigned to: @jeremystretch on GitHub. ### NetBox version v3.7.4 ### Feature type New functionality ### Proposed functionality When defining [custom validation rules](https://docs.netbox.dev/en/stable/customization/custom-validation/#custom-validation-rules) as JSON, it is currently possible to access only direct attributes on an object. For example: ```python CUSTOM_VALIDATORS = { "dcim.site": [ { "name": { "min_length": 5, } } ] } ``` It is not possible to reference an attribute of a related object; this would require creating a CustomValidator subclass in Python with a custom `validate()` method as discussed [here](https://github.com/netbox-community/netbox/discussions/8950#discussioncomment-2425489). This issue proposes a mechanism for referencing attributes of related objects, identified by a dotted path. For example, the following would reference a the name of a site's assigned region: ```python CUSTOM_VALIDATORS = { "dcim.site": [ { "region.name": { "eq": "North Carolina", } } ] } ``` This could be achieved by employing Python's [`operator.attrgetter()`](https://docs.python.org/3/library/operator.html#operator.attrgetter) utility. ### Use case This enhancement will allow users to reach "deeper" into an object when validating it without the need to write a custom subclass in Python. ### Database changes None ### External dependencies None
adam added the status: acceptedtype: feature labels 2025-12-29 20:49:06 +01:00
adam closed this issue 2025-12-29 20:49:06 +01:00
Author
Owner

@jeremystretch commented on GitHub (Mar 20, 2024):

I may end up implementing this as a byproduct of #14279 as the basic functionality will be required there as well.

@jeremystretch commented on GitHub (Mar 20, 2024): I may end up implementing this as a byproduct of #14279 as the basic functionality will be required there as well.
Author
Owner

@opericgithub commented on GitHub (May 4, 2024):

Dear @jeremystretch, I'm sorry if I ask this question in the inappropriate topic, but I don't understand the documentation.

What should be the syntax of custom_validation rules, if I would like to have the following: the name of the site in the North Carolina region should have the minimum of 5 characters, but the name of the site in any other region should have the minimum of 7 chars?

@opericgithub commented on GitHub (May 4, 2024): Dear @jeremystretch, I'm sorry if I ask this question in the inappropriate topic, but I don't understand the documentation. What should be the syntax of custom_validation rules, if I would like to have the following: the name of the site in the North Carolina region should have the minimum of 5 characters, but the name of the site in any other region should have the minimum of 7 chars?
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#9374