'Use custom error classes with constrained types in Pydantic

The documentation specifies that: you can also define your own error classes, which can specify a custom error code, message template, and context.

In the example from the link above, a validator is defined to validate an attribute and the custom error NotABarError is raised from the validation method, which then raises a Pydantic ValidationError.

Similarly, is there a way to raise custom exceptions with constrained types? The message could be the same as the default one from pydantic.error_wrappers.ValidationError, but raised from a custom error class, rather than the Pydantic's ValidationError?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source