schema_driven_records
Build a Pydantic model FROM the Dataverse schema, then use it to validate real dataset values.
Workflow: 1. Load the metadatablocks schema (dataverse_schema.py) -> know what fields exist, their types, whether they're required/multiple, and what compound sub-fields they contain. 2. Dynamically build a Pydantic model that mirrors that schema exactly, using pydantic.create_model. 3. Load a dataset export (dataset_instance.py) and flatten it to {typeName: value}. 4. Validate that flat dict against the schema-derived model, so the actual metadata is checked against the same field names, required-ness, and structure the schema defines -- instead of a generic model that would accept any field.
build_record_model(source, *, model_name=None)
Recursively build a Pydantic model matching a schema MetadataBlock (or a compound MetadataField).
The resulting model accepts exactly the fields the schema defines: correct nested structure for compound fields, List[...] wrapping for multiple=True fields, and required vs Optional matching isRequired.
Source code in src/dv_schema_models/schema_driven_records.py
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 | |
flatten_instance(block_instance)
Turn a dataset export's field list into a flat {typeName: value} dict, ready to validate.
Source code in src/dv_schema_models/schema_driven_records.py
97 98 99 | |