Provide greater flexibility when configuring custom validation rules #5553

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

Originally created by @jeremystretch on GitHub (Oct 22, 2021).

Originally assigned to: @jeremystretch on GitHub.

NetBox version

v3.0.8

Feature type

Change to existing functionality

Proposed functionality

Custom validation was introduced in NetBox v3.0. To declare custom validation rules, an administrator must define the CUSTOM_VALIDATORS configuration parameter, mapping models to their validator classes like this:

CUSTOM_VALIDATORS = {
    'dcim.site': (
        CustomValidator({
            'name': {
                'min_length': 5,
                'max_length': 30,
            }
        }),
    )
}

This FR proposes support for two alternative methods of declaring custom validators (while preserving support for the approach above). First, it should be possible to pass a dictionary of rules directly in place of a CustomValidator instance. For example:

CUSTOM_VALIDATORS = {
    'dcim.site': (
        {
            'name': {
                'min_length': 5,
                'max_length': 30,
            }
        },
    )
}

The dictionary of rules will be passed when instantiating a CustomValidator instance. The second proposed alternative is to allow referencing by path a CustomValidator class defined elsewhere. For example:

CUSTOM_VALIDATORS = {
    'dcim.site': (
        'some_plugin.validators.MyCustomValidator',
    )
}

Use case

Both approaches make it more convenient for users to write portable and repeatable custom validation rules, as they eliminate the need to handle any Python code directly.

Database changes

No response

External dependencies

No response

Originally created by @jeremystretch on GitHub (Oct 22, 2021). Originally assigned to: @jeremystretch on GitHub. ### NetBox version v3.0.8 ### Feature type Change to existing functionality ### Proposed functionality [Custom validation](https://netbox.readthedocs.io/en/stable/customization/custom-validation/) was introduced in NetBox v3.0. To declare custom validation rules, an administrator must define the `CUSTOM_VALIDATORS` configuration parameter, mapping models to their validator classes like this: ``` CUSTOM_VALIDATORS = { 'dcim.site': ( CustomValidator({ 'name': { 'min_length': 5, 'max_length': 30, } }), ) } ``` This FR proposes support for two alternative methods of declaring custom validators (while preserving support for the approach above). First, it should be possible to pass a dictionary of rules directly in place of a CustomValidator instance. For example: ``` CUSTOM_VALIDATORS = { 'dcim.site': ( { 'name': { 'min_length': 5, 'max_length': 30, } }, ) } ``` The dictionary of rules will be passed when instantiating a CustomValidator instance. The second proposed alternative is to allow referencing by path a CustomValidator class defined elsewhere. For example: ``` CUSTOM_VALIDATORS = { 'dcim.site': ( 'some_plugin.validators.MyCustomValidator', ) } ``` ### Use case Both approaches make it more convenient for users to write portable and repeatable custom validation rules, as they eliminate the need to handle any Python code directly. ### Database changes _No response_ ### External dependencies _No response_
adam added the status: acceptedtype: feature labels 2025-12-29 19:29:21 +01:00
adam closed this issue 2025-12-29 19:29:21 +01:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#5553