question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Enforce input validation on Parameter Tasks and add descriptions

See original GitHub issue

Current behavior

Parameters are pretty simple entities that look for a key and take whatever value is presented at that key, to then forward off to the Tasks that consume it.

This makes troubleshooting invalid values passed into Parameters tricky, as the failure will happen at the downstream Task(s), rather than the Parameter. Which is essentially wasting compute time, because if the Parameter was bad, there’s no chance the downstream tasks can succeed.

But to enforce input validation, we’d need to also have some basic documentation/description of what the provided Parameter is.

Proposed behavior

starlette has a slick wrapper for the way it handles environment configuration, where you can specify a cast type. The code for this is pretty small.

Taking that a step further, I’ve gotten spoiled by the way FastAPI leverages the pydantic library by using python type annotations to handle input validation of input parameters to endpoints, and think this would be a great feature to extend into the way Prefect takes in Parameters.

Where you’d be able to optionally specify an object type for the Parameter, and if set, when the Parameter Task is ran, it would perform basic datatype validation on what was passed in. If someone passed a datatype other than what that Parameter was expecting, it’d fail at that step before trying to run any downstream Tasks that consume the Parameter.

Example

During Parameter initialization, it can evaluate the value passed into the Parameter and fail at that point if it is not the expected input type.

with Flow("example") as flow:
    # basic types
    int_param = Parameter("int_param", default=100, cast=int, description="how many to pull")
    str_param = Parameter("str_param", default="string", cast=str, description="a string to search")
    
    # class types
    from enum import Enum
    
    
    class StrictValues(str, Enum):
        VALUE = 'one'
        EXAMPLE = 'two'
    
    enum_param = Parameter("enum_param", default=StrictValues.VALUE, cast=StrictValues, description="which value do you want?")
    ...

Or we could go all in with overloading pydantic.BaseModel to have a single validators (but this would break backwards compatibility):

class MyCustomParameters(pydantic.BaseModel):
    int_param: int = pydantic.Field(
        default=100,
        description='how many to pull'
    )
    str_param: str = pydantic.Field(
        default="string",
        description='how many to pull'
    )
    enum_param: StrictValues = pydantic.Field(
        default=StrictValues.VALUE,
        description='which value do you want?'
    )

with Flow(
    name="example",
    parameters=MyCustomParameters
) as flow:
    ...

pydantic raises a specific exception with details on what input validation failed, which can aid in troubleshooting bad input parameters to the Parameter Tasks.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:11
  • Comments:8 (6 by maintainers)

github_iconTop GitHub Comments

4reactions
cicdwcommented, Mar 8, 2022

Hi @Julian-Brendel, good news: we have already implemented Parameter type validation in what will eventually become Prefect 2.0

0reactions
cicdwcommented, Aug 30, 2022

Complete with 2.0.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Validating Input | Web Accessibility Initiative (WAI) - W3C
Validation should aim to be as accommodating as possible of different forms of input for particular data types. For example, telephone numbers are...
Read more >
Constraint validation - HTML: HyperText Markup Language
By setting values on validation-related attributes, allowing basic constraints to be described in a simple way, without the need for JavaScript. Semantic input...
Read more >
CWE-20: Improper Input Validation (4.9) - MITRE
Input validation is a frequently-used technique for checking potentially dangerous inputs in order to ensure that the inputs are safe for ...
Read more >
Input Validation - OWASP Cheat Sheet Series
Input validation can be implemented using any programming technique that allows effective enforcement of syntactic and semantic correctness, for example:.
Read more >
Model validation in ASP.NET Core MVC | Microsoft Learn
Client-side validation prevents submission until the form is valid. The Submit button runs JavaScript that either submits the form or displays ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found