Skip to content

Commit 28f49d8

Browse files
committed
Properly truncate characters when splitting up strings in error repr
Fixes pydantic/pydantic#6448
1 parent 4527b80 commit 28f49d8

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

src/errors/validation_exception.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,11 +233,13 @@ impl ValidationError {
233233
macro_rules! truncate_input_value {
234234
($out:expr, $value:expr) => {
235235
if $value.len() > 50 {
236+
dbg!($value.floor_char_boundary(25));
237+
dbg!($value.ceil_char_boundary($value.len() - 24));
236238
write!(
237239
$out,
238240
", input_value={}...{}",
239-
&$value[0..25],
240-
&$value[$value.len() - 24..]
241+
&$value[0..$value.floor_char_boundary(25)],
242+
&$value[$value.ceil_char_boundary($value.len() - 24)..]
241243
)?;
242244
} else {
243245
write!($out, ", input_value={}", $value)?;

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#![feature(round_char_boundary)]
12
#![cfg_attr(has_no_coverage, feature(no_coverage))]
23

34
extern crate core;

tests/test_misc.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import copy
2+
import os
23
import pickle
34
import re
5+
from unittest.mock import patch
46

57
import pytest
68
from typing_extensions import get_args
79

8-
from pydantic_core import CoreSchema, CoreSchemaType, PydanticUndefined
10+
from pydantic_core import CoreSchema, CoreSchemaType, PydanticUndefined, core_schema
911
from pydantic_core._pydantic_core import SchemaError, SchemaValidator, ValidationError, __version__, build_profile
1012

1113

@@ -177,3 +179,20 @@ def test_undefined():
177179
assert undefined_deepcopy is PydanticUndefined
178180

179181
assert pickle.loads(pickle.dumps(PydanticUndefined)) is PydanticUndefined
182+
183+
184+
def test_unicode_error_input_repr() -> None:
185+
"""https://github.com/pydantic/pydantic/issues/6448"""
186+
187+
schema = core_schema.int_schema()
188+
189+
validator = SchemaValidator(schema)
190+
191+
danger_str = 'ÿ' * 1000
192+
with patch.dict(os.environ, {'PYDANTIC_ERRORS_OMIT_URL': '1'}):
193+
with pytest.raises(ValidationError) as exc_info:
194+
validator.validate_python(danger_str)
195+
assert (
196+
repr(exc_info.value)
197+
== "1 validation error for int\n Input should be a valid integer, unable to parse string as an integer [type=int_parsing, input_value='ÿÿÿÿÿÿÿÿÿÿÿÿ...ÿÿÿÿÿÿÿÿÿÿÿ', input_type=str]" # noqa: E501
198+
)

0 commit comments

Comments
 (0)