Skip to content

Commit

Permalink
Include JSON Schema input core schema in function schemas (#1572)
Browse files Browse the repository at this point in the history
  • Loading branch information
Viicos authored and davidhewitt committed Dec 18, 2024
1 parent 83ff1cf commit 3a0cea9
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions python/pydantic_core/core_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -1970,13 +1970,15 @@ 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(
function: NoInfoValidatorFunction,
schema: CoreSchema,
*,
ref: str | None = None,
json_schema_input_schema: CoreSchema | None = None,
metadata: Dict[str, Any] | None = None,
serialization: SerSchema | None = None,
) -> BeforeValidatorFunctionSchema:
Expand All @@ -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
"""
Expand All @@ -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,
)
Expand All @@ -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:
Expand Down Expand Up @@ -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
"""
Expand All @@ -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,
)
Expand All @@ -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:
Expand All @@ -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
"""
Expand All @@ -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,
)
Expand Down Expand Up @@ -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

Expand All @@ -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:
Expand Down Expand Up @@ -2225,13 +2237,15 @@ 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
"""
return _dict_not_none(
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,
Expand All @@ -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,
Expand Down Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -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

Expand All @@ -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:
Expand All @@ -2320,13 +2339,15 @@ 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
"""
return _dict_not_none(
type='function-plain',
function={'type': 'no-info', 'function': function},
ref=ref,
json_schema_input_schema=json_schema_input_schema,
metadata=metadata,
serialization=serialization,
)
Expand All @@ -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:
Expand All @@ -2359,13 +2381,15 @@ 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
"""
return _dict_not_none(
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,
)
Expand Down

0 comments on commit 3a0cea9

Please sign in to comment.