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 Dec 12, 2024
1 parent 6a455fb commit 98bc1e2
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 @@ -1971,13 +1971,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 @@ -2003,6 +2005,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 @@ -2011,6 +2014,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 @@ -2022,6 +2026,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 @@ -2051,6 +2056,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 @@ -2059,6 +2065,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 @@ -2073,6 +2080,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 @@ -2096,6 +2104,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 @@ -2104,6 +2113,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 @@ -2189,6 +2199,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 @@ -2198,6 +2209,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 @@ -2226,13 +2238,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 @@ -2244,6 +2258,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 @@ -2274,6 +2289,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 @@ -2282,6 +2298,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 @@ -2292,6 +2309,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 @@ -2300,6 +2318,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 @@ -2321,13 +2340,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 @@ -2338,6 +2359,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 @@ -2360,13 +2382,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 98bc1e2

Please sign in to comment.