kolibri icon indicating copy to clipboard operation
kolibri copied to clipboard

Option specs: validate string values with a regular expression

Open bjesterbot opened this issue 1 month ago • 1 comments

❌ This issue is not open for contribution. Visit Contributing guidelines to learn about the contributing process and how to find suitable issues.

Target branch: develop

Current behavior

Core and plugins declare options in an option_spec dict. Each entry names a type. When Kolibri reads options.ini and the environment variables, it checks each value against that type. If a value is invalid, Kolibri logs which option failed and stops with Aborting: Could not process options config.

The types come from a fixed map in _get_validator(). A plugin cannot add a type to it. An entry can pass extra arguments to its type through validator_args, for example min for an integer. No type checks a string against a pattern.

Thus a plugin that needs a value in a fixed format must check the value itself, after Kolibri loads the options. Kolibri Data Portal (KDP) is an example. In learningequality/kolibri-data-portal#1209, KDP declares two options in its kolibri_options module:

  • COOKIEBOT_ID must be a UUID.
  • POSTHOG_PROJECT_API_KEY must match phc_[A-Za-z0-9]+.

KDP checks both in its Django AppConfig.ready(). It cannot check them in its plugin class, because Kolibri creates the plugin while it builds the option spec. This has two effects:

  • An invalid value passes the checks in Kolibri. The failure comes later, from Django setup, as an ImproperlyConfigured traceback. It does not use the error output that Kolibri gives for other invalid options.
  • update_options_file() checks a new value against the spec before it writes options.ini. It cannot reject a value that only the plugin knows is invalid.

Desired behavior

An option spec entry can declare a regular expression that a string value must fully match. Kolibri applies it everywhere that it already checks option values. An invalid value then fails in the same way as a value of the wrong type.

The feature should deliver these capabilities:

  • A plugin can declare the pattern in its own option_spec, with no code outside the spec.
  • A value from options.ini or from an environment variable that does not fully match fails the check. Kolibri logs the error and stops, as it does for other invalid options.
  • update_options_file() rejects a value that does not fully match.
  • A value that matches loads unchanged. Kolibri does not strip or change it.
  • Entries that declare no pattern behave as they do now.

Acceptance criteria

  • [ ] An option spec entry in core or in a plugin can declare a pattern for a string value.
  • [ ] If a value does not fully match the pattern, startup stops with the error output that Kolibri uses for other invalid options. The output names the option, and the environment variable when the value came from one.
  • [ ] A partial match fails. For example, the pattern phc_[A-Za-z0-9]+ rejects phc_abc def and xphc_abc.
  • [ ] An empty value is checked like any other value. A spec that allows an empty value uses a pattern that matches the empty string.
  • [ ] update_options_file() raises ValueError for a value that does not match, and it does not write the file.
  • [ ] Unit tests in kolibri/utils/tests/test_options.py cover these cases:
    • A matching value.
    • A value from options.ini that does not match.
    • A value from an environment variable that does not match.
    • An empty value.

Out of scope

  • Custom check functions that plugins register. A pattern covers the formats that KDP needs, and it keeps the option spec declarative.
  • Patterns for list types, such as each item of a path_list.

Value add

  • A plugin can state the format of a value in one place: its option spec.
  • An invalid value fails early, with the same error output as other invalid options.
  • Every path that checks option values against the spec also applies the pattern. This includes update_options_file(), and the runtime options that #15629 proposes to make editable from the device settings page.

Possible tradeoffs

  • Kolibri prints the invalid value in its error output for other types. An option with a pattern can hold a secret, such as an API key. The error for a pattern can name the option and the pattern without the value.
  • The pattern lives in the option spec as a string. An invalid pattern must fail when Kolibri builds the spec, not when it checks a value. Otherwise a typo in a plugin hides until a value is set.

Technical pointers

  • kolibri/utils/options.py: _get_validator(), get_configspec() with validator_args, read_options_file(), and update_options_file().
  • kolibri/plugins/utils/options.py: merges each plugin's option_spec into the core spec.
  • Related: #15629 adds runtime options that superadmins edit on the device settings page.

AI usage

I asked Claude Code to draft this issue after we hit this limit while moving KDP options to the Kolibri options mechanism. Claude read the options code on develop at bffac37, confirmed each link and line range against that commit, and searched for existing issues. It found none that ask for pattern checks on options.

bjesterbot avatar Nov 02 '26 20:11 bjesterbot

🔍 Thanks for opening this issue! @rtibblesbot will check it against the code and comment with its findings.

What happens next:

  • The community-issue-review label stays on until the review comment is posted.
  • The comment identifies the files, lines, or commits examined so you can follow the reasoning and respond if you disagree.
  • If the check confirms the issue, @rtibblesbot may also correct the title and description. GitHub preserves the edit history, so the original text remains available.
  • @rtibblesbot's comments are LLM-generated, so please evaluate them critically.

A maintainer decides what happens next. This check does not close the issue.

learning-equality-bot avatar Nov 02 '26 20:11 learning-equality-bot