Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@samcorky
Copy link
Contributor

@samcorky samcorky commented Sep 5, 2025

The config that is used by the Python app is now validated and rejected upfront if there are any errors.
This is all ENV VARS starting with SMIB_ apart from SMIB_PROXY_ and SMIB_COMPOSE_.

Errors are formatted nicely with useful information.

Closes #322

- Introduce user-friendly error formatting for validation failures using `_format_validation_errors`.
- Add robust validation logic for Slack settings with `@field_validator` for token prefixes.
- Ensure all settings are initialized at import time, failing early if issues are encountered.
- Add handling for `error_type` to conditionally display provided values only when applicable.
- Ensure robust formatting of validation error messages.
@samcorky samcorky linked an issue Sep 5, 2025 that may be closed by this pull request
@sjefferson99 sjefferson99 requested a review from Copilot September 5, 2025 21:38
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR adds comprehensive configuration validation at application startup to reject invalid environment variables upfront with user-friendly error messages. The validation covers all SMIB_ environment variables except SMIB_PROXY_ and SMIB_COMPOSE_ prefixes.

  • Adds field validators for Slack bot and app tokens to ensure proper formatting
  • Implements centralized validation error collection and formatting in the config module
  • Introduces fail-fast behavior that exits the application immediately on configuration errors

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.

File Description
src/smib/config/slack.py Adds token prefix validation for Slack bot and app tokens
src/smib/config/init.py Implements configuration validation framework with error collection and user-friendly formatting

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@samcorky
Copy link
Contributor Author

samcorky commented Sep 5, 2025

Changes done

@sjefferson99
Copy link
Member

@sam57719 How do I test? I changed the name of the var and and the variable value in .env and rebuilt docker container and it built fine, what sort of errors does this catch and where does it show?

@samcorky
Copy link
Contributor Author

samcorky commented Sep 6, 2025

This will validate the values of valid env vars. Any unknown env vars are ignored. The only 2 required ones are the 2 slack tokens.

So for example, change the port to a alpha value, or set the log level to something invalid. Or omit one of the slack tokens.

It doesn't validate things like the space open announce channel against slack.

It's more the type of value thats validated.

@sjefferson99
Copy link
Member

smib@smib-sj:~/git/smib $ docker logs -n 100 -f smib
He's dead, Jim 🖖
Validation error for LoggingSettings:
• log_level:
Error: Input should be 'DEBUG', 'INFO', 'WARNING' or 'ERROR'
Provided Value: INSANE
Setting Description: Logging level for the application (DEBUG, INFO, WARNING, ERROR)
Setting Environment Variable: SMIB_LOGGING_LOG_LEVEL
Setting Type: Literal
Setting Default: INFO

Copy link
Member

@sjefferson99 sjefferson99 left a comment

Choose a reason for hiding this comment

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

Container builds, spot check env vars are correctly logged as incorrect.

@samcorky samcorky merged commit 88eb886 into master Sep 7, 2025
@samcorky samcorky deleted the 322-validate-config-on-initialisation branch September 7, 2025 09:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Validate config on initialisation

3 participants