Friday 5 November 2021

Pydantic: Error handling

Pydnatic throws ‘ValidationError’ whenever it see an issue while validating the data.

 

validation_error_1.py

from pydantic import BaseModel, ValidationError, conint, constr

class Employee(BaseModel):
    id: int
    name: constr(min_length=3, max_length=10)
    age: conint(gt=18)

try:
    emp1 = Employee(id = 1, name = 'Pt', age = 8)
except ValidationError as e:
    print(e)

 

Output

2 validation errors for Employee
name
  ensure this value has at least 3 characters (type=value_error.any_str.min_length; limit_value=3)
age
  ensure this value is greater than 18 (type=value_error.number.not_gt; limit_value=18)

As you see the output, Pydantic will return the list of errors found in the input data.

 

Different ways to extract the information from ValidaitonError object

Method

Description

e.errors()

method will return list of errors found in the input data.

e.json()

method will return a JSON representation of errors.

str(e)

method will return a human readable representation of the errors.

 

validation_error_2.py

from pydantic import BaseModel, ValidationError, conint, constr

class Employee(BaseModel):
    id: int
    name: constr(min_length=3, max_length=10)
    age: conint(gt=18)

try:
    emp1 = Employee(id = 1, name = 'Pt', age = 8)
except ValidationError as e:
    print(e.errors())
    print('\n\n\n')
    print(e.json())
    print('\n\n\n')
    print(str(e))


Output

[{'loc': ('name',), 'msg': 'ensure this value has at least 3 characters', 'type': 'value_error.any_str.min_length', 'ctx': {'limit_value': 3}}, {'loc': ('age',), 'msg': 'ensure this value is greater than 18', 'type': 'value_error.number.not_gt', 'ctx': {'limit_value': 18}}]




[
  {
    "loc": [
      "name"
    ],
    "msg": "ensure this value has at least 3 characters",
    "type": "value_error.any_str.min_length",
    "ctx": {
      "limit_value": 3
    }
  },
  {
    "loc": [
      "age"
    ],
    "msg": "ensure this value is greater than 18",
    "type": "value_error.number.not_gt",
    "ctx": {
      "limit_value": 18
    }
  }
]




2 validation errors for Employee
name
  ensure this value has at least 3 characters (type=value_error.any_str.min_length; limit_value=3)
age
  ensure this value is greater than 18 (type=value_error.number.not_gt; limit_value=18)


{

    "loc": [

      "age"

    ],

    "msg": "ensure this value is greater than 18",

    "type": "value_error.number.not_gt",

    "ctx": {

      "limit_value": 18

    }

  }

As you see above snippet, each error object contain properties loc, msg, type and ctx. Below table summarizes the properties.

 

Property

Description

loc

Specifies the error's location as a list. The first item in the list will be the field where the error occurred,

msg

Human readable error message

type

Computer readable identifier for the error type

ctx

an optional object which contains values required to render the error message.




 

  

Previous                                                    Next                                                    Home

No comments:

Post a Comment