Pydantic: [Question] Improving enum error messages

Created on 12 Jan 2019  路  6Comments  路  Source: samuelcolvin/pydantic

Question

Hello!

I'd like to improve the default error messages for enums to include the allowed values as defined in an enum class instead the of default value is not a valid enumeration member message without having to override Configs.error_msg_templates[type_error.enum] on the base model.

Is there a good way to parse the enum values from the Enum class?

Example:

class AllowedAWSInstanceTypesEnum(str, Enum):
    t2_medium = "t2.medium"
    t2_micro = "t2.micro"

class AWSBase(BaseModel):
  instance_size: AllowedAWSInstanceTypesEnum

Example Json

{
    "instance_size": "m4.xlarge"
}

This should generate an error similar to

value m4.xlarge is not valid. Must be one of ["t2.medium", "t2.micro"]

Thank you for your help!

feature request help wanted question

Most helpful comment

having a fixed error value, like value is not a valid enumeration member allows you to use it as the key of a dictionary, do str comparison, etc.

This is what the type attribute of errors is for. I would advise against using the msg of errors in a lookup as they're like to change.

All 6 comments

Easy enough to iterate over an enum, but do we want to add all enum values to the default error message?

Maybe but it might lead to very long error messages, perhaps we could add "choices" to ctx then people can modify the message as they wish.

Also to have in mind: having a fixed error value, like value is not a valid enumeration member allows you to use it as the key of a dictionary, do str comparison, etc.

For example, you can check if the error value is exactly "value is not a valid enumeration member" to trigger a nice UI message explaining it to the final user in a more stylish way, for example translating the message to your supported languages.

If the error was "value m4.xlarge is not valid. Must be one of ..." you would have a hard time detecting the type of error to trigger the UI message, or to translate it, etc.

On the other side, you can get all the enum values from the JSON Schema two (I don't know if it applies to your use case), e.g.:

from enum import Enum
from pydantic import BaseModel

class AllowedAWSInstanceTypesEnum(str, Enum):
    t2_medium = "t2.medium"
    t2_micro = "t2.micro"

class AWSBase(BaseModel):
  instance_size: AllowedAWSInstanceTypesEnum

print(AWSBase.schema_json(indent=2))

prints:

{
  "title": "AWSBase",
  "type": "object",
  "properties": {
    "instance_size": {
      "title": "Instance_Size",
      "enum": [
        "t2.medium",
        "t2.micro"
      ],
      "type": "string"
    }
  },
  "required": [
    "instance_size"
  ]
}

having a fixed error value, like value is not a valid enumeration member allows you to use it as the key of a dictionary, do str comparison, etc.

This is what the type attribute of errors is for. I would advise against using the msg of errors in a lookup as they're like to change.

:facepalm: you're right.

Fixed I think.

Hello.

In Pydantic 1.6.1 the message to Enum ValidationError is in general: value is not a valid enumeration member; permitted: ....
I'd like to know, in most cases, what value led to that error (not only in Enum types). The ValidationError ends up overlapping the ValueError message that usually informs such value. Is there any form of adding this in the message template or context field?

Thank you!

Was this page helpful?
0 / 5 - 0 ratings