openwisp-controller icon indicating copy to clipboard operation
openwisp-controller copied to clipboard

[feature] Improve custom command validation

Open cbeaujoin-stellar opened this issue 7 months ago • 1 comments

Currently, when we define a new custom command, we can validate the input fields using the JSONSchema.

IE code of one of the base command:

'password_regex': {
                            'type': 'string',
                            'minLength': 6,
                            'maxLength': 30,
                            'pattern': '[\S]',
                        }

Would it possible possible to leverage the validation directly in the python code of the callable method ?

A proposal would be to be able to raise django.core.exceptions.ValidationError, the main benefit would be to be able to use a very large type of libs available in Python in order to check input fields. IE:

        try:
            ip = ipaddress.IPv4Address(ip_address)
        except ValueError:
            raise ValidationError(
                f"Invalid IP address format: {ip_address}. Must be a valid IPv4 address (e.g. 192.168.1.1)")

I made some test and it is currently working, but has ValidationError is not catch at the _exec_command level the output is a bit dirty. In fact in AbstractCommand the code lets any exception propagate from command = self._callable(**input) without specific type handling, which means the raw exception details will be captured in the output. This provides full visibility into what exactly went wrong during command execution rather than trying to normalize or sanitize the error output.

cbeaujoin-stellar avatar May 15 '25 09:05 cbeaujoin-stellar

JSON schema is pretty powerful nowadays and allows you to do a lot of validation, including validating ip addresses.

If you don't like that and prefer to do it in a different way you should be able to monkey patch the clean method and implement your own custom validation there.

nemesifier avatar May 15 '25 17:05 nemesifier