DatetimeSchema
This class defines the schema for datetime validation and serialization, allowing for strict type checking and range constraints using inclusive or exclusive boundaries. It supports advanced configurations for timezone awareness, UTC offsets, and microsecond precision handling. Additionally, it provides options for validating datetimes relative to the current time and includes support for metadata and custom serialization schemas.
Attributes
| Attribute | Type | Description |
|---|---|---|
| type | Literal['datetime'] | The fixed literal identifier used to specify that this schema validates datetime objects. |
| strict | bool | Determines whether to use strict mode, which requires the input to be a datetime object rather than a string that can be parsed. |
| le | datetime | The inclusive upper bound for the datetime value, ensuring the input is less than or equal to this timestamp. |
| ge | datetime | The inclusive lower bound for the datetime value, ensuring the input is greater than or equal to this timestamp. |
| lt | datetime | The exclusive upper bound for the datetime value, ensuring the input is strictly less than this timestamp. |
| gt | datetime | The exclusive lower bound for the datetime value, ensuring the input is strictly greater than this timestamp. |
| now_op | Literal['past', 'future'] | Constraint that requires the datetime to be relative to the current time, either in the past or the future. |
| tz_constraint | `Literal['aware', 'naive'] | int` |
| now_utc_offset | int = time.localtime().tm_gmtoff | The UTC offset in seconds used when comparing against 'now', restricted to a range between -86,400 and 86,400. |
| microseconds_precision | Literal['truncate', 'error'] = truncate | Defines the behavior when a datetime has microsecond precision, either truncating the extra data or raising a validation error. |
| ref | str | A unique string identifier for the schema used to create references in recursive or shared schema definitions. |
| metadata | dict[str, Any] | A dictionary of additional metadata used to store custom information or plugin-specific configuration for the schema. |
| serialization | SerSchema | Custom serialization configuration that defines how the datetime should be converted to a string or other format during serialization. |
Constructor
Signature
def DatetimeSchema(
type: Literal['datetime'],
strict: bool,
le: datetime,
ge: datetime,
lt: datetime,
gt: datetime,
now_op: Literal['past', 'future'],
tz_constraint: Literal['aware', 'naive']| int,
now_utc_offset: int,
microseconds_precision: Literal['truncate', 'error'] = 'truncate',
ref: string,
metadata: dict[str, Any],
serialization: SerSchema
) - > null
Parameters
| Name | Type | Description |
|---|---|---|
| type | Literal['datetime'] | The fixed discriminator string identifying this as a datetime schema type. |
| strict | bool | Whether to disable type coercion and require strict datetime objects or specifically formatted strings. |
| le | datetime | The inclusive upper bound constraint for the datetime value. |
| ge | datetime | The inclusive lower bound constraint for the datetime value. |
| lt | datetime | The exclusive upper bound constraint for the datetime value. |
| gt | datetime | The exclusive lower bound constraint for the datetime value. |
| now_op | Literal['past', 'future'] | A dynamic constraint requiring the datetime to be relative to the current system time. |
| tz_constraint | `Literal['aware', 'naive'] | int` |
| now_utc_offset | int | The local UTC offset in seconds used to calculate 'now' for relative constraints, defaulting to the local system offset. |
| microseconds_precision | Literal['truncate', 'error'] = 'truncate' | Determines the behavior when encountering sub-microsecond precision during validation. |
| ref | string | A unique identifier for the schema used for recursive references and shared definitions. |
| metadata | dict[str, Any] | A dictionary of additional metadata used for custom validation logic or documentation generation. |
| serialization | SerSchema | Configuration defining how the datetime should be transformed into a serializable format like JSON. |