Skip to content

17219 fix custom validator display if function #17247

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

Merged
merged 5 commits into from
Aug 27, 2024

Conversation

arthanson
Copy link
Collaborator

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

@arthanson arthanson changed the title DRAFT: 17219 fix custom validator display if function 17219 fix custom validator display if function Aug 22, 2024
@arthanson arthanson marked this pull request as ready for review August 22, 2024 18:53
Copy link
Member

@jeremystretch jeremystretch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fails when configuring both class- and dictionary-based validators:

CUSTOM_VALIDATORS = {
    'dcim.device': [
        {
            "name": {
                "min_length": 5
            }
        },
        UniqueSerial(),
    ]
}

@jeremystretch
Copy link
Member

It might be cleaner to just write a custom JSON encoder (like CustomFieldJSONEncoder) to serialize Python classes to their string names. We can then do the serialization inside the view (rather than the template) and pass the rendered JSON directly as context data for the template.

@jeremystretch jeremystretch merged commit d23b937 into develop Aug 27, 2024
6 checks passed
@jeremystretch jeremystretch deleted the 17219-custom-validator branch August 27, 2024 18:19
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 27, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

System page - Server Error if Custom Validation Logic is used
2 participants