prefect
prefect copied to clipboard
Pydantic Field validators not working when editing Block through the UI
First check
- [X] I added a descriptive title to this issue.
- [X] I used the GitHub search to find a similar issue and didn't find it.
- [X] I refreshed the page and this issue still occurred.
- [X] I checked if this issue was specific to the browser I was using by testing with a different browser.
Bug summary
I am not sure how much of a problem this is, but it was unexpected. I was creating a Block class and wanted to make a few attributes constant values - yes, logically they'd be defined constants or something but for the user's edification, I wanted them displayed in the UI when editing a block. So I tried using Pydantic's Field:
bucket: str = Field("cdn.area51.onl", const=True)
In the UI, when creating a new block, what results is an input line where the hint (not an initial value) is 'cdn.area51.onl' and you can edit the value, the input line will accept any value, and the field is also labeled as (Optional) by Prefect. The use of the string as the hint for the input line was very unexpected.
If Pydantic's Fields are not supported like this, then it's not a problem but it would be nice to have some ability to use Pydantic features to easily add validation to Block editing w/o having to write a specific method. Hopefully I've overlooked how to write validation code for Blocks in the docs....
Similarly, for another field, the regex validator expression is ignored. And it would be nice if there was some way to specify the field can not be blank or None, but can't figure that out. Regex would work I suppose
Also, I tried specifying the type of another field as Literal['string'] - a drop down of None and "string" showed up in the UI. The None was unexpected but it makes some sense. I then set the initial value of this field to "string" and then the drop down had only "string" as a possible value. All good... but the field in the UI was still labeled as (Optional). A drop down with only 1 value is hardly optional.
(Optional) seems to be added for every field unless there is no initial value specified in the code- i.e. just the type of the field is specified (e.g. attribute: str). But that doesn't make sense - None could still be a possible value, but to get the UI to add "(Optional)" I have to use "attribute: str = None".
Reproduction
class TestBlock(Block):
onedrop: Literal["only value"] = "only value"
twodrop: Literal["only value"]
constantstr: str = Field("not a hint", const=True)
regexpr: str = Field("thehint", regex="^thehint/(more data|data)/")
notoptional: str
optionalvalue: str = None
then register the block: prefect block register --file test.py
Error
Browers
- [ ] Chrome
- [X] Firefox
- [X] Safari
- [ ] Edge
Prefect version
Version: 2.6.3
API version: 0.8.2
Python version: 3.8.14
Git commit: 9e7da96e
Built: Tue, Oct 18, 2022 1:55 PM
OS/Arch: darwin/x86_64
Profile: dryrun
Server type: ephemeral
Server:
Database: sqlite
SQLite version: 3.39.3
### Additional context
Firefox Developer version 106
_No response_
Update: I tried adding a @validator method to my block (given it is based on Pydantic's BaseModel) just to be thorough and prevent users from entering bad values / typos.... and it seems Pydantic's validation functionality is not supported at all.
Pydantic's main purpose is not to validate input data (as their docs state) but the functionality is there and it would make sense for Prefect to utilize it - how else can validation of a Block's input data one can enter through the UI happen?
I have read through the docs and the code, hopefully I am overlooking something
This issue is stale because it has been open 30 days with no activity. To keep this issue open remove stale label or comment.
This issue was closed because it has been stale for 14 days with no activity. If this issue is important or you have more to add feel free to re-open it.