Validate input against a reference list
Check that an incoming list of values exists in a reference list, surface every missing or duplicate input, and apply the change only once validation passes.
import sasvi
from sasvi import errors
client = sasvi.SVIClient.from_env()
record = client.admin.reference_lists.get(name="<list-name>")
if record is None:
raise SystemExit("reference list does not exist on this tenant")
incoming = ["HIGH", "MEDIUM", "URGENT", "LOW", "HIGH"] # one typo, one duplicate
result = record.validate_values(incoming, by="code", case_sensitive=True)
if not result: # bool(result) is False when validation failed
print(f"Validation failed: {len(result.missing)} missing, {len(result.duplicates)} duplicate.")
print("missing:", result.missing_values)
print("duplicates:", result.duplicate_values)
result.raise_for_invalid() # raises ReferenceListValidationError
bool(result) returns valid, and the missing_values and duplicate_values
properties give you compact lists for logging. Call raise_for_invalid() instead
when you want an inline pipeline to abort on any failure.
Load the values
valid_codes = [v for v in incoming if v not in result.missing_values]
record.items.replace([
{"code": code, "label": code.title()}
for code in dict.fromkeys(valid_codes) # dedupe, keep input order
])
record.items.replace(...) does an atomic full-collection write. Use
record.items.append(...) to add one item, and record.items.upsert(...) to add or
update one.
CSV ingest
from sasvi import errors
from pathlib import Path
raw = Path("incoming.csv").read_text().splitlines()
codes = [row.strip() for row in raw if row.strip()]
result = record.validate_values(codes, by="code")
try:
result.raise_for_invalid()
except errors.ReferenceListValidationError as exc:
log_failure(exc.details) # the structured details list
raise
record.items.replace([{"code": c, "label": c.title()} for c in codes])
Async
validate_values_async takes the same arguments. It reads the record's loaded copy of the
list, and awaits one read of the list first when the record has none, for example after a
write cleared it:
result = await record.validate_values_async(codes)
if result:
await record.items.replace_async([...])
Go further
- Configuration guide. Reference list CRUD and bulk loading.
- Errors reference.
ReferenceListValidationError.