Skip to content

Commit 6478dba

Browse files
committed
support nested JSON validators
1 parent a0936cd commit 6478dba

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

src/validators/json.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
use jiter::FloatMode;
21
use pyo3::intern;
32
use pyo3::prelude::*;
43
use pyo3::types::PyDict;
54

6-
use jiter::{JsonValue, PartialMode, PythonParse};
5+
use jiter::{FloatMode, JsonValue, PartialMode, PythonParse};
76

87
use crate::errors::{ErrorType, ErrorTypeDefaults, ValError, ValLineError, ValResult};
98
use crate::input::{EitherBytes, Input, InputType, ValidationMatch};
@@ -71,7 +70,11 @@ impl Validator for JsonValidator {
7170
let parse_builder = PythonParse {
7271
allow_inf_nan: true,
7372
cache_mode: state.cache_str(),
74-
partial_mode: PartialMode::Off,
73+
partial_mode: if state.allow_partial {
74+
PartialMode::TrailingStrings
75+
} else {
76+
PartialMode::Off
77+
},
7578
catch_duplicate_keys: false,
7679
float_mode: FloatMode::Float,
7780
};

tests/validators/test_allow_partial.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,3 +299,15 @@ def test_nullable():
299299
assert v.validate_python(None, allow_partial=True) is None
300300
assert v.validate_python(['ab', 'cd'], allow_partial=True) == ['ab', 'cd']
301301
assert v.validate_python(['ab', 'c'], allow_partial=True) == ['ab']
302+
303+
304+
@pytest.mark.parametrize(
305+
'json_nested_type', [None, core_schema.dict_schema(core_schema.str_schema(), core_schema.int_schema())]
306+
)
307+
def test_json(json_nested_type):
308+
v = SchemaValidator(core_schema.list_schema(core_schema.json_schema(json_nested_type)))
309+
310+
assert v.validate_python(['{"a": 1}', '{"b": 2}']) == snapshot([{'a': 1}, {'b': 2}])
311+
assert v.validate_python(['{"a": 1}', '{"b": 2}'], allow_partial=True) == snapshot([{'a': 1}, {'b': 2}])
312+
assert v.validate_python(['{"a": 1}', 'xxx'], allow_partial=True) == snapshot([{'a': 1}])
313+
assert v.validate_python(['{"a": 1}', '{"b": 2'], allow_partial=True) == snapshot([{'a': 1}, {'b': 2}])

0 commit comments

Comments
 (0)