@cosyte/x12 0.0.5 → 0.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -2463,8 +2463,55 @@ function coerce(value) {
2463
2463
  return "[unrenderable value]";
2464
2464
  }
2465
2465
  }
2466
+ function renderCallerJson(value) {
2467
+ let text;
2468
+ try {
2469
+ text = JSON.stringify(value) ?? "undefined";
2470
+ } catch {
2471
+ return "[unrenderable value]";
2472
+ }
2473
+ if (text.length <= BUILD_REFUSAL_VALUE_MAX_LENGTH) return text;
2474
+ return `${text.slice(0, BUILD_REFUSAL_VALUE_MAX_LENGTH)}\u2026 (${String(text.length)} characters)`;
2475
+ }
2476
+
2477
+ // src/builder/caller-array.ts
2478
+ var NONE = Object.freeze([]);
2479
+ function describeShape(value) {
2480
+ if (typeof value !== "object") {
2481
+ return `a ${typeof value} (${renderCallerValue(value)})`;
2482
+ }
2483
+ let length;
2484
+ try {
2485
+ length = value.length;
2486
+ } catch {
2487
+ return "an object whose 'length' getter threw";
2488
+ }
2489
+ if (length !== void 0) {
2490
+ return `an array-like object with length ${renderCallerValue(length)}`;
2491
+ }
2492
+ let tag;
2493
+ try {
2494
+ tag = Object.prototype.toString.call(value);
2495
+ } catch {
2496
+ tag = "[object Unknown]";
2497
+ }
2498
+ return `a non-array ${renderCallerValue(tag)}`;
2499
+ }
2500
+ function requireCallerArray(value, at, refuse) {
2501
+ if (value === void 0 || value === null) return NONE;
2502
+ if (!isRealArray(value)) {
2503
+ refuse(`${at} must be an array. Received ${describeShape(value)}.`);
2504
+ }
2505
+ return value;
2506
+ }
2507
+ function isRealArray(value) {
2508
+ return Array.isArray(value);
2509
+ }
2466
2510
 
2467
2511
  // src/transactions/remit/build-835.ts
2512
+ function refuseSpec(message) {
2513
+ throw new Remit835BuildError(REMIT_835_BUILD_ERROR_CODES.X12_835_BUILD_INVALID_SPEC, message);
2514
+ }
2468
2515
  var X221A1_VERSION_RELEASE = "005010X221A1";
2469
2516
  var X12_835_FUNCTIONAL_ID = "HP";
2470
2517
  var X12_AGENCY_CODE = "X";
@@ -2618,7 +2665,14 @@ function build835(spec) {
2618
2665
  emitClaim(claim, body, seg, esc, comp);
2619
2666
  }
2620
2667
  }
2621
- emitProviderAdjustments(spec.providerAdjustments ?? [], body, seg, esc, comp);
2668
+ emitProviderAdjustments(
2669
+ spec.providerAdjustments,
2670
+ "build835: spec.providerAdjustments",
2671
+ body,
2672
+ seg,
2673
+ esc,
2674
+ comp
2675
+ );
2622
2676
  const seCount = body.length + 2;
2623
2677
  const se = seg(["SE", String(seCount), esc(stControlNumber)]);
2624
2678
  const ge = seg(["GE", "1", esc(envelope.groupControlNumber)]);
@@ -2627,14 +2681,16 @@ function build835(spec) {
2627
2681
  return parseX12(raw);
2628
2682
  }
2629
2683
  function enforceStructuralSpec(spec) {
2630
- if (spec.traces.length === 0) {
2684
+ const traces = requireCallerArray(spec.traces, "build835: spec.traces", refuseSpec);
2685
+ if (traces.length === 0) {
2631
2686
  throw new Remit835BuildError(
2632
2687
  REMIT_835_BUILD_ERROR_CODES.X12_835_BUILD_INVALID_SPEC,
2633
2688
  "build835: at least one TRN trace is required (TR3 005010X221A1 Loop header)."
2634
2689
  );
2635
2690
  }
2636
- for (let i = 0; i < spec.claims.length; i += 1) {
2637
- const claim = spec.claims[i];
2691
+ const claims = requireCallerArray(spec.claims, "build835: spec.claims", refuseSpec);
2692
+ for (let i = 0; i < claims.length; i += 1) {
2693
+ const claim = claims[i];
2638
2694
  if (claim === void 0) continue;
2639
2695
  if (claim.patientControlNumber === "") {
2640
2696
  throw new Remit835BuildError(
@@ -2796,7 +2852,7 @@ function emitClaim(claim, body, seg, esc, comp) {
2796
2852
  comp([claim.facilityTypeCode ?? "", "", claim.claimFrequencyCode ?? ""])
2797
2853
  ])
2798
2854
  );
2799
- emitCasGroup(claim.adjustments ?? [], body, seg, esc);
2855
+ emitCasGroup(claim.adjustments, "build835: claim.adjustments", body, seg, esc);
2800
2856
  if (claim.patient !== void 0) body.push(emitPerson(claim.patient, seg, esc));
2801
2857
  if (claim.subscriber !== void 0) body.push(emitPerson(claim.subscriber, seg, esc));
2802
2858
  if (claim.correctedPatient !== void 0) body.push(emitPerson(claim.correctedPatient, seg, esc));
@@ -2842,7 +2898,7 @@ function emitServiceLine(line, body, seg, esc, comp) {
2842
2898
  ])
2843
2899
  );
2844
2900
  emitServiceLineDtm(line, body, seg, esc);
2845
- emitCasGroup(line.adjustments ?? [], body, seg, esc);
2901
+ emitCasGroup(line.adjustments, "build835: serviceLine.adjustments", body, seg, esc);
2846
2902
  for (const ref of line.references ?? []) body.push(emitRef(ref, seg, esc));
2847
2903
  for (const amt of line.amounts ?? []) {
2848
2904
  body.push(seg(["AMT", esc(amt.qualifier), esc(amt.amount.toString())]));
@@ -2860,7 +2916,8 @@ function emitServiceLineDtm(line, body, seg, esc) {
2860
2916
  if (start !== void 0) body.push(seg(["DTM", "150", esc(start)]));
2861
2917
  if (end !== void 0) body.push(seg(["DTM", "151", esc(end)]));
2862
2918
  }
2863
- function emitCasGroup(adjustments, body, seg, esc) {
2919
+ function emitCasGroup(raw, at, body, seg, esc) {
2920
+ const adjustments = requireCallerArray(raw, at, refuseSpec);
2864
2921
  let i = 0;
2865
2922
  while (i < adjustments.length) {
2866
2923
  const first = adjustments[i];
@@ -2918,7 +2975,8 @@ function emitProvider(provider, seg, esc) {
2918
2975
  esc(provider.idCode ?? "")
2919
2976
  ]);
2920
2977
  }
2921
- function emitProviderAdjustments(adjustments, body, seg, esc, comp) {
2978
+ function emitProviderAdjustments(raw, at, body, seg, esc, comp) {
2979
+ const adjustments = requireCallerArray(raw, at, refuseSpec);
2922
2980
  let i = 0;
2923
2981
  while (i < adjustments.length) {
2924
2982
  const first = adjustments[i];
@@ -4112,6 +4170,15 @@ var Claim837BuildError = class extends Error {
4112
4170
  };
4113
4171
 
4114
4172
  // src/transactions/claim/build-837.ts
4173
+ function refuseHierarchy(message) {
4174
+ throw new Claim837BuildError(
4175
+ CLAIM_837_BUILD_ERROR_CODES.X12_837_BUILD_INVALID_HIERARCHY,
4176
+ message
4177
+ );
4178
+ }
4179
+ function refuseSpec2(message) {
4180
+ throw new Claim837BuildError(CLAIM_837_BUILD_ERROR_CODES.X12_837_BUILD_INVALID_SPEC, message);
4181
+ }
4115
4182
  var VERSION_BY_VARIANT = {
4116
4183
  P: "005010X222A2",
4117
4184
  I: "005010X223A3",
@@ -4222,26 +4289,45 @@ function buildClaim837(variant, spec) {
4222
4289
  return parseX12(raw);
4223
4290
  }
4224
4291
  function enforceStructuralSpec2(variant, spec) {
4225
- if (spec.billingProviders.length === 0) {
4292
+ const billingProviders = requireCallerArray(
4293
+ spec.billingProviders,
4294
+ "build837: spec.billingProviders",
4295
+ refuseHierarchy
4296
+ );
4297
+ if (billingProviders.length === 0) {
4226
4298
  throw new Claim837BuildError(
4227
4299
  CLAIM_837_BUILD_ERROR_CODES.X12_837_BUILD_INVALID_HIERARCHY,
4228
4300
  "build837: at least one billing provider (HL level 20) is required."
4229
4301
  );
4230
4302
  }
4231
- for (let b = 0; b < spec.billingProviders.length; b += 1) {
4232
- const billing = spec.billingProviders[b];
4303
+ for (let b = 0; b < billingProviders.length; b += 1) {
4304
+ const billing = billingProviders[b];
4233
4305
  if (billing === void 0) continue;
4234
- if (billing.subscribers.length === 0) {
4306
+ const subscribers = requireCallerArray(
4307
+ billing.subscribers,
4308
+ `build837: spec.billingProviders[${String(b)}].subscribers`,
4309
+ refuseHierarchy
4310
+ );
4311
+ if (subscribers.length === 0) {
4235
4312
  throw new Claim837BuildError(
4236
4313
  CLAIM_837_BUILD_ERROR_CODES.X12_837_BUILD_INVALID_HIERARCHY,
4237
4314
  `build837: billing provider at index ${String(b)} has no subscriber (HL level 22) child.`
4238
4315
  );
4239
4316
  }
4240
- for (let s = 0; s < billing.subscribers.length; s += 1) {
4241
- const subscriber = billing.subscribers[s];
4317
+ for (let s = 0; s < subscribers.length; s += 1) {
4318
+ const subscriber = subscribers[s];
4242
4319
  if (subscriber === void 0) continue;
4243
- const directClaims = subscriber.claims ?? [];
4244
- const patients = subscriber.patients ?? [];
4320
+ const locator = `spec.billingProviders[${String(b)}].subscribers[${String(s)}]`;
4321
+ const directClaims = requireCallerArray(
4322
+ subscriber.claims,
4323
+ `build837: ${locator}.claims`,
4324
+ refuseHierarchy
4325
+ );
4326
+ const patients = requireCallerArray(
4327
+ subscriber.patients,
4328
+ `build837: ${locator}.patients`,
4329
+ refuseHierarchy
4330
+ );
4245
4331
  if (directClaims.length === 0 && patients.length === 0) {
4246
4332
  throw new Claim837BuildError(
4247
4333
  CLAIM_837_BUILD_ERROR_CODES.X12_837_BUILD_INVALID_HIERARCHY,
@@ -4261,14 +4347,19 @@ function enforceStructuralSpec2(variant, spec) {
4261
4347
  for (let p = 0; p < patients.length; p += 1) {
4262
4348
  const patient = patients[p];
4263
4349
  if (patient === void 0) continue;
4264
- if (patient.claims.length === 0) {
4350
+ const patientClaims = requireCallerArray(
4351
+ patient.claims,
4352
+ `build837: ${locator}.patients[${String(p)}].claims`,
4353
+ refuseHierarchy
4354
+ );
4355
+ if (patientClaims.length === 0) {
4265
4356
  throw new Claim837BuildError(
4266
4357
  CLAIM_837_BUILD_ERROR_CODES.X12_837_BUILD_INVALID_HIERARCHY,
4267
4358
  `build837: dependent patient at billing[${String(b)}].subscriber[${String(s)}].patient[${String(p)}] has no claim.`
4268
4359
  );
4269
4360
  }
4270
- for (let c = 0; c < patient.claims.length; c += 1) {
4271
- const claim = patient.claims[c];
4361
+ for (let c = 0; c < patientClaims.length; c += 1) {
4362
+ const claim = patientClaims[c];
4272
4363
  if (claim !== void 0) {
4273
4364
  enforceClaim(
4274
4365
  variant,
@@ -4288,14 +4379,19 @@ function enforceClaim(variant, claim, locator) {
4288
4379
  `build837: claim at ${locator} has an empty claimId (CLM-01 is required).`
4289
4380
  );
4290
4381
  }
4291
- if (claim.serviceLines.length === 0) {
4382
+ const serviceLines = requireCallerArray(
4383
+ claim.serviceLines,
4384
+ `build837: claim at ${locator}: serviceLines`,
4385
+ refuseSpec2
4386
+ );
4387
+ if (serviceLines.length === 0) {
4292
4388
  throw new Claim837BuildError(
4293
4389
  CLAIM_837_BUILD_ERROR_CODES.X12_837_BUILD_INVALID_SPEC,
4294
4390
  `build837: claim at ${locator} has no service line (a CLM requires at least one LX/SVx loop).`
4295
4391
  );
4296
4392
  }
4297
- for (let l = 0; l < claim.serviceLines.length; l += 1) {
4298
- const line = claim.serviceLines[l];
4393
+ for (let l = 0; l < serviceLines.length; l += 1) {
4394
+ const line = serviceLines[l];
4299
4395
  if (line === void 0) continue;
4300
4396
  if (line.variant !== variant) {
4301
4397
  throw new Claim837BuildError(
@@ -4525,12 +4621,13 @@ function emitAdjudication(adj, body, ctx) {
4525
4621
  adj.paidUnits === void 0 ? "" : ctx.esc(adj.paidUnits.toString())
4526
4622
  ])
4527
4623
  );
4528
- emitCasGroup2(adj.adjustments ?? [], body, ctx);
4624
+ emitCasGroup2(adj.adjustments, "build837: adjudication.adjustments", body, ctx);
4529
4625
  if (adj.dateAdjudicated !== void 0) {
4530
4626
  body.push(ctx.seg(["DTP", "573", "D8", ctx.esc(adj.dateAdjudicated)]));
4531
4627
  }
4532
4628
  }
4533
- function emitCasGroup2(adjustments, body, ctx) {
4629
+ function emitCasGroup2(raw, at, body, ctx) {
4630
+ const adjustments = requireCallerArray(raw, at, refuseSpec2);
4534
4631
  let i = 0;
4535
4632
  while (i < adjustments.length) {
4536
4633
  const first = adjustments[i];
@@ -5248,6 +5345,12 @@ var Eligibility271BuildError = class extends Error {
5248
5345
  };
5249
5346
 
5250
5347
  // src/transactions/eligibility/build-271.ts
5348
+ function refuseHierarchy2(message) {
5349
+ throw new Eligibility271BuildError(
5350
+ ELIGIBILITY_271_BUILD_ERROR_CODES.X12_271_BUILD_INVALID_HIERARCHY,
5351
+ message
5352
+ );
5353
+ }
5251
5354
  var X279A1_VERSION_RELEASE = "005010X279A1";
5252
5355
  var X12_271_FUNCTIONAL_ID = "HB";
5253
5356
  var X12_AGENCY_CODE3 = "X";
@@ -5325,25 +5428,40 @@ function build271(spec) {
5325
5428
  return parseX12(raw);
5326
5429
  }
5327
5430
  function enforceStructuralSpec3(spec) {
5328
- if (spec.informationSources.length === 0) {
5431
+ const sources = requireCallerArray(
5432
+ spec.informationSources,
5433
+ "build271: spec.informationSources",
5434
+ refuseHierarchy2
5435
+ );
5436
+ if (sources.length === 0) {
5329
5437
  throw new Eligibility271BuildError(
5330
5438
  ELIGIBILITY_271_BUILD_ERROR_CODES.X12_271_BUILD_INVALID_HIERARCHY,
5331
5439
  "build271: at least one information source (HL level 20) is required."
5332
5440
  );
5333
5441
  }
5334
- for (let s = 0; s < spec.informationSources.length; s += 1) {
5335
- const source = spec.informationSources[s];
5442
+ for (let s = 0; s < sources.length; s += 1) {
5443
+ const source = sources[s];
5336
5444
  if (source === void 0) continue;
5337
- if (source.receivers.length === 0) {
5445
+ const receivers = requireCallerArray(
5446
+ source.receivers,
5447
+ `build271: spec.informationSources[${String(s)}].receivers`,
5448
+ refuseHierarchy2
5449
+ );
5450
+ if (receivers.length === 0) {
5338
5451
  throw new Eligibility271BuildError(
5339
5452
  ELIGIBILITY_271_BUILD_ERROR_CODES.X12_271_BUILD_INVALID_HIERARCHY,
5340
5453
  `build271: information source at index ${String(s)} has no receiver (HL level 21) child.`
5341
5454
  );
5342
5455
  }
5343
- for (let r = 0; r < source.receivers.length; r += 1) {
5344
- const receiver = source.receivers[r];
5456
+ for (let r = 0; r < receivers.length; r += 1) {
5457
+ const receiver = receivers[r];
5345
5458
  if (receiver === void 0) continue;
5346
- if (receiver.subscribers.length === 0) {
5459
+ const subscribers = requireCallerArray(
5460
+ receiver.subscribers,
5461
+ `build271: spec.informationSources[${String(s)}].receivers[${String(r)}].subscribers`,
5462
+ refuseHierarchy2
5463
+ );
5464
+ if (subscribers.length === 0) {
5347
5465
  throw new Eligibility271BuildError(
5348
5466
  ELIGIBILITY_271_BUILD_ERROR_CODES.X12_271_BUILD_INVALID_HIERARCHY,
5349
5467
  `build271: receiver at source[${String(s)}].receiver[${String(r)}] has no subscriber (HL level 22) child.`
@@ -5919,6 +6037,18 @@ var ClaimStatus277BuildError = class extends Error {
5919
6037
  };
5920
6038
 
5921
6039
  // src/transactions/status/build-277.ts
6040
+ function refuseHierarchy3(message) {
6041
+ throw new ClaimStatus277BuildError(
6042
+ CLAIM_STATUS_277_BUILD_ERROR_CODES.X12_277_BUILD_INVALID_HIERARCHY,
6043
+ message
6044
+ );
6045
+ }
6046
+ function refuseSpec3(message) {
6047
+ throw new ClaimStatus277BuildError(
6048
+ CLAIM_STATUS_277_BUILD_ERROR_CODES.X12_277_BUILD_INVALID_SPEC,
6049
+ message
6050
+ );
6051
+ }
5922
6052
  var X12_277_FUNCTIONAL_ID = "HN";
5923
6053
  var X12_AGENCY_CODE4 = "X";
5924
6054
  var HL_LEVEL3 = {
@@ -6013,42 +6143,62 @@ function buildClaimStatus(version, spec) {
6013
6143
  return parseX12(raw);
6014
6144
  }
6015
6145
  function enforceStructuralSpec4(spec) {
6016
- if (spec.informationSources.length === 0) {
6146
+ const sources = requireCallerArray(
6147
+ spec.informationSources,
6148
+ "build277: spec.informationSources",
6149
+ refuseHierarchy3
6150
+ );
6151
+ if (sources.length === 0) {
6017
6152
  throw new ClaimStatus277BuildError(
6018
6153
  CLAIM_STATUS_277_BUILD_ERROR_CODES.X12_277_BUILD_INVALID_HIERARCHY,
6019
6154
  "build277: at least one information source (HL level 20) is required."
6020
6155
  );
6021
6156
  }
6022
- for (let s = 0; s < spec.informationSources.length; s += 1) {
6023
- const source = spec.informationSources[s];
6157
+ for (let s = 0; s < sources.length; s += 1) {
6158
+ const source = sources[s];
6024
6159
  if (source === void 0) continue;
6025
- if (source.receivers.length === 0) {
6160
+ const receivers = requireCallerArray(
6161
+ source.receivers,
6162
+ `build277: spec.informationSources[${String(s)}].receivers`,
6163
+ refuseHierarchy3
6164
+ );
6165
+ if (receivers.length === 0) {
6026
6166
  throw new ClaimStatus277BuildError(
6027
6167
  CLAIM_STATUS_277_BUILD_ERROR_CODES.X12_277_BUILD_INVALID_HIERARCHY,
6028
6168
  `build277: information source at index ${String(s)} has no receiver (HL level 21) child.`
6029
6169
  );
6030
6170
  }
6031
- for (let r = 0; r < source.receivers.length; r += 1) {
6032
- const receiver = source.receivers[r];
6171
+ for (let r = 0; r < receivers.length; r += 1) {
6172
+ const receiver = receivers[r];
6033
6173
  if (receiver === void 0) continue;
6034
- if (receiver.providers.length === 0) {
6174
+ const providers = requireCallerArray(
6175
+ receiver.providers,
6176
+ `build277: spec.informationSources[${String(s)}].receivers[${String(r)}].providers`,
6177
+ refuseHierarchy3
6178
+ );
6179
+ if (providers.length === 0) {
6035
6180
  throw new ClaimStatus277BuildError(
6036
6181
  CLAIM_STATUS_277_BUILD_ERROR_CODES.X12_277_BUILD_INVALID_HIERARCHY,
6037
6182
  `build277: receiver at source[${String(s)}].receiver[${String(r)}] has no provider (HL level 19) child.`
6038
6183
  );
6039
6184
  }
6040
- for (let p = 0; p < receiver.providers.length; p += 1) {
6041
- const provider = receiver.providers[p];
6185
+ for (let p = 0; p < providers.length; p += 1) {
6186
+ const provider = providers[p];
6042
6187
  if (provider === void 0) continue;
6043
6188
  const locator = `source[${String(s)}].receiver[${String(r)}].provider[${String(p)}]`;
6044
- if (provider.subscribers.length === 0) {
6189
+ const subscribers = requireCallerArray(
6190
+ provider.subscribers,
6191
+ `build277: ${locator}.subscribers`,
6192
+ refuseHierarchy3
6193
+ );
6194
+ if (subscribers.length === 0) {
6045
6195
  throw new ClaimStatus277BuildError(
6046
6196
  CLAIM_STATUS_277_BUILD_ERROR_CODES.X12_277_BUILD_INVALID_HIERARCHY,
6047
6197
  `build277: provider at ${locator} has no subscriber (HL level 22) child.`
6048
6198
  );
6049
6199
  }
6050
- for (let u = 0; u < provider.subscribers.length; u += 1) {
6051
- const subscriber = provider.subscribers[u];
6200
+ for (let u = 0; u < subscribers.length; u += 1) {
6201
+ const subscriber = subscribers[u];
6052
6202
  if (subscriber === void 0) continue;
6053
6203
  enforceSubscriber(subscriber, `${locator}.subscriber[${String(u)}]`);
6054
6204
  }
@@ -6057,8 +6207,16 @@ function enforceStructuralSpec4(spec) {
6057
6207
  }
6058
6208
  }
6059
6209
  function enforceSubscriber(subscriber, locator) {
6060
- const claims = subscriber.claims ?? [];
6061
- const dependents = subscriber.dependents ?? [];
6210
+ const claims = requireCallerArray(
6211
+ subscriber.claims,
6212
+ `build277: subscriber at ${locator}: claims`,
6213
+ refuseHierarchy3
6214
+ );
6215
+ const dependents = requireCallerArray(
6216
+ subscriber.dependents,
6217
+ `build277: subscriber at ${locator}: dependents`,
6218
+ refuseHierarchy3
6219
+ );
6062
6220
  if (claims.length === 0 && dependents.length === 0) {
6063
6221
  throw new ClaimStatus277BuildError(
6064
6222
  CLAIM_STATUS_277_BUILD_ERROR_CODES.X12_277_BUILD_INVALID_HIERARCHY,
@@ -6073,21 +6231,34 @@ function enforceSubscriber(subscriber, locator) {
6073
6231
  const dependent = dependents[d];
6074
6232
  if (dependent === void 0) continue;
6075
6233
  const depLocator = `${locator}.dependent[${String(d)}]`;
6076
- if (dependent.claims.length === 0) {
6234
+ const depClaims = requireCallerArray(
6235
+ dependent.claims,
6236
+ `build277: dependent at ${depLocator}: claims`,
6237
+ refuseHierarchy3
6238
+ );
6239
+ if (depClaims.length === 0) {
6077
6240
  throw new ClaimStatus277BuildError(
6078
6241
  CLAIM_STATUS_277_BUILD_ERROR_CODES.X12_277_BUILD_INVALID_HIERARCHY,
6079
6242
  `build277: dependent at ${depLocator} has no claim.`
6080
6243
  );
6081
6244
  }
6082
- for (let c = 0; c < dependent.claims.length; c += 1) {
6083
- const claim = dependent.claims[c];
6245
+ for (let c = 0; c < depClaims.length; c += 1) {
6246
+ const claim = depClaims[c];
6084
6247
  if (claim !== void 0) enforceClaim2(claim, `${depLocator}.claim[${String(c)}]`, c === 0);
6085
6248
  }
6086
6249
  }
6087
6250
  }
6088
6251
  function enforceClaim2(claim, locator, isFirst) {
6089
- const statuses = claim.statuses ?? [];
6090
- const serviceLines = claim.serviceLines ?? [];
6252
+ const statuses = requireCallerArray(
6253
+ claim.statuses,
6254
+ `build277: claim at ${locator}: statuses`,
6255
+ refuseSpec3
6256
+ );
6257
+ const serviceLines = requireCallerArray(
6258
+ claim.serviceLines,
6259
+ `build277: claim at ${locator}: serviceLines`,
6260
+ refuseSpec3
6261
+ );
6091
6262
  if (claim.trace === void 0 && statuses.length === 0 && serviceLines.length === 0) {
6092
6263
  throw new ClaimStatus277BuildError(
6093
6264
  CLAIM_STATUS_277_BUILD_ERROR_CODES.X12_277_BUILD_INVALID_SPEC,
@@ -6106,7 +6277,11 @@ function enforceClaim2(claim, locator, isFirst) {
6106
6277
  for (let l = 0; l < serviceLines.length; l += 1) {
6107
6278
  const line = serviceLines[l];
6108
6279
  if (line === void 0) continue;
6109
- const lineStatuses = line.statuses ?? [];
6280
+ const lineStatuses = requireCallerArray(
6281
+ line.statuses,
6282
+ `build277: claim at ${locator}.line[${String(l)}]: statuses`,
6283
+ refuseSpec3
6284
+ );
6110
6285
  for (let i = 0; i < lineStatuses.length; i += 1) {
6111
6286
  enforceStatus(lineStatuses[i], `${locator}.line[${String(l)}].status[${String(i)}]`);
6112
6287
  }
@@ -6656,6 +6831,9 @@ var Premium820BuildError = class extends Error {
6656
6831
  };
6657
6832
 
6658
6833
  // src/transactions/premium/build-820.ts
6834
+ function refuseSpec4(message) {
6835
+ throw new Premium820BuildError(PREMIUM_820_BUILD_ERROR_CODES.X12_820_BUILD_INVALID_SPEC, message);
6836
+ }
6659
6837
  var X218_VERSION_RELEASE = "005010X218";
6660
6838
  var X12_820_FUNCTIONAL_ID = "RA";
6661
6839
  var X12_AGENCY_CODE5 = "X";
@@ -6799,20 +6977,26 @@ function build820(spec) {
6799
6977
  return parseX12(raw);
6800
6978
  }
6801
6979
  function enforceStructuralSpec5(spec) {
6802
- if (spec.traces.length === 0) {
6980
+ const traces = requireCallerArray(spec.traces, "build820: spec.traces", refuseSpec4);
6981
+ if (traces.length === 0) {
6803
6982
  throw new Premium820BuildError(
6804
6983
  PREMIUM_820_BUILD_ERROR_CODES.X12_820_BUILD_INVALID_SPEC,
6805
6984
  "build820: at least one TRN trace is required (TR3 005010X218 header)."
6806
6985
  );
6807
6986
  }
6808
- if (spec.remittances.length === 0) {
6987
+ const remittances = requireCallerArray(
6988
+ spec.remittances,
6989
+ "build820: spec.remittances",
6990
+ refuseSpec4
6991
+ );
6992
+ if (remittances.length === 0) {
6809
6993
  throw new Premium820BuildError(
6810
6994
  PREMIUM_820_BUILD_ERROR_CODES.X12_820_BUILD_INVALID_SPEC,
6811
6995
  "build820: at least one remittance (Loop 2000) is required."
6812
6996
  );
6813
6997
  }
6814
- for (let r = 0; r < spec.remittances.length; r += 1) {
6815
- const remittance = spec.remittances[r];
6998
+ for (let r = 0; r < remittances.length; r += 1) {
6999
+ const remittance = remittances[r];
6816
7000
  if (remittance === void 0) continue;
6817
7001
  if (remittance.entity === void 0 && remittance.individual === void 0) {
6818
7002
  throw new Premium820BuildError(
@@ -6820,14 +7004,19 @@ function enforceStructuralSpec5(spec) {
6820
7004
  `build820: remittance at index ${String(r)} has neither an entity (ENT) nor an individual (NM1) to open its loop.`
6821
7005
  );
6822
7006
  }
6823
- if (remittance.openItems.length === 0) {
7007
+ const openItems = requireCallerArray(
7008
+ remittance.openItems,
7009
+ `build820: spec.remittances[${String(r)}].openItems`,
7010
+ refuseSpec4
7011
+ );
7012
+ if (openItems.length === 0) {
6824
7013
  throw new Premium820BuildError(
6825
7014
  PREMIUM_820_BUILD_ERROR_CODES.X12_820_BUILD_INVALID_SPEC,
6826
7015
  `build820: remittance at index ${String(r)} has no RMR open item.`
6827
7016
  );
6828
7017
  }
6829
- for (let o = 0; o < remittance.openItems.length; o += 1) {
6830
- const item = remittance.openItems[o];
7018
+ for (let o = 0; o < openItems.length; o += 1) {
7019
+ const item = openItems[o];
6831
7020
  if (item === void 0) continue;
6832
7021
  if (item.qualifier === "" && item.referenceId === "") {
6833
7022
  throw new Premium820BuildError(
@@ -7338,6 +7527,12 @@ var Enrollment834BuildError = class extends Error {
7338
7527
  };
7339
7528
 
7340
7529
  // src/transactions/enrollment/build-834.ts
7530
+ function refuseSpec5(message) {
7531
+ throw new Enrollment834BuildError(
7532
+ ENROLLMENT_834_BUILD_ERROR_CODES.X12_834_BUILD_INVALID_SPEC,
7533
+ message
7534
+ );
7535
+ }
7341
7536
  var X220A1_VERSION_RELEASE = "005010X220A1";
7342
7537
  var X12_834_FUNCTIONAL_ID = "BE";
7343
7538
  var X12_AGENCY_CODE6 = "X";
@@ -7437,14 +7632,15 @@ function build834(spec) {
7437
7632
  return parseX12(raw);
7438
7633
  }
7439
7634
  function enforceStructuralSpec6(spec) {
7440
- if (spec.members.length === 0) {
7635
+ const members = requireCallerArray(spec.members, "build834: spec.members", refuseSpec5);
7636
+ if (members.length === 0) {
7441
7637
  throw new Enrollment834BuildError(
7442
7638
  ENROLLMENT_834_BUILD_ERROR_CODES.X12_834_BUILD_INVALID_SPEC,
7443
7639
  "build834: at least one member loop (INS) is required."
7444
7640
  );
7445
7641
  }
7446
- for (let m = 0; m < spec.members.length; m += 1) {
7447
- const member = spec.members[m];
7642
+ for (let m = 0; m < members.length; m += 1) {
7643
+ const member = members[m];
7448
7644
  if (member === void 0) continue;
7449
7645
  if (member.maintenanceTypeCode === "") {
7450
7646
  throw new Enrollment834BuildError(
@@ -7458,8 +7654,13 @@ function enforceStructuralSpec6(spec) {
7458
7654
  `build834: member at index ${String(m)} has an unknown INS-03 maintenance type code ${renderCallerValue(member.maintenanceTypeCode)} (outside X12 Code Source 875).`
7459
7655
  );
7460
7656
  }
7461
- for (let c = 0; c < (member.healthCoverages ?? []).length; c += 1) {
7462
- const coverage = member.healthCoverages?.[c];
7657
+ const coverages = requireCallerArray(
7658
+ member.healthCoverages,
7659
+ `build834: spec.members[${String(m)}].healthCoverages`,
7660
+ refuseSpec5
7661
+ );
7662
+ for (let c = 0; c < coverages.length; c += 1) {
7663
+ const coverage = coverages[c];
7463
7664
  if (coverage === void 0) continue;
7464
7665
  const hd01 = coverage.maintenanceTypeCode;
7465
7666
  if (hd01 !== void 0 && hd01 !== "" && lookupMaintenanceType(hd01) === void 0) {
@@ -7977,6 +8178,12 @@ var ServicesReview278BuildError = class extends Error {
7977
8178
  };
7978
8179
 
7979
8180
  // src/transactions/auth/build-278.ts
8181
+ function refuseHierarchy4(message) {
8182
+ throw new ServicesReview278BuildError(
8183
+ AUTH_278_BUILD_ERROR_CODES.X12_278_BUILD_INVALID_HIERARCHY,
8184
+ message
8185
+ );
8186
+ }
7980
8187
  var X12_278_FUNCTIONAL_ID = "HI";
7981
8188
  var X12_AGENCY_CODE7 = "X";
7982
8189
  var HL_LEVEL4 = {
@@ -8088,7 +8295,11 @@ function buildServicesReview(version, direction, spec) {
8088
8295
  }
8089
8296
  function enforceStructuralSpec7(spec, direction) {
8090
8297
  const subscriber = spec.subscriber;
8091
- const subscriberReviews = subscriber.reviews ?? [];
8298
+ const subscriberReviews = requireCallerArray(
8299
+ subscriber.reviews,
8300
+ "build278: spec.subscriber.reviews",
8301
+ refuseHierarchy4
8302
+ );
8092
8303
  if (subscriberReviews.length === 0 && subscriber.dependent === void 0) {
8093
8304
  throw new ServicesReview278BuildError(
8094
8305
  AUTH_278_BUILD_ERROR_CODES.X12_278_BUILD_INVALID_HIERARCHY,
@@ -8101,14 +8312,19 @@ function enforceStructuralSpec7(spec, direction) {
8101
8312
  }
8102
8313
  const dependent = subscriber.dependent;
8103
8314
  if (dependent !== void 0) {
8104
- if (dependent.reviews.length === 0) {
8315
+ const dependentReviews = requireCallerArray(
8316
+ dependent.reviews,
8317
+ "build278: spec.subscriber.dependent.reviews",
8318
+ refuseHierarchy4
8319
+ );
8320
+ if (dependentReviews.length === 0) {
8105
8321
  throw new ServicesReview278BuildError(
8106
8322
  AUTH_278_BUILD_ERROR_CODES.X12_278_BUILD_INVALID_HIERARCHY,
8107
8323
  "build278: dependent has no review (HL level EV/SS) child."
8108
8324
  );
8109
8325
  }
8110
- for (let r = 0; r < dependent.reviews.length; r += 1) {
8111
- const review = dependent.reviews[r];
8326
+ for (let r = 0; r < dependentReviews.length; r += 1) {
8327
+ const review = dependentReviews[r];
8112
8328
  if (review !== void 0) enforceReview(review, `dependent.review[${String(r)}]`, direction);
8113
8329
  }
8114
8330
  }
@@ -8134,7 +8350,11 @@ function enforceReview(review, locator, direction) {
8134
8350
  );
8135
8351
  }
8136
8352
  }
8137
- const nested = review.reviews ?? [];
8353
+ const nested = requireCallerArray(
8354
+ review.reviews,
8355
+ `build278: review at ${locator}: reviews`,
8356
+ refuseHierarchy4
8357
+ );
8138
8358
  for (let r = 0; r < nested.length; r += 1) {
8139
8359
  const child = nested[r];
8140
8360
  if (child !== void 0) enforceReview(child, `${locator}.review[${String(r)}]`, direction);
@@ -9428,17 +9648,17 @@ function levenshtein(a, b) {
9428
9648
  function validateProfileName(opts) {
9429
9649
  if (opts === null || opts === void 0) {
9430
9650
  throw new X12ProfileError(
9431
- `defineProfile: options is required and must be an object. Received: ${String(opts)}.`
9651
+ `defineProfile: options is required and must be an object. Received: ${renderCallerJson(opts)}.`
9432
9652
  );
9433
9653
  }
9434
9654
  if (typeof opts.name !== "string") {
9435
9655
  throw new X12ProfileError(
9436
- `defineProfile: 'name' is required and must be a non-empty string. Received: ${JSON.stringify(opts.name)}.`
9656
+ `defineProfile: 'name' is required and must be a non-empty string. Received: ${renderCallerJson(opts.name)}.`
9437
9657
  );
9438
9658
  }
9439
9659
  if (opts.name.trim().length === 0) {
9440
9660
  throw new X12ProfileError(
9441
- `defineProfile: 'name' is required and must be a non-empty string. Received: ${JSON.stringify(opts.name)}.`,
9661
+ `defineProfile: 'name' is required and must be a non-empty string. Received: ${renderCallerJson(opts.name)}.`,
9442
9662
  opts.name
9443
9663
  );
9444
9664
  }
@@ -9454,7 +9674,7 @@ function validateOptionKeys(opts) {
9454
9674
  }
9455
9675
  }
9456
9676
  throw new X12ProfileError(
9457
- `Profile '${opts.name}' has unknown option key '${key}'. ` + (hint !== void 0 ? `Did you mean '${hint}'? ` : "") + `Known keys: ${KNOWN_OPTION_KEYS.join(", ")}.`,
9677
+ `Profile ${renderCallerValue(opts.name)} has unknown option key ${renderCallerValue(key)}. ` + (hint !== void 0 ? `Did you mean '${hint}'? ` : "") + `Known keys: ${KNOWN_OPTION_KEYS.join(", ")}.`,
9458
9678
  opts.name
9459
9679
  );
9460
9680
  }
@@ -9463,44 +9683,46 @@ function validateQuirks(quirks, profileName) {
9463
9683
  const seenIds = /* @__PURE__ */ new Set();
9464
9684
  for (let i = 0; i < quirks.length; i++) {
9465
9685
  const q = quirks[i];
9466
- const at = `quirks[${String(i)}]`;
9467
9686
  if (q === void 0 || q === null || typeof q !== "object") {
9468
- throw new X12ProfileError(`Profile '${profileName}' ${at} must be an object.`, profileName);
9687
+ throw new X12ProfileError(
9688
+ `Profile ${renderCallerValue(profileName)} quirks[${String(i)}] must be an object.`,
9689
+ profileName
9690
+ );
9469
9691
  }
9470
9692
  if (typeof q.id !== "string" || !QUIRK_ID_RE.test(q.id)) {
9471
9693
  throw new X12ProfileError(
9472
- `Profile '${profileName}' ${at}.id must be a kebab-case string (e.g. "payer-loop-ref-2u"). Received: ${JSON.stringify(q.id)}.`,
9694
+ `Profile ${renderCallerValue(profileName)} quirks[${String(i)}].id must be a kebab-case string (e.g. "payer-loop-ref-2u"). Received: ${renderCallerJson(q.id)}.`,
9473
9695
  profileName
9474
9696
  );
9475
9697
  }
9476
9698
  if (seenIds.has(q.id)) {
9477
9699
  throw new X12ProfileError(
9478
- `Profile '${profileName}' declares duplicate quirk id '${q.id}'. Each quirk id must be unique within a profile.`,
9700
+ `Profile ${renderCallerValue(profileName)} declares duplicate quirk id ${renderCallerValue(q.id)}. Each quirk id must be unique within a profile.`,
9479
9701
  profileName
9480
9702
  );
9481
9703
  }
9482
9704
  seenIds.add(q.id);
9483
9705
  if (typeof q.effect !== "string" || !KNOWN_EFFECTS.includes(q.effect)) {
9484
9706
  throw new X12ProfileError(
9485
- `Profile '${profileName}' quirk '${q.id}' has invalid effect ${JSON.stringify(q.effect)} - must be one of ${KNOWN_EFFECTS.join(" / ")}.`,
9707
+ `Profile ${renderCallerValue(profileName)} quirk ${renderCallerValue(q.id)} has invalid effect ${renderCallerJson(q.effect)} - must be one of ${KNOWN_EFFECTS.join(" / ")}.`,
9486
9708
  profileName
9487
9709
  );
9488
9710
  }
9489
9711
  if (typeof q.summary !== "string" || q.summary.trim().length === 0) {
9490
9712
  throw new X12ProfileError(
9491
- `Profile '${profileName}' quirk '${q.id}' must have a non-empty summary.`,
9713
+ `Profile ${renderCallerValue(profileName)} quirk ${renderCallerValue(q.id)} must have a non-empty summary.`,
9492
9714
  profileName
9493
9715
  );
9494
9716
  }
9495
9717
  if (typeof q.fixture !== "string" || !FIXTURE_PATH_RE.test(q.fixture)) {
9496
9718
  throw new X12ProfileError(
9497
- `Profile '${profileName}' quirk '${q.id}' must cite a 'fixture' - a relative path under test/fixtures/ (e.g. "remit/835-availity-quirk.edi") demonstrating the deviation. No invented quirks. Received: ${JSON.stringify(q.fixture)}.`,
9719
+ `Profile ${renderCallerValue(profileName)} quirk ${renderCallerValue(q.id)} must cite a 'fixture' - a relative path under test/fixtures/ (e.g. "remit/835-availity-quirk.edi") demonstrating the deviation. No invented quirks. Received: ${renderCallerJson(q.fixture)}.`,
9498
9720
  profileName
9499
9721
  );
9500
9722
  }
9501
9723
  if (typeof q.sourceCategory !== "string" || q.sourceCategory.trim().length === 0) {
9502
9724
  throw new X12ProfileError(
9503
- `Profile '${profileName}' quirk '${q.id}' must have a non-empty sourceCategory (where the deviation was observed).`,
9725
+ `Profile ${renderCallerValue(profileName)} quirk ${renderCallerValue(q.id)} must have a non-empty sourceCategory (where the deviation was observed).`,
9504
9726
  profileName
9505
9727
  );
9506
9728
  }
@@ -9508,7 +9730,7 @@ function validateQuirks(quirks, profileName) {
9508
9730
  for (const code of q.expectedWarnings) {
9509
9731
  if (!WARNING_CODE_SET.has(code)) {
9510
9732
  throw new X12ProfileError(
9511
- `Profile '${profileName}' quirk '${q.id}' lists unknown expected warning ${JSON.stringify(code)} - must be a member of WARNING_CODES.`,
9733
+ `Profile ${renderCallerValue(profileName)} quirk ${renderCallerValue(q.id)} lists unknown expected warning ${renderCallerJson(code)} - must be a member of WARNING_CODES.`,
9512
9734
  profileName
9513
9735
  );
9514
9736
  }
@@ -9670,7 +9892,7 @@ var profiles = Object.freeze({
9670
9892
  });
9671
9893
 
9672
9894
  // src/index.ts
9673
- var VERSION = "0.0.5";
9895
+ var VERSION = "0.0.7";
9674
9896
 
9675
9897
  exports.ACK_BUILD_ERROR_CODES = ACK_BUILD_ERROR_CODES;
9676
9898
  exports.ALL_WARNING_MESSAGES = ALL_WARNING_MESSAGES;