Skip to content

Validating against enum #44

New issue

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

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

Already on GitHub? # to your account

Closed
peterkronenberg opened this issue Jun 28, 2024 · 5 comments · Fixed by #46
Closed

Validating against enum #44

peterkronenberg opened this issue Jun 28, 2024 · 5 comments · Fixed by #46
Assignees
Labels
enhancement New feature or request

Comments

@peterkronenberg
Copy link

Is there a way to validate an argument to ensure that it has only a list of specific values?

For example

func(
          suit = Json(hearts | clubs | spades | diamonds)
):

Or maybe validate against an existing Python enum

@smt5541
Copy link
Collaborator

smt5541 commented Jun 28, 2024

Currently, the best way to do this is the func parameter passed to the decorator, but I can see type hints being used for ensuring a value is in the enum to make that smoother.

@smt5541 smt5541 added the enhancement New feature or request label Jun 28, 2024
@peterkronenberg
Copy link
Author

Where are the type hints documented? I don't recall seeing anything about arguments to the decorator

@smt5541
Copy link
Collaborator

smt5541 commented Jun 28, 2024

Sorry, I misspoke - there are parameters that can be passed to the decorator, but those are to customize error handling. I meant to say the func parameter of Parameter's subclasses.

Currently in the midst of fleshing out and updating documentation in #45 , so that may help as well.

@peterkronenberg
Copy link
Author

Ah, I see. An example of usage would be great. I'll keep on eye on the docs. Looks good so far.

@smt5541
Copy link
Collaborator

smt5541 commented Jun 28, 2024

An existing example would be in the unit tests:

In addition to what's shown in that example, you can return a custom error message by returning a tuple of (valid: bool, error_message: str) instead of a single bool

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants