MCPcopy
hub / github.com/pydantic/pydantic / test_on_validate_json_on_error

Function test_on_validate_json_on_error

tests/test_plugins.py:79–123  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

77
78
79def test_on_validate_json_on_error() -> None:
80 class CustomOnValidateJson:
81 def on_enter(
82 self,
83 input: str | bytes | bytearray,
84 *,
85 strict: bool | None = None,
86 extra: ExtraValues | None = None,
87 context: Any | None = None,
88 self_instance: Any | None = None,
89 by_alias: bool | None = None,
90 by_name: bool | None = None,
91 ) -> None:
92 assert input == '{"a": "potato"}'
93 assert strict is None
94 assert extra is None
95 assert context is None
96 assert self_instance is None
97
98 def on_error(self, error: ValidationError) -> None:
99 assert error.title == 'Model'
100 assert error.errors(include_url=False) == [
101 {
102 'input': 'potato',
103 'loc': ('a',),
104 'msg': 'Input should be a valid integer, unable to parse string as an integer',
105 'type': 'int_parsing',
106 },
107 ]
108
109 class Plugin(PydanticPluginProtocol):
110 def new_schema_validator(self, schema, schema_type, schema_type_path, schema_kind, config, plugin_settings):
111 assert config == {'title': 'Model'}
112 assert plugin_settings == {'observe': 'all'}
113 return None, CustomOnValidateJson(), None
114
115 plugin = Plugin()
116 with install_plugin(plugin):
117
118 class Model(BaseModel, plugin_settings={'observe': 'all'}):
119 a: int
120
121 assert Model.model_validate({'a': 1}) == Model(a=1)
122 with contextlib.suppress(ValidationError):
123 Model.model_validate_json('{"a": "potato"}')
124
125
126def test_on_validate_python_on_success() -> None:

Callers

nothing calls this directly

Calls 5

install_pluginFunction · 0.85
model_validateMethod · 0.80
model_validate_jsonMethod · 0.80
PluginClass · 0.70
ModelClass · 0.70

Tested by

no test coverage detected