Skip values that don't pass validation
See original GitHub issueQuestion
Please complete:
- OS: macOS 10.14.6
- Python version: 3.7.4
- Pydantic version: 1.0
I’d like to skip fields that don’t pass validation instead of throwing a ValidationError
. If a field is defined as an int
with a default value of None
, I’d like to assign a value of None
to that field if its value is not a valid int
.
Here’s how I’m accomplishing this now:
from pydantic import BaseModel, validator
class Thing(BaseModel):
id: str
total: int = None
@validator('total', pre=True)
def skip_invalid_ints(cls, value):
try:
return int(value)
except (TypeError, ValueError):
pass
def main():
thing1 = Thing.parse_obj(dict(id='foo'))
assert thing1.total is None
thing2 = Thing.parse_obj(dict(id='foo', total=5))
assert thing2.total == 5
thing3 = Thing.parse_obj(dict(id='foo', total='bar'))
assert thing3.total is None
thing4 = Thing.parse_obj(dict(id='foo', total=None))
assert thing4.total is None
if __name__ == '__main__':
main()
My question is, is there an easier way to do this? Some Config setting maybe?
Issue Analytics
- State:
- Created 4 years ago
- Reactions:2
- Comments:5 (4 by maintainers)
Top Results From Across the Web
Skip values that don't pass validation · Issue #932 - GitHub
I'd like to skip fields that don't pass validation instead of throwing a ValidationError . If a field is defined as an int...
Read more >Apply data validation to cells - Microsoft Support
Use data validation rules to control the type of data or the values that users enter into a cell. One example of validation...
Read more >Ignore Blanks in Data Validation Lists in Excel - YouTube
Excel File: https://www.teachexcel.com/excel-tutorial/ ignore -blanks-in-a-data- validation -list-in-excel_1302.html?nav=ytExcel Courses: ...
Read more >Ignore Blanks in a Data Validation List in Excel
Excel 365 Dynamic Array Formula to Remove Blanks. Remove blanks, sort, and return a unique list of values. Select All. =SORT(UNIQUE(FILTER ...
Read more >Excel Ignore Blanks in Data Validation List
Hi Kevin,. I'm not sure I follow you because I think that's what the formula above does i.e. generate a list of values...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
need to document using
validate_model
.Entirely possible, but you need to use
validate_model
directly.Here is an example.
This could do with being documented.