imednet.validation package¶
Validation utilities for iMednet data.
This package provides tools for validating record data against study schemas, including caching of variable metadata and data dictionary management.
Submodules¶
imednet.validation.cache module¶
Caching and validation logic for iMednet variable schemas.
- class imednet.validation.cache.AsyncSchemaCache[source]¶
Bases:
BaseSchemaCache[AsyncImednetFacade]Asynchronous implementation of the schema cache.
- form_key_from_id(form_id, study_key=None)¶
Resolve a form key from
form_id, isolated bystudy_key.- Parameters:
form_id (int | str) –
study_key (str | None) –
- Return type:
str | None
- property forms: dict[str, dict[str, imednet.models.variables.Variable]]¶
Return cached variables grouped by form key for the active or unambiguous study.
- forms_for_study(study_key)¶
Return cached variables grouped by form key for
study_key.- Parameters:
study_key (str) –
- Return type:
dict[str, dict[str, Variable]]
- property is_prevalidated: bool¶
Return True while inside a
prevalidated()scope owned by the active task.
- is_record_prevalidated(record)¶
Return True if
recordwas already validated for this cache in the active task.- Parameters:
record (dict[str, Any]) –
- Return type:
bool
- populate(variables, study_key=None, forms=None)¶
Populate the cache with
formsandvariables, isolated bystudy_key.- Parameters:
variables (Iterable[Variable]) –
study_key (str | None) –
forms (Iterable[Any] | None) –
- Return type:
None
- prevalidated(records=None)¶
Mark
records(or the active block) as validated in the current task context.- Parameters:
records (Iterable[dict[str, Any]] | None) –
- Return type:
Iterator[None]
- refresh(forms, variables, study_key=None)¶
Refresh the cache, using the appropriate sync or async implementation.
- Parameters:
forms (FormsEndpoint | AsyncFormsEndpoint) – The forms endpoint (sync or async).
variables (VariablesEndpoint | AsyncVariablesEndpoint) – The variables endpoint (sync or async).
study_key (str | None) – The study key to refresh variables for.
- Returns:
An awaitable if async, else None.
- Return type:
Any
- property study_forms: dict[tuple[str, str], dict[str, imednet.models.variables.Variable]]¶
Return cached variables keyed by
(study_key, form_key).
- class imednet.validation.cache.AsyncSchemaValidator[source]¶
Bases:
BaseSchemaValidator[AsyncImednetFacade]Validate record payloads using variable metadata from the API (Asynchronous).
- __init__(sdk)[source]¶
Initialize the asynchronous schema validator.
- Parameters:
sdk (AsyncImednetFacade) –
- Return type:
None
- async refresh(study_key)[source]¶
Populate the schema cache for
study_keyfrom Forms and Variables endpoints.This method never raises
ValidationError; any API errors bubble up asApiError.- Parameters:
study_key (str) –
- Return type:
None
- async validate_batch(study_key: str, records: Sequence[BaseRecordRequest | Mapping[str, Any]], *, collect_errors: Literal[False] = False) None[source]¶
- async validate_batch(study_key: str, records: Sequence[BaseRecordRequest | Mapping[str, Any]], *, collect_errors: Literal[True]) BatchValidationReport
- async validate_batch(study_key: str, records: Sequence[BaseRecordRequest | Mapping[str, Any]], *, collect_errors: bool = False) BatchValidationReport | None
Validate a batch of record payloads asynchronously.
- async validate_record(study_key, record)[source]¶
Validate a single record payload asynchronously.
- Parameters:
study_key (str) –
record (BaseRecordRequest | Mapping[str, Any]) –
- Return type:
None
- class imednet.validation.cache.BaseSchemaCache[source]¶
Bases:
Generic[_TClient]Cache of variables partitioned by
(study_key, form_key)and(study_key, form_id).- __init__(is_async)[source]¶
Initialize the schema cache.
- Parameters:
is_async (bool) – Whether this cache is used in an asynchronous context.
- Return type:
None
- form_key_from_id(form_id, study_key=None)[source]¶
Resolve a form key from
form_id, isolated bystudy_key.- Parameters:
form_id (int | str) –
study_key (str | None) –
- Return type:
str | None
- property forms: dict[str, dict[str, imednet.models.variables.Variable]]¶
Return cached variables grouped by form key for the active or unambiguous study.
- forms_for_study(study_key)[source]¶
Return cached variables grouped by form key for
study_key.- Parameters:
study_key (str) –
- Return type:
dict[str, dict[str, Variable]]
- property is_prevalidated: bool¶
Return True while inside a
prevalidated()scope owned by the active task.
- is_record_prevalidated(record)[source]¶
Return True if
recordwas already validated for this cache in the active task.- Parameters:
record (dict[str, Any]) –
- Return type:
bool
- populate(variables, study_key=None, forms=None)[source]¶
Populate the cache with
formsandvariables, isolated bystudy_key.- Parameters:
variables (Iterable[Variable]) –
study_key (str | None) –
forms (Iterable[Any] | None) –
- Return type:
None
- prevalidated(records=None)[source]¶
Mark
records(or the active block) as validated in the current task context.- Parameters:
records (Iterable[dict[str, Any]] | None) –
- Return type:
Iterator[None]
- refresh(forms, variables, study_key=None)[source]¶
Refresh the cache, using the appropriate sync or async implementation.
- Parameters:
forms (FormsEndpoint | AsyncFormsEndpoint) – The forms endpoint (sync or async).
variables (VariablesEndpoint | AsyncVariablesEndpoint) – The variables endpoint (sync or async).
study_key (str | None) – The study key to refresh variables for.
- Returns:
An awaitable if async, else None.
- Return type:
Any
- property study_forms: dict[tuple[str, str], dict[str, imednet.models.variables.Variable]]¶
Return cached variables keyed by
(study_key, form_key).
- class imednet.validation.cache.BaseSchemaValidator[source]¶
Bases:
Generic[_TClient]Base validator sharing logic between sync and async implementations.
- class imednet.validation.cache.BatchValidationReport[source]¶
Bases:
objectAggregated multi-record validation report collecting all constraint errors and warnings.
- total_records: int = 0¶
- valid_records: int = 0¶
- invalid_records: int = 0¶
- errors: list[RecordValidationIssue] = list()¶
- warnings: list[RecordValidationIssue] = list()¶
- property error_count: int¶
Return total number of validation errors across the batch.
- format_summary()[source]¶
Format a concise PHI-safe summary of all validation errors.
- Return type:
str
- property is_valid: bool¶
Return True when zero validation errors were recorded.
- class imednet.validation.cache.RecordValidationIssue[source]¶
Bases:
objectStructured single validation error or warning produced during schema validation.
- record_index: int¶
Required.
- form_key: str | None¶
Required.
- variable_name: str | None¶
Required.
- message: str¶
Required.
- rule_id: str | None = None¶
- severity: Literal['error', 'warning'] = 'error'¶
- class imednet.validation.cache.SchemaCache[source]¶
Bases:
BaseSchemaCache[ImednetFacade]Synchronous implementation of the schema cache.
- form_key_from_id(form_id, study_key=None)¶
Resolve a form key from
form_id, isolated bystudy_key.- Parameters:
form_id (int | str) –
study_key (str | None) –
- Return type:
str | None
- property forms: dict[str, dict[str, imednet.models.variables.Variable]]¶
Return cached variables grouped by form key for the active or unambiguous study.
- forms_for_study(study_key)¶
Return cached variables grouped by form key for
study_key.- Parameters:
study_key (str) –
- Return type:
dict[str, dict[str, Variable]]
- property is_prevalidated: bool¶
Return True while inside a
prevalidated()scope owned by the active task.
- is_record_prevalidated(record)¶
Return True if
recordwas already validated for this cache in the active task.- Parameters:
record (dict[str, Any]) –
- Return type:
bool
- populate(variables, study_key=None, forms=None)¶
Populate the cache with
formsandvariables, isolated bystudy_key.- Parameters:
variables (Iterable[Variable]) –
study_key (str | None) –
forms (Iterable[Any] | None) –
- Return type:
None
- prevalidated(records=None)¶
Mark
records(or the active block) as validated in the current task context.- Parameters:
records (Iterable[dict[str, Any]] | None) –
- Return type:
Iterator[None]
- refresh(forms, variables, study_key=None)¶
Refresh the cache, using the appropriate sync or async implementation.
- Parameters:
forms (FormsEndpoint | AsyncFormsEndpoint) – The forms endpoint (sync or async).
variables (VariablesEndpoint | AsyncVariablesEndpoint) – The variables endpoint (sync or async).
study_key (str | None) – The study key to refresh variables for.
- Returns:
An awaitable if async, else None.
- Return type:
Any
- property study_forms: dict[tuple[str, str], dict[str, imednet.models.variables.Variable]]¶
Return cached variables keyed by
(study_key, form_key).
- class imednet.validation.cache.SchemaValidator[source]¶
Bases:
BaseSchemaValidator[ImednetFacade]Validate record payloads using variable metadata from the API (Synchronous).
- __init__(sdk)[source]¶
Initialize the synchronous schema validator.
- Parameters:
sdk (ImednetFacade) –
- Return type:
None
- refresh(study_key)[source]¶
Populate the schema cache for
study_keyfrom Forms and Variables endpoints.This method never raises
ValidationError; any API errors bubble up asApiError.- Parameters:
study_key (str) –
- Return type:
None
- validate_batch(study_key: str, records: Sequence[BaseRecordRequest | Mapping[str, Any]], *, collect_errors: Literal[False] = False) None[source]¶
- validate_batch(study_key: str, records: Sequence[BaseRecordRequest | Mapping[str, Any]], *, collect_errors: Literal[True]) BatchValidationReport
- validate_batch(study_key: str, records: Sequence[BaseRecordRequest | Mapping[str, Any]], *, collect_errors: bool = False) BatchValidationReport | None
Validate a batch of record payloads.
- validate_record(study_key, record)[source]¶
Validate a single record payload.
- Parameters:
study_key (str) –
record (BaseRecordRequest | Mapping[str, Any]) –
- Return type:
None
- class imednet.validation.cache.ValidationSchemaProtocol[source]¶
Bases:
ProtocolUnified schema protocol implemented by BaseSchemaCache and DataDictionary.
- imednet.validation.cache.calculate_readiness_score(schema, form_key, data, *, study_key=None)[source]¶
Calculate the schema readiness score for a Study Record’s data.
- Returns:
A tuple of (score, list of validation failure or warning reasons).
- Parameters:
schema (ValidationSchemaProtocol) –
form_key (str) –
data (dict[str, Any]) –
study_key (str | None) –
- Return type:
tuple[float, list[str]]
- imednet.validation.cache.is_schema_record_prevalidated(schema, record)[source]¶
Return True if
recordwas already validated forschemain the active task.- Parameters:
schema (Any) –
record (dict[str, Any]) –
- Return type:
bool
- imednet.validation.cache.prevalidated_records(schema, records=None)[source]¶
Mark
recordsas pre-validated forschemain the active task context.- Parameters:
schema (Any) –
records (Iterable[dict[str, Any]] | None) –
- Return type:
Iterator[None]
- imednet.validation.cache.validate_record_data(schema: ValidationSchemaProtocol, form_key: str, data: dict[str, Any], *, study_key: str | None = None, collect_errors: Literal[False] = False, record_index: int = 0, allow_disabled_form: bool = False) None[source]¶
- imednet.validation.cache.validate_record_data(schema: ValidationSchemaProtocol, form_key: str, data: dict[str, Any], *, study_key: str | None = None, collect_errors: Literal[True], record_index: int = 0, allow_disabled_form: bool = False) BatchValidationReport
- imednet.validation.cache.validate_record_data(schema: ValidationSchemaProtocol, form_key: str, data: dict[str, Any], *, study_key: str | None = None, collect_errors: bool = False, record_index: int = 0, allow_disabled_form: bool = False) BatchValidationReport | None
Validate
dataforform_keyusing the provided validation schema.- Raises:
ValidationError – If
collect_errors=Falseand the form key or data fails validation checks.
- imednet.validation.cache.validate_record_entry(schema: ValidationSchemaProtocol, record: BaseRecordRequest | Mapping[str, Any], *, study_key: str | None = None, collect_errors: Literal[False] = False, record_index: int = 0) None[source]¶
- imednet.validation.cache.validate_record_entry(schema: ValidationSchemaProtocol, record: BaseRecordRequest | Mapping[str, Any], *, study_key: str | None = None, collect_errors: Literal[True], record_index: int = 0) BatchValidationReport
- imednet.validation.cache.validate_record_entry(schema: ValidationSchemaProtocol, record: BaseRecordRequest | Mapping[str, Any], *, study_key: str | None = None, collect_errors: bool = False, record_index: int = 0) BatchValidationReport | None
Validate a single record entry against the schema.
Resolves the form key from “formKey”, “form_key”, “formId”, or “form_id”.
- imednet.validation.cache.validate_records_batch(schema, records, *, study_key=None, collect_errors=True)[source]¶
Validate a sequence of records against schema, aggregating errors when requested.
- Parameters:
schema (ValidationSchemaProtocol) –
records (Sequence[BaseRecordRequest | Mapping[str, Any]]) –
study_key (str | None) –
collect_errors (bool) –
- Return type: