Skip to content

Commit 2f8050f

Browse files
committed
chore: regenerate the example helpers
1 parent 97be33d commit 2f8050f

2 files changed

Lines changed: 42 additions & 15 deletions

File tree

‎examples/python-r4-us-core/fhir_types/profile_helpers.py‎

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -603,17 +603,29 @@ def referenced_resource_type(reference: str) -> str | None:
603603

604604

605605
def validate_reference(res: object, profile_name: str, field: str, allowed: Sequence[str]) -> list[str]:
606-
"""Checks that a Reference field points to one of the ``allowed`` resource
607-
types."""
606+
"""Checks that every reference present names one of the ``allowed`` resource
607+
types.
608+
609+
A repeating element (``Provenance.target``, ``Observation.focus``) holds a
610+
list of References where a single one holds an object, and a Reference is
611+
never itself a list — so the shape tells the two apart with no help from the
612+
caller. One error per offending type, not per entry, so a list of ten wrong
613+
references of the same type reports once.
614+
"""
608615
value = _get_field(res, field)
609616
if value is None:
610617
return []
611-
ref = _get_field(value, "reference")
612-
if not isinstance(ref, str):
613-
return []
614-
ref_type = referenced_resource_type(ref)
615-
if ref_type is None or ref_type in allowed:
616-
return []
618+
entries = value if isinstance(value, list) else [value]
619+
offending: list[str] = []
620+
for entry in entries:
621+
ref = _get_field(entry, "reference")
622+
if not isinstance(ref, str):
623+
continue
624+
ref_type = referenced_resource_type(ref)
625+
if ref_type is None or ref_type in allowed or ref_type in offending:
626+
continue
627+
offending.append(ref_type)
617628
return [
618629
f"{profile_name}: field '{field}' references '{ref_type}' but only {', '.join(allowed)} are allowed"
630+
for ref_type in offending
619631
]

‎examples/typescript-r4-us-core/fhir-types/profile-helpers.ts‎

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -561,14 +561,29 @@ const REFERENCE_TYPE_RE = /(?:^|\/)([A-Za-z]+)\/[A-Za-z0-9\-.]{1,64}(?:\/_histor
561561

562562
export const referencedResourceType = (reference: string): string | undefined => REFERENCE_TYPE_RE.exec(reference)?.[1];
563563

564+
/**
565+
* Checks that every reference present names one of the `allowed` resource types.
566+
*
567+
* A repeating element (`Provenance.target`, `Observation.focus`) holds a list of
568+
* References where a single one holds an object, and a Reference is never itself
569+
* an array — so the shape tells the two apart with no help from the caller. One
570+
* error per offending type, not per entry, so a list of ten wrong references of
571+
* the same type reports once.
572+
*/
564573
export const validateReference = (res: object, profileName: string, field: string, allowed: string[]): string[] => {
565574
const value = (res as Record<string, unknown>)[field];
566575
if (value === undefined || value === null) return [];
567-
const ref = (value as Record<string, unknown>).reference as string | undefined;
568-
if (!ref) return [];
569-
const refType = referencedResourceType(ref);
570-
if (refType === undefined) return [];
571-
return allowed.includes(refType)
572-
? []
573-
: [`${profileName}: field '${field}' references '${refType}' but only ${allowed.join(", ")} are allowed`];
576+
const entries = Array.isArray(value) ? value : [value];
577+
const offending: string[] = [];
578+
for (const entry of entries) {
579+
const ref = (entry as Record<string, unknown> | null)?.reference as string | undefined;
580+
if (!ref) continue;
581+
const refType = referencedResourceType(ref);
582+
if (refType === undefined || allowed.includes(refType)) continue;
583+
if (!offending.includes(refType)) offending.push(refType);
584+
}
585+
return offending.map(
586+
(refType) =>
587+
`${profileName}: field '${field}' references '${refType}' but only ${allowed.join(", ")} are allowed`,
588+
);
574589
};

0 commit comments

Comments
 (0)