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.

__init__()[source]

Initialize the asynchronous schema cache.

Return type:

None

form_key_from_id(form_id, study_key=None)

Resolve a form key from form_id, isolated by study_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 record was 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 forms and variables, isolated by study_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:
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).

variables_for_form(form_key, study_key=None)

Return the variables associated with form_key for study_key.

Parameters:
  • form_key (str) –

  • study_key (str | None) –

Return type:

dict[str, Variable]

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_key from Forms and Variables endpoints.

This method never raises ValidationError; any API errors bubble up as ApiError.

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:
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 by study_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 record was 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 forms and variables, isolated by study_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:
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).

variables_for_form(form_key, study_key=None)[source]

Return the variables associated with form_key for study_key.

Parameters:
  • form_key (str) –

  • study_key (str | None) –

Return type:

dict[str, Variable]

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: object

Aggregated 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.

raise_for_errors()[source]

Raise ValidationError with validation_report attached if invalid.

Return type:

None

to_dict()[source]

Serialize the batch validation report to a JSON-compatible dictionary.

Return type:

dict[str, Any]

class imednet.validation.cache.RecordValidationIssue[source]

Bases: object

Structured 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'
to_dict()[source]

Serialize validation issue to a JSON-compatible dictionary.

Return type:

dict[str, Any]

class imednet.validation.cache.SchemaCache[source]

Bases: BaseSchemaCache[ImednetFacade]

Synchronous implementation of the schema cache.

__init__()[source]

Initialize the synchronous schema cache.

Return type:

None

form_key_from_id(form_id, study_key=None)

Resolve a form key from form_id, isolated by study_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 record was 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 forms and variables, isolated by study_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:
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).

variables_for_form(form_key, study_key=None)

Return the variables associated with form_key for study_key.

Parameters:
  • form_key (str) –

  • study_key (str | None) –

Return type:

dict[str, Variable]

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_key from Forms and Variables endpoints.

This method never raises ValidationError; any API errors bubble up as ApiError.

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:
Return type:

None

class imednet.validation.cache.ValidationSchemaProtocol[source]

Bases: Protocol

Unified schema protocol implemented by BaseSchemaCache and DataDictionary.

form_key_from_id(form_id)[source]

Resolve a formKey from a formId.

Parameters:

form_id (int | str) –

Return type:

str | None

variables_for_form(form_key)[source]

Return a mapping of variable names to variable definitions for form_key.

Parameters:

form_key (str) –

Return type:

Mapping[str, Any]

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:
Return type:

tuple[float, list[str]]

imednet.validation.cache.is_schema_record_prevalidated(schema, record)[source]

Return True if record was already validated for schema in the active task.

Parameters:
  • schema (Any) –

  • record (dict[str, Any]) –

Return type:

bool

imednet.validation.cache.prevalidated_records(schema, records=None)[source]

Mark records as pre-validated for schema in 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 data for form_key using the provided validation schema.

Raises:

ValidationError – If collect_errors=False and 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:
Return type:

BatchValidationReport