openwisp / openwisp/openwisp-controller

[feature] Improve custom command validation

Open
#1,039 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
Python
Stars
773
Forks
315
Avg merge
2d 16h
Merged PRs (30d)
14

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in AbstractCommand, focusing on _exec_command and the call to self._callable(**input); review how command execution currently handles exceptions and captures output. Trace the existing JSONSchema validation path and Django's ValidationError behavior. Done means callable validation errors are handled consistently without exposing raw exception details, with coverage for invalid custom-command input.

Written by the indexing model from the issue text.

Assessment

Tech stack
django, python
Domain
backend
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.