flask-parameter-validation
flask-parameter-validation copied to clipboard
Validating against enum
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
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.
Where are the type hints documented? I don't recall seeing anything about arguments to the decorator
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.
Ah, I see. An example of usage would be great. I'll keep on eye on the docs. Looks good so far.
An existing example would be in the unit tests: https://github.com/Ge0rg3/flask-parameter-validation/blob/7b994141a05a17455939fd67e6f59fe337687cea/flask_parameter_validation/test/testing_blueprints/str_blueprint.py#L74
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