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.

Skip values that don't pass validation

See original GitHub issue

Question

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:open
  • Created 4 years ago
  • Reactions:2
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

4reactions
samuelcolvincommented, Feb 13, 2021

need to document using validate_model.

3reactions
samuelcolvincommented, Oct 24, 2019

Entirely possible, but you need to use validate_model directly.

Here is an example.

This could do with being documented.

Read more comments on GitHub >

github_iconTop 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 >

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