Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch suggestion #525

Open
jgardiner68 opened this issue Aug 24, 2024 · 2 comments
Open

Switch suggestion #525

jgardiner68 opened this issue Aug 24, 2024 · 2 comments

Comments

@jgardiner68
Copy link

ChatGPT suggested that this was valid -- it looks wonderful:

from voluptuous import Schema, Required, All, Length, Switch, Invalid

# Define the schema with Switch
schema = Schema({
    'issuetype': str,
    Switch('issuetype', {
        'Feature Request': Schema({
            Required('feature_details'): All(str, Length(min=10)),
        }),
        'Bug': Schema({
            Required('bug_report'): All(str, Length(min=5)),
        }),
    }),
})
@alecthomas
Copy link
Owner

alecthomas commented Aug 24, 2024

It's also invalid Python. Classic ChatGPT.

@ds-cbo
Copy link
Contributor

ds-cbo commented Aug 27, 2024

I haven't tested this, but I think you're looking for something along the lines of

schema = Schema(Any(
    {
        'issuetype': Equal("Feature Request"),
        'feature_details": ...,
    },
    {
        'issuetype': Equal("Bug"),
        'bug_report': ...,
    },
))

which imho also looks a lot clearer than what ChatGPT suggested

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

No branches or pull requests

3 participants