flask-parameter-validation icon indicating copy to clipboard operation
flask-parameter-validation copied to clipboard

Validating against enum

Open peterkronenberg opened this issue 1 year ago • 5 comments

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

peterkronenberg avatar Jun 28 '24 18:06 peterkronenberg

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 avatar Jun 28 '24 19:06 smt5541

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

peterkronenberg avatar Jun 28 '24 19:06 peterkronenberg

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.

smt5541 avatar Jun 28 '24 19:06 smt5541

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

peterkronenberg avatar Jun 28 '24 19:06 peterkronenberg

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

smt5541 avatar Jun 28 '24 20:06 smt5541