Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
jeremystretch opened this issue Mar 20, 2024 · 2 comments
Assignees
Labels
status: accepted This issue has been accepted for implementation type: feature Introduction of new functionality to the application
Milestone

Comments

@jeremystretch
Copy link
Member

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

@jeremystretch jeremystretch added the type: feature Introduction of new functionality to the application label Mar 20, 2024
@jeremystretch
Copy link
Member Author

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

@jeremystretch jeremystretch added the status: accepted This issue has been accepted for implementation label Mar 20, 2024
@jeremystretch jeremystretch added this to the v4.0 milestone Mar 20, 2024
@jeremystretch jeremystretch self-assigned this Mar 22, 2024
jeremystretch added a commit that referenced this issue Mar 22, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
…ttribute via dotted path
jeremystretch added a commit that referenced this issue Mar 25, 2024
@opericgithub
Copy link

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?

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 2, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
status: accepted This issue has been accepted for implementation type: feature Introduction of new functionality to the application
Projects
None yet
Development

No branches or pull requests

2 participants