Schema Validation Flow¶
Pre-submission Record validation in the SDK is built around
ValidationSchemaProtocol, a unified protocol
implemented by both BaseSchemaCache
(SchemaCache and
AsyncSchemaCache) and
DataDictionary. Passing either implementation via
schema=... validates Record payloads locally and raises
ValidationError before an invalid payload is sent to the API.
SchemaCache vs. DataDictionary¶
SchemaCache and DataDictionary enforce different scopes of Study metadata checks
because the REST /forms endpoint exposes formId, formKey, and soft-deleted Form
status (disabled), while the REST /variables endpoint exposes 17 structural Variable
attributes (including variableType and deleted / disabled retirement metadata)
without exposing required policies, Choice codelists, numeric or length bounds, or
conditional XML rules. By contrast, an offline Data Dictionary
(CSV, CDISC ODM XML, or REDCap CSV) carries required policies, Choice codelists, numeric
and length bounds, CDISC ODM <RangeCheck> definitions, and conditional XML business logic
rules.
When validate_record_data() or
validate_record_entry() receives a REST-hydrated
BaseSchemaCache instance (SchemaCache
or AsyncSchemaCache), it verifies Form existence (including
active zero-Variable Forms hydrated from /forms), rejects new Record writes targeting
soft-deleted Forms where Form.disabled is True (rule_id="disabled_form") while
permitting updates to existing Records, rejects unknown Variable names, blocks scalar and
nested ***MASKED*** PHI sentinels (rule_id="masked_phi") before type checks, and
validates scalar Variable types across both active and retired Variables. Because REST
/variables omits required attributes, BaseSchemaCache evaluates Required Variable
policies (Yes and Auto Query) only when offline callers explicitly pass required
metadata via SchemaCache.populate. Passing an offline DataDictionary executes the full
Study constraint and rule pipeline:
Check |
|
|
|---|---|---|
Form existence (including zero-Variable Forms) & |
Enforced |
Enforced |
Soft-deleted Form rejection on new Record writes ( |
Enforced |
Enforced |
Unknown Variable detection on Form |
Enforced |
Enforced |
Masked PHI sentinel rejection ( |
Enforced |
Enforced |
Scalar Variable data type ( |
Enforced |
Enforced |
Required Variable policies ( |
Offline |
Enforced (offline CSV / ODM XML / REDCap) |
Choice codelist membership ( |
Skipped |
Enforced (offline CSV / ODM XML / REDCap) |
Numeric ( |
Skipped |
Enforced (offline CSV / ODM XML / REDCap) |
Conditional XML business logic rules via |
Skipped |
Enforced (offline CSV / ODM XML) |
Masked PHI Sentinels and Retired Variables¶
Before running Variable type checks, validate_record_data()
inspects every submitted value (both scalar strings and nested containers such as lists,
tuples, sets, and dictionaries) for the ***MASKED*** redaction token. Any matching
Variable fails immediately with rule_id="masked_phi" (and skips redundant downstream type
errors for that Variable) so redacted export payloads cannot overwrite clinical data in a
Study.
When hydrating BaseSchemaCache, Variables marked
deleted or disabled are stored in _retired_form_variables (while still registering
the Form in _known_forms and preserving formId to formKey links, even when every
Variable on a Form is retired), and soft-deleted Forms (Form.disabled) are tracked in
_disabled_forms. During validation:
Submitting a new Record payload (without an existing
recordId,recordOid, orvisitId) to adisabledForm fails immediately withrule_id="disabled_form", whereas updates to existing Records and historical export quality gates continue to resolve and validate the Form.Omitting a
deletedordisabledVariable never triggersrequiredorrequired_auto_queryissues, even if the historical Variable metadata declaredrequired=Trueorrequired="Yes".Supplying a value for a retired Variable in a historical Record payload does not fail with
rule_id="unknown_variable", and still undergoes***MASKED***sentinel and scalar data type checks (rule_id="type").
API-Backed Type Checks (SchemaValidator)¶
When no offline Data Dictionary is supplied, SchemaValidator
and AsyncSchemaValidator manage a SchemaCache or
AsyncSchemaCache hydrated from both FormsEndpoint
(get_forms) and VariablesEndpoint
(get_variables). If Form or Variable metadata for a Study Form is not yet cached, the
validator refreshes both endpoints from the API on demand so active zero-Variable Forms and
soft-deleted disabled Forms are registered alongside Variable types.
BaseSchemaCache partitions cached Form and Variable
definitions by (study_key, form_key) and Form ID resolutions by (study_key, form_id),
so multi-Study validators and workflows can validate Studies that share identical formKey
or formId values without cross-Study cache collisions.
The diagram below outlines the API-backed SchemaValidator flow.
Record payloads can also be validated asynchronously. Use
AsyncSchemaValidator.validate_batch with AsyncImednetSDK before
submitting Records:
async with AsyncImednetSDK() as sdk:
validator = AsyncSchemaValidator(sdk)
await validator.validate_batch(study_key, records)
await sdk.records.create(study_key, records, schema=validator.schema)
Rule and Constraint Validation (DataDictionary)¶
To enforce required Variables, Choice codelists, numeric/length bounds, and conditional XML
edit checks before calling POST /studies/{studyKey}/records, pass schema=data_dictionary
to sdk.records.create, sdk.records.create_batch,
RecordUpdateWorkflow, or
RegisterSubjectsWorkflow:
from imednet import ImednetSDK
from imednet.datadict import (
BusinessLogicRule,
Choice,
DataDictionary,
Form,
Metadata,
Variable,
parse_logic_xml,
)
from imednet.errors import ValidationError
from imednet.simulation import SimulationState, SimulationTransport
logic_xml = (
"<rule>"
'<conditions><condition variable="V_AGE" operator="<" value="18"/></conditions>'
'<actions><action type="error" message="Enrolled Subjects must be at least 18 years old."/></actions>'
"</rule>"
)
dd = DataDictionary(
metadata=Metadata.create(study_key="STUDY"),
forms=[
Form(
formKey="F_DM",
formName="Demographics",
variables=[
Variable(
variableName="V_AGE",
variableType="Integer",
required="Yes",
extraAttributes={"minValue": 0, "maxValue": 120},
),
Variable(
variableName="V_GENDER",
variableType="Radio",
required="Yes",
choices=[
Choice(choiceValue="Female", choiceText="Female"),
Choice(choiceValue="Male", choiceText="Male"),
],
),
],
businessLogic=[
BusinessLogicRule(
id="101",
name="Adult Consent Check",
type="Form",
status="Active",
form="F_DM",
logic=parse_logic_xml(logic_xml),
)
],
)
],
)
with ImednetSDK(
simulation_mode=True,
transport=SimulationTransport(SimulationState()),
) as sdk:
job = sdk.records.create(
"STUDY",
[
{
"formKey": "F_DM",
"siteName": "SITE",
"data": {"V_AGE": 34, "V_GENDER": "Female"},
}
],
schema=dd,
)
assert job.batch_id is not None
try:
sdk.records.create(
"STUDY",
[
{
"formKey": "F_DM",
"siteName": "SITE",
"data": {"V_AGE": 16, "V_GENDER": "Female"},
}
],
schema=dd,
)
except ValidationError as exc:
assert "Adult Consent Check" in str(exc)
See Data Dictionary Workflows and Rule Validation for loading a DataDictionary from CSV, CDISC ODM XML,
REDCap CSV, or the REST API.
Offline SchemaCache Example¶
imednet.testing.fake_data provides helpers for generating Form
metadata and sample Records without an API connection, and SchemaCache.populate
lets unit tests verify Required Variable policies (Yes and Auto Query),
***MASKED*** sentinel blocking, and retired Variable type checks offline:
from types import SimpleNamespace
from typing import Any, cast
from imednet.models.variables import Variable as ApiVariable
from imednet.testing import fake_data
from imednet.validation.cache import SchemaCache, validate_record_data
forms = fake_data.fake_forms_for_cache(1, study_key="S")
variables = fake_data.fake_variables_for_cache(
forms, vars_per_form=2, study_key="S"
)
forms_ep = cast(Any, SimpleNamespace(list=lambda **_: forms))
vars_ep = cast(
Any,
SimpleNamespace(
list=lambda form_id=None, **__: [
v for v in variables if form_id is None or v.form_id == form_id
]
),
)
schema = SchemaCache()
schema.refresh(forms_ep, vars_ep, study_key="S")
sample_record = fake_data.fake_record(schema)
assert "recordData" in sample_record
policy_cache = SchemaCache()
policy_cache.populate(
[
ApiVariable.model_validate(
{
"variableName": "V_SUBJ_AGE",
"variableType": "Integer",
"formId": 501,
"formKey": "F_SCREEN",
"required": "Yes",
}
),
ApiVariable.model_validate(
{
"variableName": "V_NOTES",
"variableType": "Multi Select",
"formId": 501,
"formKey": "F_SCREEN",
"required": "Auto Query",
}
),
ApiVariable.model_validate(
{
"variableName": "V_LEGACY_SCORE",
"variableType": "Integer",
"formId": 501,
"formKey": "F_SCREEN",
"deleted": True,
"required": True,
}
),
],
study_key="STUDY",
)
assert "V_LEGACY_SCORE" in policy_cache._retired_form_variables[("STUDY", "F_SCREEN")]
# Missing active 'Yes' raises required error; 'Auto Query' records a warning; retired Variable is not required
missing_report = validate_record_data(
policy_cache,
"F_SCREEN",
{},
study_key="STUDY",
collect_errors=True,
)
assert [e.rule_id for e in missing_report.errors] == ["required"]
assert [w.rule_id for w in missing_report.warnings] == ["required_auto_query"]
# Scalar/nested '***MASKED***' sentinels fail with 'masked_phi'; retired Variables are still type-checked
masked_and_retired = validate_record_data(
policy_cache,
"F_SCREEN",
{
"V_SUBJ_AGE": "***MASKED***",
"V_NOTES": ["***MASKED***"],
"V_LEGACY_SCORE": "not-an-int",
},
study_key="STUDY",
collect_errors=True,
)
assert [e.rule_id for e in masked_and_retired.errors] == [
"masked_phi",
"masked_phi",
"type",
]