Skip to content

Commit 459e415

Browse files
committed
refactor throwing error logic for lineage
Signed-off-by: Apoorva Pendse <[email protected]>
1 parent cc65dda commit 459e415

File tree

1 file changed

+32
-22
lines changed

1 file changed

+32
-22
lines changed

shared-libs/cht-datasource/src/qualifier.ts

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -317,18 +317,19 @@ export const byPersonQualifier = (data: unknown): PersonQualifier => {
317317
throw new InvalidArgumentError(`Missing or empty required fields [${JSON.stringify(qualifier)}].`);
318318
}
319319

320+
// cannot use checkFieldLineageAndThrowError as parent is required in `person`
320321
if (!isNormalizedParent(qualifier.parent)) {
321322
throw new InvalidArgumentError(`Missing required fields in the parent hierarchy [${JSON.stringify(qualifier)}].`);
322323
}
323324

324-
if (!hasValidContactType(qualifier) && !hasValidLegacyContactType(qualifier, 'person')) {
325-
throw new InvalidArgumentError('Invalid type for contacts.');
326-
}
327-
328325
if (hasBloatedLineage(qualifier)) {
329326
throw new InvalidArgumentError(`Additional fields found in the parent lineage [${JSON.stringify(qualifier)}].`);
330327
}
331328

329+
if (!hasValidContactType(qualifier) && !hasValidLegacyContactType(qualifier, 'person')) {
330+
throw new InvalidArgumentError('Invalid type for contacts.');
331+
}
332+
332333
return qualifier as unknown as PersonQualifier;
333334
};
334335

@@ -402,24 +403,8 @@ export type PlaceQualifier = ContactQualifier & Readonly<{
402403
*/
403404
export const byPlaceQualifier = (data:unknown): PlaceQualifier => {
404405
const qualifier = byContactQualifierNonAssertive(data);
405-
if ('parent' in qualifier) {
406-
if (!isNormalizedParent(qualifier.parent)) {
407-
throw new InvalidArgumentError(`Missing required fields in the parent hierarchy [${JSON.stringify(qualifier)}].`);
408-
}
409-
if (hasBloatedLineage(qualifier)) {
410-
throw new InvalidArgumentError(`Additional fields found in the parent lineage [${JSON.stringify(qualifier)}].`);
411-
}
412-
}
413-
414-
if (hasField(qualifier, {name: 'contact', type: 'object'})) {
415-
if (!isNormalizedParent(qualifier.contact)) {
416-
// eslint-disable-next-line max-len
417-
throw new InvalidArgumentError(`Missing required fields in the contact hierarchy [${JSON.stringify(qualifier)}].`);
418-
}
419-
if (hasBloatedLineage(qualifier.contact)){
420-
throw new InvalidArgumentError(`Additional fields found in the contact lineage [${JSON.stringify(qualifier)}].`);
421-
}
422-
}
406+
checkFieldLineageAndThrowError(qualifier);
407+
checkFieldLineageAndThrowError(qualifier, 'contact');
423408

424409
if (!hasValidContactType(qualifier) && !hasValidLegacyContactType(qualifier, 'place')) {
425410
throw new InvalidArgumentError('Invalid type for contacts.');
@@ -428,6 +413,31 @@ export const byPlaceQualifier = (data:unknown): PlaceQualifier => {
428413
return qualifier as PlaceQualifier;
429414
};
430415

416+
/** @internal*/
417+
const checkFieldLineageAndThrowError = (data: Record<string, unknown>, field?:string) => {
418+
let hierarchyRoot = data;
419+
let normalized_parent = data.parent;
420+
421+
if (field && hasField(data, {name: field, type: 'object'})) {
422+
hierarchyRoot = data[field] as unknown as Record<string, unknown>;
423+
normalized_parent = data[field];
424+
}
425+
426+
if (!hasField(hierarchyRoot, {type: 'object', name: 'parent'})){
427+
return;
428+
}
429+
430+
if (!isNormalizedParent(normalized_parent)) {
431+
// eslint-disable-next-line max-len
432+
throw new InvalidArgumentError(`Missing required fields in the ${field??'parent'} hierarchy [${JSON.stringify(data)}].`);
433+
}
434+
if (hasBloatedLineage(hierarchyRoot)) {
435+
// eslint-disable-next-line max-len
436+
throw new InvalidArgumentError(`Additional fields found in the ${field??'parent'} lineage [${JSON.stringify(data)}].`);
437+
}
438+
};
439+
440+
431441
/** @internal*/
432442
export const isPlaceQualifier = (data:unknown) : data is PlaceQualifier => {
433443
if (!checkContactQualifierFields(data)) {

0 commit comments

Comments
 (0)