@@ -124,6 +124,11 @@ def config(self) -> CoreConfig | None:
124124 """The CoreConfig that applies to this validation."""
125125 ...
126126
127+ @property
128+ def mode (self ) -> Literal ['python' , 'json' ]:
129+ """The type of input data we are currently validating"""
130+ ...
131+
127132
128133class FieldValidationInfo (ValidationInfo , Protocol ):
129134 """
@@ -1050,8 +1055,6 @@ class IsInstanceSchema(TypedDict, total=False):
10501055 type : Required [Literal ['is-instance' ]]
10511056 cls : Required [Any ]
10521057 cls_repr : str
1053- json_types : Set [JsonType ]
1054- json_function : Callable [[Any ], Any ]
10551058 ref : str
10561059 metadata : Any
10571060 serialization : SerSchema
@@ -1060,8 +1063,6 @@ class IsInstanceSchema(TypedDict, total=False):
10601063def is_instance_schema (
10611064 cls : Any ,
10621065 * ,
1063- json_types : Set [JsonType ] | None = None ,
1064- json_function : Callable [[Any ], Any ] | None = None ,
10651066 cls_repr : str | None = None ,
10661067 ref : str | None = None ,
10671068 metadata : Any = None ,
@@ -1083,23 +1084,13 @@ class A:
10831084
10841085 Args:
10851086 cls: The value must be an instance of this class
1086- json_types: When parsing JSON directly, the value must be one of these json types
1087- json_function: When parsing JSON directly, If provided, the JSON value is passed to this
1088- function and the return value used as the output value
10891087 cls_repr: If provided this string is used in the validator name instead of `repr(cls)`
10901088 ref: optional unique identifier of the schema, used to reference the schema in other places
10911089 metadata: Any other information you want to include with the schema, not used by pydantic-core
10921090 serialization: Custom serialization schema
10931091 """
10941092 return dict_not_none (
1095- type = 'is-instance' ,
1096- cls = cls ,
1097- json_types = json_types ,
1098- json_function = json_function ,
1099- cls_repr = cls_repr ,
1100- ref = ref ,
1101- metadata = metadata ,
1102- serialization = serialization ,
1093+ type = 'is-instance' , cls = cls , cls_repr = cls_repr , ref = ref , metadata = metadata , serialization = serialization
11031094 )
11041095
11051096
@@ -2604,6 +2595,63 @@ def fn(v: str, info: core_schema.ValidationInfo) -> str:
26042595 )
26052596
26062597
2598+ class JsonOrPythonSchema (TypedDict , total = False ):
2599+ type : Required [Literal ['json-or-python' ]]
2600+ json_schema : Required [CoreSchema ]
2601+ python_schema : Required [CoreSchema ]
2602+ ref : str
2603+ metadata : Any
2604+ serialization : SerSchema
2605+
2606+
2607+ def json_or_python_schema (
2608+ json_schema : CoreSchema ,
2609+ python_schema : CoreSchema ,
2610+ * ,
2611+ ref : str | None = None ,
2612+ metadata : Any = None ,
2613+ serialization : SerSchema | None = None ,
2614+ ) -> JsonOrPythonSchema :
2615+ """
2616+ Returns a schema that uses the Json or Python schema depending on the input:
2617+
2618+ ```py
2619+ from pydantic_core import SchemaValidator, ValidationError, core_schema
2620+
2621+ v = SchemaValidator(
2622+ core_schema.json_or_python_schema(
2623+ json_schema=core_schema.int_schema(),
2624+ python_schema=core_schema.int_schema(strict=True),
2625+ )
2626+ )
2627+
2628+ assert v.validate_json('"123"') == 123
2629+
2630+ try:
2631+ v.validate_python('123')
2632+ except ValidationError:
2633+ pass
2634+ else:
2635+ raise AssertionError('Validation should have failed')
2636+ ```
2637+
2638+ Args:
2639+ json_schema: The schema to use for Json inputs
2640+ python_schema: The schema to use for Python inputs
2641+ ref: optional unique identifier of the schema, used to reference the schema in other places
2642+ metadata: Any other information you want to include with the schema, not used by pydantic-core
2643+ serialization: Custom serialization schema
2644+ """
2645+ return dict_not_none (
2646+ type = 'json-or-python' ,
2647+ json_schema = json_schema ,
2648+ python_schema = python_schema ,
2649+ ref = ref ,
2650+ metadata = metadata ,
2651+ serialization = serialization ,
2652+ )
2653+
2654+
26072655class TypedDictField (TypedDict , total = False ):
26082656 type : Required [Literal ['typed-dict-field' ]]
26092657 schema : Required [CoreSchema ]
@@ -3612,6 +3660,7 @@ def definition_reference_schema(
36123660 TaggedUnionSchema ,
36133661 ChainSchema ,
36143662 LaxOrStrictSchema ,
3663+ JsonOrPythonSchema ,
36153664 TypedDictSchema ,
36163665 ModelFieldsSchema ,
36173666 ModelSchema ,
@@ -3664,6 +3713,7 @@ def definition_reference_schema(
36643713 'tagged-union' ,
36653714 'chain' ,
36663715 'lax-or-strict' ,
3716+ 'json-or-python' ,
36673717 'typed-dict' ,
36683718 'model-fields' ,
36693719 'model' ,
0 commit comments