Skip to main content

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

AttributeTypeDescription
typeLiteral['datetime']The fixed literal identifier used to specify that this schema validates datetime objects.
strictboolDetermines whether to use strict mode, which requires the input to be a datetime object rather than a string that can be parsed.
ledatetimeThe inclusive upper bound for the datetime value, ensuring the input is less than or equal to this timestamp.
gedatetimeThe inclusive lower bound for the datetime value, ensuring the input is greater than or equal to this timestamp.
ltdatetimeThe exclusive upper bound for the datetime value, ensuring the input is strictly less than this timestamp.
gtdatetimeThe exclusive lower bound for the datetime value, ensuring the input is strictly greater than this timestamp.
now_opLiteral['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_offsetint = time.localtime().tm_gmtoffThe UTC offset in seconds used when comparing against 'now', restricted to a range between -86,400 and 86,400.
microseconds_precisionLiteral['truncate', 'error'] = truncateDefines the behavior when a datetime has microsecond precision, either truncating the extra data or raising a validation error.
refstrA unique string identifier for the schema used to create references in recursive or shared schema definitions.
metadatadict[str, Any]A dictionary of additional metadata used to store custom information or plugin-specific configuration for the schema.
serializationSerSchemaCustom 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

NameTypeDescription
typeLiteral['datetime']The fixed discriminator string identifying this as a datetime schema type.
strictboolWhether to disable type coercion and require strict datetime objects or specifically formatted strings.
ledatetimeThe inclusive upper bound constraint for the datetime value.
gedatetimeThe inclusive lower bound constraint for the datetime value.
ltdatetimeThe exclusive upper bound constraint for the datetime value.
gtdatetimeThe exclusive lower bound constraint for the datetime value.
now_opLiteral['past', 'future']A dynamic constraint requiring the datetime to be relative to the current system time.
tz_constraint`Literal['aware', 'naive']int`
now_utc_offsetintThe local UTC offset in seconds used to calculate 'now' for relative constraints, defaulting to the local system offset.
microseconds_precisionLiteral['truncate', 'error'] = 'truncate'Determines the behavior when encountering sub-microsecond precision during validation.
refstringA unique identifier for the schema used for recursive references and shared definitions.
metadatadict[str, Any]A dictionary of additional metadata used for custom validation logic or documentation generation.
serializationSerSchemaConfiguration defining how the datetime should be transformed into a serializable format like JSON.