Trivia should have some validation checks for the trivia files #4571

Closed
opened 2020-10-31 03:09:33 +01:00 by Jackenmen · 5 comments
Jackenmen commented 2020-10-31 03:09:33 +01:00 (Migrated from github.com)

We should be checking the validity of the trivia list in [p]trivia command and [p]triviaset custom upload command. I don't know how much checks we need (could use schema verification, although that can result in poor errors sometimes), but our format looks like this (written in a form of a dictionary):

# AUTHOR and CONFIG are special keys that are optional
{
    "AUTHOR": "some string",  # type: str
    "CONFIG": {
        # all keys in here are optional
        "max_score": 123,  # type: int
        "timeout": 123.0,  # type: float
        "delay": 123.0,  # type: float
        "bot_plays": False,  # type: bool
        "reveal_answer": False,  # type: bool
        "payout_multiplier": 123.0,  # type: float
    },
    "question 1 (string)": [
        # list of answers; can be a combination of any of the types below
        "answer 1",  # type: str
        123,  # type: int (gets implicitly converted by Trivia)
        True,  # type: bool (gets implicitly converted by Trivia;
               # this one can be a source of issues,
               # but it probably needs to remain to avoid breakage,
               # at least until next minor version)
        1.69,  # type: float (gets implicitly converted by Trivia;
               # this one can be a source of issues,
               # but it probably needs to remain to avoid breakage,
               # at least until next minor version)
    ],
}

As an example, this is one of the errors that can currently occur when starting the trivia with an invalid format, that happens to be valid YAML.

Exception in command 'trivia'
Traceback (most recent call last):
  File "/Users/choure/redenv/lib/python3.8/site-packages/discord/ext/commands/core.py", line 85, in wrapped
    ret = await coro(*args, **kwargs)
  File "/Users/choure/redenv/lib/python3.8/site-packages/redbot/cogs/trivia/trivia.py", line 320, in trivia
    trivia_dict.update(dict_)
ValueError: dictionary update sequence element #0 has length 1; 2 is required
We should be checking the validity of the trivia list in `[p]trivia` command and `[p]triviaset custom upload` command. I don't know how much checks we need (could use schema verification, although that can result in poor errors sometimes), but our format looks like this (written in a form of a dictionary): ```py # AUTHOR and CONFIG are special keys that are optional { "AUTHOR": "some string", # type: str "CONFIG": { # all keys in here are optional "max_score": 123, # type: int "timeout": 123.0, # type: float "delay": 123.0, # type: float "bot_plays": False, # type: bool "reveal_answer": False, # type: bool "payout_multiplier": 123.0, # type: float }, "question 1 (string)": [ # list of answers; can be a combination of any of the types below "answer 1", # type: str 123, # type: int (gets implicitly converted by Trivia) True, # type: bool (gets implicitly converted by Trivia; # this one can be a source of issues, # but it probably needs to remain to avoid breakage, # at least until next minor version) 1.69, # type: float (gets implicitly converted by Trivia; # this one can be a source of issues, # but it probably needs to remain to avoid breakage, # at least until next minor version) ], } ``` As an example, this is one of the errors that can currently occur when starting the trivia with an invalid format, that happens to be valid YAML. ``` Exception in command 'trivia' Traceback (most recent call last): File "/Users/choure/redenv/lib/python3.8/site-packages/discord/ext/commands/core.py", line 85, in wrapped ret = await coro(*args, **kwargs) File "/Users/choure/redenv/lib/python3.8/site-packages/redbot/cogs/trivia/trivia.py", line 320, in trivia trivia_dict.update(dict_) ValueError: dictionary update sequence element #0 has length 1; 2 is required ```
leblancg commented 2020-11-09 04:25:19 +01:00 (Migrated from github.com)

Hi there, I am new to this project and I am interested in taking on this issue. My thinking is that JSON schema validation has a good chance of solving this. What are your concerns?

Hi there, I am new to this project and I am interested in taking on this issue. My thinking is that JSON schema validation has a good chance of solving this. What are your concerns?
Stonedestroyer commented 2020-11-09 05:07:58 +01:00 (Migrated from github.com)

Hi there, I am new to this project and I am interested in taking on this issue. My thinking is that JSON schema validation has a good chance of solving this. What are your concerns?

The issue with json is that yaml was picked cause of user accessibility to be able to create them without any programming experience. Changing to json would make the gap to creating trivia's much harder as we don't have a way to interactively create them.

> Hi there, I am new to this project and I am interested in taking on this issue. My thinking is that JSON schema validation has a good chance of solving this. What are your concerns? The issue with json is that yaml was picked cause of user accessibility to be able to create them without any programming experience. Changing to json would make the gap to creating trivia's much harder as we don't have a way to interactively create them.
leblancg commented 2020-11-09 06:29:27 +01:00 (Migrated from github.com)

My bad, I didn't mean changing from YAML to JSON I just meant that after parsing the YAML we could use a JSON schema validator such as this one (https://github.com/Julian/jsonschema) to verify that the YAML follows the desired format.

My bad, I didn't mean changing from YAML to JSON I just meant that after parsing the YAML we could use a JSON schema validator such as this one (https://github.com/Julian/jsonschema) to verify that the YAML follows the desired format.
leblancg commented 2020-12-05 17:38:04 +01:00 (Migrated from github.com)

I've found that some trivia lists (ex. hockey.yaml:1353) has a float listed as one of the answers. Should this be acceptable too?

I've found that some trivia lists (ex. hockey.yaml:1353) has a float listed as one of the answers. Should this be acceptable too?
Jackenmen commented 2020-12-05 17:43:11 +01:00 (Migrated from github.com)

@leblancg It's the same situation as with bool type - it should be allowed (at least for now) as disallowing it would be a breaking change since it may break existing trivia lists (such as the hockey one).

I updated the issue, good catch!

@leblancg It's the same situation as with `bool` type - it should be allowed (at least for now) as disallowing it would be a breaking change since it may break existing trivia lists (such as the hockey one). I updated the issue, good catch!
Sign in to join this conversation.
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#4571
No description provided.