Fix Permissions YAML validation error selection #6816

Closed
samirhvbr wants to merge 1 commit from fix/permissions-yaml-error into V3/develop
samirhvbr commented 2026-10-22 19:54:16 +02:00 (Migrated from github.com)

Description of the changes

Fixes #3185. For example, importing COMMAND: {ping: {defalt: false}} currently reports that top-level keys must be COG or COMMAND, although those keys are correct. Non-boolean rule values produce the same misleading response.

The schema's empty-dict alternatives repeat outer validation errors after the specific nested error. Use the last message from SchemaError.code, which removes duplicate messages while preserving order, so the response identifies the failing level. The validation schema and accepted ACL formats are unchanged.

Tests exercise the actual YAML upload/response path for invalid command/cog names, rule keys, numeric/null/string rule values, and invalid top-level keys. They also check that an invalid upload does not clear existing rules. The 14 nested-error cases fail on 004a160b before this change; all pass afterward.

Have the changes in this PR been tested?

Yes

  • tox -p auto -e py38,py39,py310,py311,style,docs passes on macOS / Apple Silicon (327 passed on Python 3.8-3.10 and 328 on Python 3.11, with 7 skipped in each environment; style and all three documentation builders pass).
  • python -m black --check . and git diff --check pass.
  • No live Discord session or local PostgreSQL backend was exercised.

AI assistance: prepared with Codex on behalf of @samirhvbr; reproductions and the checks above were executed locally.

### Description of the changes Fixes #3185. For example, importing `COMMAND: {ping: {defalt: false}}` currently reports that top-level keys must be `COG` or `COMMAND`, although those keys are correct. Non-boolean rule values produce the same misleading response. The schema's empty-dict alternatives repeat outer validation errors after the specific nested error. Use the last message from `SchemaError.code`, which removes duplicate messages while preserving order, so the response identifies the failing level. The validation schema and accepted ACL formats are unchanged. Tests exercise the actual YAML upload/response path for invalid command/cog names, rule keys, numeric/null/string rule values, and invalid top-level keys. They also check that an invalid upload does not clear existing rules. The 14 nested-error cases fail on `004a160b` before this change; all pass afterward. ### Have the changes in this PR been tested? Yes - `tox -p auto -e py38,py39,py310,py311,style,docs` passes on macOS / Apple Silicon (327 passed on Python 3.8-3.10 and 328 on Python 3.11, with 7 skipped in each environment; style and all three documentation builders pass). - `python -m black --check .` and `git diff --check` pass. - No live Discord session or local PostgreSQL backend was exercised. AI assistance: prepared with Codex on behalf of @samirhvbr; reproductions and the checks above were executed locally.
Jackenmen commented 2026-11-09 21:51:09 +01:00 (Migrated from github.com)

This approach feels a bit hacky - I'm not sure that relying on last line of e.code to be the most specific error is ideal. We could print all errors to avoid it but most of the errors aren't particularly relevant to what the user did wrong so that's probably not a good approach either.

The way tests are implemented is pretty hacky as well - overreliance on mocks is not good and it is hardly necessary here - you could easily split out the error formatting into a separate function so it can be tested independently. Use of LLMs is fine, if you know what you're doing but I'm not convinced that you do.

This approach feels a bit hacky - I'm not sure that relying on last line of `e.code` to be the most specific error is ideal. We could print all errors to avoid it but most of the errors aren't particularly relevant to what the user did wrong so that's probably not a good approach either. The way tests are implemented is pretty hacky as well - overreliance on mocks is not good and it is hardly necessary here - you could easily split out the error formatting into a separate function so it can be tested independently. Use of LLMs is fine, if you know what you're doing but I'm not convinced that you do.

Pull request closed

Sign in to join this conversation.
No reviewers
No labels
Automated PR
Blocked
Blocked By: Damage Control
Blocked By: Dependency
Blocked By: Other PR
Breaking Change
Category: CI
Category: Cogs - Admin
Category: Cogs - Alias
Category: Cogs - Audio
Category: Cogs - Bank
Category: Cogs - Cleanup
Category: Cogs - CustomCommands
Category: Cogs - Dev
Category: Cogs - Downloader
Category: Cogs - Economy
Category: Cogs - Filter
Category: Cogs - General
Category: Cogs - Image
Category: Cogs - Mod
Category: Cogs - Modlog
Category: Cogs - Mutes
Category: Cogs - Permissions
Category: Cogs - Reports
Category: Cogs - Streams
Category: Cogs - Trivia
Category: Cogs - Trivia - Lists
Category: Cogs - Warnings
Category: Core - API - App Commands Package
Category: Core - API - Audio
Category: Core - API - Bank
Category: Core - API - Commands Package
Category: Core - API - Config
Category: Core - API - Other
Category: Core - API - Utils Package
Category: Core - Bot Class
Category: Core - Bot Commands
Category: Core - Command-line Interfaces
Category: Core - Downloader
Category: Core - Help
Category: Core - i18n
Category: Core - Modlog
Category: Core - Other Internals
Category: Core - RPC/ZMQ
Category: Docker
Category: Docs - Changelogs
Category: Docs - For Developers
Category: Docs - Install Guides
Category: Docs - Other
Category: Docs - User Guides
Category: Meta
Category: RPC/ZMQ methods
Category: Vendored Packages
Changelog Entry: Added
Changelog Entry: Pending
Changelog Entry: Skipped
Closed: Duplicate
Closed: External
Closed: Invalid
Closed: User Error
Closed: Won't Fix
Complex Issue
Docs-only
Feature Branch
Good First Issue
Help Wanted
High Priority
Needs Backport To stable-docs
No Activity
No Repro
QA: Bypassed
QA: Changes Requested
QA: Passed
Release Blocker
Status: Accepted
Status: Frozen
Status: In Progress
Status: Needs Discussion
Status: Needs Info
Status: Needs Triage
Status: PRs Welcome
Type: Bug
Type: Dependency Update
Type: Deprecation
Type: Enhancement
Type: Feature
Type: Informational
Type: Merge
Type: Optimisation
Type: Question
Type: Removal
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
Mrcoolgamer11/Red-DiscordBot!6816
No description provided.