Skip to content

Commit 92e694f

Browse files
committed
support partial JSON parsing
1 parent 45ef7d5 commit 92e694f

File tree

4 files changed

+31
-5
lines changed

4 files changed

+31
-5
lines changed

Cargo.lock

Lines changed: 1 addition & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ base64 = "0.22.1"
4646
num-bigint = "0.4.6"
4747
python3-dll-a = "0.2.10"
4848
uuid = "1.11.0"
49-
jiter = { version = "0.6", features = ["python"] }
49+
#jiter = { version = "0.6", features = ["python"] }
50+
jiter = { git = "https://github.com/pydantic/jiter", branch = "JsonValue-partial", features = ["python"] }
5051
hex = "0.4.3"
5152

5253
[lib]

src/validators/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -383,8 +383,8 @@ impl SchemaValidator {
383383
self_instance: Option<&Bound<'_, PyAny>>,
384384
allow_partial: bool,
385385
) -> ValResult<PyObject> {
386-
let json_value =
387-
jiter::JsonValue::parse(json_data, true).map_err(|e| json::map_json_err(input, e, json_data))?;
386+
let json_value = jiter::JsonValue::parse_with_config(json_data, true, allow_partial)
387+
.map_err(|e| json::map_json_err(input, e, json_data))?;
388388
self._validate(
389389
py,
390390
&json_value,

tests/validators/test_allow_partial.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ def test_list():
3434
v.validate_python([[1, 2], 'wrong', [3, 4]])
3535
with pytest.raises(ValidationError, match='Input should be a valid tuple'):
3636
v.validate_python([[1, 2], 'wrong', 'wrong'])
37+
assert v.validate_json(b'[[1, 2], [3, 4]]', allow_partial=True) == [(1, 2), (3, 4)]
38+
assert v.validate_json(b'[[1, 2], [3,', allow_partial=True) == [(1, 2)]
3739

3840

3941
def test_list_partial_nested():
@@ -162,6 +164,23 @@ def test_partial_typed_dict():
162164
]
163165
)
164166

167+
# validate strings
168+
assert v.validate_strings({'a': '11', 'b': '22'}) == snapshot({'a': 11, 'b': 22})
169+
with pytest.raises(ValidationError, match='Input should be greater than 10'):
170+
v.validate_strings({'a': '11', 'b': '2'})
171+
assert v.validate_strings({'a': '11', 'b': '2'}, allow_partial=True) == snapshot({'a': 11})
172+
173+
assert v.validate_json(b'{"b": "12", "a": 11, "c": 13}', allow_partial=True) == IsStrictDict(a=11, b=12, c=13)
174+
assert v.validate_json(b'{"b": "12", "a": 11, "c": 13', allow_partial=True) == IsStrictDict(a=11, b=12, c=13)
175+
assert v.validate_json(b'{"a": 11, "b": "12", "c": 1', allow_partial=True) == IsStrictDict(a=11, b=12)
176+
assert v.validate_json(b'{"a": 11, "b": "12", "c":', allow_partial=True) == IsStrictDict(a=11, b=12)
177+
assert v.validate_json(b'{"a": 11, "b": "12", "c"', allow_partial=True) == IsStrictDict(a=11, b=12)
178+
assert v.validate_json(b'{"a": 11, "b": "12", "c', allow_partial=True) == IsStrictDict(a=11, b=12)
179+
assert v.validate_json(b'{"a": 11, "b": "12", "', allow_partial=True) == IsStrictDict(a=11, b=12)
180+
assert v.validate_json(b'{"a": 11, "b": "12", ', allow_partial=True) == IsStrictDict(a=11, b=12)
181+
assert v.validate_json(b'{"a": 11, "b": "12",', allow_partial=True) == IsStrictDict(a=11, b=12)
182+
assert v.validate_json(b'{"a": 11, "b": "12"', allow_partial=True) == IsStrictDict(a=11, b=12)
183+
165184

166185
def test_non_partial_typed_dict():
167186
v = SchemaValidator(
@@ -212,3 +231,10 @@ def test_double_nested():
212231
assert v.validate_python({'a': 11, 'b': [{'a': 1, 'b': 20}, {'a': 3, 'b': 40}]}, allow_partial=True) == snapshot(
213232
{'a': 11}
214233
)
234+
json = b'{"a": 11, "b": [{"a": 10, "b": 20}, {"a": 30, "b": 40}]}'
235+
assert v.validate_json(json, allow_partial=True) == snapshot(
236+
{'a': 11, 'b': [{'a': 10, 'b': 20}, {'a': 30, 'b': 40}]}
237+
)
238+
for i in range(1, len(json)):
239+
value = v.validate_json(json[:i], allow_partial=True)
240+
assert isinstance(value, dict)

0 commit comments

Comments
 (0)