literal_schema
Returns a schema that matches a literal value, e.g.:
from pydantic_core import SchemaValidator, core_schema
schema = core_schema.literal_schema(['hello', 'world'])
v = SchemaValidator(schema)
assert v.validate_python('hello') == 'hello'
def literal_schema(
expected: list[Any],
ref: str | None = None,
metadata: dict[str, Any] | None = None,
serialization: SerSchema | None = None
) - > LiteralSchema
Returns a schema that matches a literal value.
Parameters
| Name | Type | Description |
|---|---|---|
| expected | list[Any] | A list of specific values that the input must match to be considered valid. |
| ref | `str | None` = None |
| metadata | `dict[str, Any] | None` = None |
| serialization | `SerSchema | None` = None |
Returns
| Type | Description |
|---|---|
LiteralSchema | A core schema object that validates input against a specific set of allowed literal values. |