From 3a0cea9cd12744f1434cf3a9fb8b2badfa3eea1c Mon Sep 17 00:00:00 2001 From: Victorien <65306057+Viicos@users.noreply.github.com> Date: Thu, 12 Dec 2024 15:11:18 +0100 Subject: [PATCH] Include JSON Schema input core schema in function schemas (#1572) --- python/pydantic_core/core_schema.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/python/pydantic_core/core_schema.py b/python/pydantic_core/core_schema.py index 0fcfe9cab..e588e7721 100644 --- a/python/pydantic_core/core_schema.py +++ b/python/pydantic_core/core_schema.py @@ -1970,6 +1970,7 @@ class _ValidatorFunctionSchema(TypedDict, total=False): class BeforeValidatorFunctionSchema(_ValidatorFunctionSchema, total=False): type: Required[Literal['function-before']] + json_schema_input_schema: CoreSchema def no_info_before_validator_function( @@ -1977,6 +1978,7 @@ def no_info_before_validator_function( schema: CoreSchema, *, ref: str | None = None, + json_schema_input_schema: CoreSchema | None = None, metadata: Dict[str, Any] | None = None, serialization: SerSchema | None = None, ) -> BeforeValidatorFunctionSchema: @@ -2002,6 +2004,7 @@ def fn(v: bytes) -> str: function: The validator function to call schema: The schema to validate the output of the validator function ref: optional unique identifier of the schema, used to reference the schema in other places + json_schema_input_schema: The core schema to be used to generate the corresponding JSON Schema input type metadata: Any other information you want to include with the schema, not used by pydantic-core serialization: Custom serialization schema """ @@ -2010,6 +2013,7 @@ def fn(v: bytes) -> str: function={'type': 'no-info', 'function': function}, schema=schema, ref=ref, + json_schema_input_schema=json_schema_input_schema, metadata=metadata, serialization=serialization, ) @@ -2021,6 +2025,7 @@ def with_info_before_validator_function( *, field_name: str | None = None, ref: str | None = None, + json_schema_input_schema: CoreSchema | None = None, metadata: Dict[str, Any] | None = None, serialization: SerSchema | None = None, ) -> BeforeValidatorFunctionSchema: @@ -2050,6 +2055,7 @@ def fn(v: bytes, info: core_schema.ValidationInfo) -> str: field_name: The name of the field schema: The schema to validate the output of the validator function ref: optional unique identifier of the schema, used to reference the schema in other places + json_schema_input_schema: The core schema to be used to generate the corresponding JSON Schema input type metadata: Any other information you want to include with the schema, not used by pydantic-core serialization: Custom serialization schema """ @@ -2058,6 +2064,7 @@ def fn(v: bytes, info: core_schema.ValidationInfo) -> str: function=_dict_not_none(type='with-info', function=function, field_name=field_name), schema=schema, ref=ref, + json_schema_input_schema=json_schema_input_schema, metadata=metadata, serialization=serialization, ) @@ -2072,6 +2079,7 @@ def no_info_after_validator_function( schema: CoreSchema, *, ref: str | None = None, + json_schema_input_schema: CoreSchema | None = None, metadata: Dict[str, Any] | None = None, serialization: SerSchema | None = None, ) -> AfterValidatorFunctionSchema: @@ -2095,6 +2103,7 @@ def fn(v: str) -> str: function: The validator function to call after the schema is validated schema: The schema to validate before the validator function ref: optional unique identifier of the schema, used to reference the schema in other places + json_schema_input_schema: The core schema to be used to generate the corresponding JSON Schema input type metadata: Any other information you want to include with the schema, not used by pydantic-core serialization: Custom serialization schema """ @@ -2103,6 +2112,7 @@ def fn(v: str) -> str: function={'type': 'no-info', 'function': function}, schema=schema, ref=ref, + json_schema_input_schema=json_schema_input_schema, metadata=metadata, serialization=serialization, ) @@ -2188,6 +2198,7 @@ class WrapValidatorFunctionSchema(TypedDict, total=False): function: Required[WrapValidatorFunction] schema: Required[CoreSchema] ref: str + json_schema_input_schema: CoreSchema metadata: Dict[str, Any] serialization: SerSchema @@ -2197,6 +2208,7 @@ def no_info_wrap_validator_function( schema: CoreSchema, *, ref: str | None = None, + json_schema_input_schema: CoreSchema | None = None, metadata: Dict[str, Any] | None = None, serialization: SerSchema | None = None, ) -> WrapValidatorFunctionSchema: @@ -2225,6 +2237,7 @@ def fn( function: The validator function to call schema: The schema to validate the output of the validator function ref: optional unique identifier of the schema, used to reference the schema in other places + json_schema_input_schema: The core schema to be used to generate the corresponding JSON Schema input type metadata: Any other information you want to include with the schema, not used by pydantic-core serialization: Custom serialization schema """ @@ -2232,6 +2245,7 @@ def fn( type='function-wrap', function={'type': 'no-info', 'function': function}, schema=schema, + json_schema_input_schema=json_schema_input_schema, ref=ref, metadata=metadata, serialization=serialization, @@ -2243,6 +2257,7 @@ def with_info_wrap_validator_function( schema: CoreSchema, *, field_name: str | None = None, + json_schema_input_schema: CoreSchema | None = None, ref: str | None = None, metadata: Dict[str, Any] | None = None, serialization: SerSchema | None = None, @@ -2273,6 +2288,7 @@ def fn( function: The validator function to call schema: The schema to validate the output of the validator function field_name: The name of the field this validators is applied to, if any + json_schema_input_schema: The core schema to be used to generate the corresponding JSON Schema input type ref: optional unique identifier of the schema, used to reference the schema in other places metadata: Any other information you want to include with the schema, not used by pydantic-core serialization: Custom serialization schema @@ -2281,6 +2297,7 @@ def fn( type='function-wrap', function=_dict_not_none(type='with-info', function=function, field_name=field_name), schema=schema, + json_schema_input_schema=json_schema_input_schema, ref=ref, metadata=metadata, serialization=serialization, @@ -2291,6 +2308,7 @@ class PlainValidatorFunctionSchema(TypedDict, total=False): type: Required[Literal['function-plain']] function: Required[ValidationFunction] ref: str + json_schema_input_schema: CoreSchema metadata: Dict[str, Any] serialization: SerSchema @@ -2299,6 +2317,7 @@ def no_info_plain_validator_function( function: NoInfoValidatorFunction, *, ref: str | None = None, + json_schema_input_schema: CoreSchema | None = None, metadata: Dict[str, Any] | None = None, serialization: SerSchema | None = None, ) -> PlainValidatorFunctionSchema: @@ -2320,6 +2339,7 @@ def fn(v: str) -> str: Args: function: The validator function to call ref: optional unique identifier of the schema, used to reference the schema in other places + json_schema_input_schema: The core schema to be used to generate the corresponding JSON Schema input type metadata: Any other information you want to include with the schema, not used by pydantic-core serialization: Custom serialization schema """ @@ -2327,6 +2347,7 @@ def fn(v: str) -> str: type='function-plain', function={'type': 'no-info', 'function': function}, ref=ref, + json_schema_input_schema=json_schema_input_schema, metadata=metadata, serialization=serialization, ) @@ -2337,6 +2358,7 @@ def with_info_plain_validator_function( *, field_name: str | None = None, ref: str | None = None, + json_schema_input_schema: CoreSchema | None = None, metadata: Dict[str, Any] | None = None, serialization: SerSchema | None = None, ) -> PlainValidatorFunctionSchema: @@ -2359,6 +2381,7 @@ def fn(v: str, info: core_schema.ValidationInfo) -> str: function: The validator function to call field_name: The name of the field this validators is applied to, if any ref: optional unique identifier of the schema, used to reference the schema in other places + json_schema_input_schema: The core schema to be used to generate the corresponding JSON Schema input type metadata: Any other information you want to include with the schema, not used by pydantic-core serialization: Custom serialization schema """ @@ -2366,6 +2389,7 @@ def fn(v: str, info: core_schema.ValidationInfo) -> str: type='function-plain', function=_dict_not_none(type='with-info', function=function, field_name=field_name), ref=ref, + json_schema_input_schema=json_schema_input_schema, metadata=metadata, serialization=serialization, )