@cosyte/x12 0.0.5 → 0.0.6

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.mjs CHANGED
@@ -2461,8 +2461,55 @@ function coerce(value) {
2461
2461
  return "[unrenderable value]";
2462
2462
  }
2463
2463
  }
2464
+ function renderCallerJson(value) {
2465
+ let text;
2466
+ try {
2467
+ text = JSON.stringify(value) ?? "undefined";
2468
+ } catch {
2469
+ return "[unrenderable value]";
2470
+ }
2471
+ if (text.length <= BUILD_REFUSAL_VALUE_MAX_LENGTH) return text;
2472
+ return `${text.slice(0, BUILD_REFUSAL_VALUE_MAX_LENGTH)}\u2026 (${String(text.length)} characters)`;
2473
+ }
2474
+
2475
+ // src/builder/caller-array.ts
2476
+ var NONE = Object.freeze([]);
2477
+ function describeShape(value) {
2478
+ if (typeof value !== "object") {
2479
+ return `a ${typeof value} (${renderCallerValue(value)})`;
2480
+ }
2481
+ let length;
2482
+ try {
2483
+ length = value.length;
2484
+ } catch {
2485
+ return "an object whose 'length' getter threw";
2486
+ }
2487
+ if (length !== void 0) {
2488
+ return `an array-like object with length ${renderCallerValue(length)}`;
2489
+ }
2490
+ let tag;
2491
+ try {
2492
+ tag = Object.prototype.toString.call(value);
2493
+ } catch {
2494
+ tag = "[object Unknown]";
2495
+ }
2496
+ return `a non-array ${renderCallerValue(tag)}`;
2497
+ }
2498
+ function requireCallerArray(value, at, refuse) {
2499
+ if (value === void 0 || value === null) return NONE;
2500
+ if (!isRealArray(value)) {
2501
+ refuse(`${at} must be an array. Received ${describeShape(value)}.`);
2502
+ }
2503
+ return value;
2504
+ }
2505
+ function isRealArray(value) {
2506
+ return Array.isArray(value);
2507
+ }
2464
2508
 
2465
2509
  // src/transactions/remit/build-835.ts
2510
+ function refuseSpec(message) {
2511
+ throw new Remit835BuildError(REMIT_835_BUILD_ERROR_CODES.X12_835_BUILD_INVALID_SPEC, message);
2512
+ }
2466
2513
  var X221A1_VERSION_RELEASE = "005010X221A1";
2467
2514
  var X12_835_FUNCTIONAL_ID = "HP";
2468
2515
  var X12_AGENCY_CODE = "X";
@@ -2616,7 +2663,14 @@ function build835(spec) {
2616
2663
  emitClaim(claim, body, seg, esc, comp);
2617
2664
  }
2618
2665
  }
2619
- emitProviderAdjustments(spec.providerAdjustments ?? [], body, seg, esc, comp);
2666
+ emitProviderAdjustments(
2667
+ spec.providerAdjustments,
2668
+ "build835: spec.providerAdjustments",
2669
+ body,
2670
+ seg,
2671
+ esc,
2672
+ comp
2673
+ );
2620
2674
  const seCount = body.length + 2;
2621
2675
  const se = seg(["SE", String(seCount), esc(stControlNumber)]);
2622
2676
  const ge = seg(["GE", "1", esc(envelope.groupControlNumber)]);
@@ -2625,14 +2679,16 @@ function build835(spec) {
2625
2679
  return parseX12(raw);
2626
2680
  }
2627
2681
  function enforceStructuralSpec(spec) {
2628
- if (spec.traces.length === 0) {
2682
+ const traces = requireCallerArray(spec.traces, "build835: spec.traces", refuseSpec);
2683
+ if (traces.length === 0) {
2629
2684
  throw new Remit835BuildError(
2630
2685
  REMIT_835_BUILD_ERROR_CODES.X12_835_BUILD_INVALID_SPEC,
2631
2686
  "build835: at least one TRN trace is required (TR3 005010X221A1 Loop header)."
2632
2687
  );
2633
2688
  }
2634
- for (let i = 0; i < spec.claims.length; i += 1) {
2635
- const claim = spec.claims[i];
2689
+ const claims = requireCallerArray(spec.claims, "build835: spec.claims", refuseSpec);
2690
+ for (let i = 0; i < claims.length; i += 1) {
2691
+ const claim = claims[i];
2636
2692
  if (claim === void 0) continue;
2637
2693
  if (claim.patientControlNumber === "") {
2638
2694
  throw new Remit835BuildError(
@@ -2794,7 +2850,7 @@ function emitClaim(claim, body, seg, esc, comp) {
2794
2850
  comp([claim.facilityTypeCode ?? "", "", claim.claimFrequencyCode ?? ""])
2795
2851
  ])
2796
2852
  );
2797
- emitCasGroup(claim.adjustments ?? [], body, seg, esc);
2853
+ emitCasGroup(claim.adjustments, "build835: claim.adjustments", body, seg, esc);
2798
2854
  if (claim.patient !== void 0) body.push(emitPerson(claim.patient, seg, esc));
2799
2855
  if (claim.subscriber !== void 0) body.push(emitPerson(claim.subscriber, seg, esc));
2800
2856
  if (claim.correctedPatient !== void 0) body.push(emitPerson(claim.correctedPatient, seg, esc));
@@ -2840,7 +2896,7 @@ function emitServiceLine(line, body, seg, esc, comp) {
2840
2896
  ])
2841
2897
  );
2842
2898
  emitServiceLineDtm(line, body, seg, esc);
2843
- emitCasGroup(line.adjustments ?? [], body, seg, esc);
2899
+ emitCasGroup(line.adjustments, "build835: serviceLine.adjustments", body, seg, esc);
2844
2900
  for (const ref of line.references ?? []) body.push(emitRef(ref, seg, esc));
2845
2901
  for (const amt of line.amounts ?? []) {
2846
2902
  body.push(seg(["AMT", esc(amt.qualifier), esc(amt.amount.toString())]));
@@ -2858,7 +2914,8 @@ function emitServiceLineDtm(line, body, seg, esc) {
2858
2914
  if (start !== void 0) body.push(seg(["DTM", "150", esc(start)]));
2859
2915
  if (end !== void 0) body.push(seg(["DTM", "151", esc(end)]));
2860
2916
  }
2861
- function emitCasGroup(adjustments, body, seg, esc) {
2917
+ function emitCasGroup(raw, at, body, seg, esc) {
2918
+ const adjustments = requireCallerArray(raw, at, refuseSpec);
2862
2919
  let i = 0;
2863
2920
  while (i < adjustments.length) {
2864
2921
  const first = adjustments[i];
@@ -2916,7 +2973,8 @@ function emitProvider(provider, seg, esc) {
2916
2973
  esc(provider.idCode ?? "")
2917
2974
  ]);
2918
2975
  }
2919
- function emitProviderAdjustments(adjustments, body, seg, esc, comp) {
2976
+ function emitProviderAdjustments(raw, at, body, seg, esc, comp) {
2977
+ const adjustments = requireCallerArray(raw, at, refuseSpec);
2920
2978
  let i = 0;
2921
2979
  while (i < adjustments.length) {
2922
2980
  const first = adjustments[i];
@@ -4110,6 +4168,15 @@ var Claim837BuildError = class extends Error {
4110
4168
  };
4111
4169
 
4112
4170
  // src/transactions/claim/build-837.ts
4171
+ function refuseHierarchy(message) {
4172
+ throw new Claim837BuildError(
4173
+ CLAIM_837_BUILD_ERROR_CODES.X12_837_BUILD_INVALID_HIERARCHY,
4174
+ message
4175
+ );
4176
+ }
4177
+ function refuseSpec2(message) {
4178
+ throw new Claim837BuildError(CLAIM_837_BUILD_ERROR_CODES.X12_837_BUILD_INVALID_SPEC, message);
4179
+ }
4113
4180
  var VERSION_BY_VARIANT = {
4114
4181
  P: "005010X222A2",
4115
4182
  I: "005010X223A3",
@@ -4220,26 +4287,45 @@ function buildClaim837(variant, spec) {
4220
4287
  return parseX12(raw);
4221
4288
  }
4222
4289
  function enforceStructuralSpec2(variant, spec) {
4223
- if (spec.billingProviders.length === 0) {
4290
+ const billingProviders = requireCallerArray(
4291
+ spec.billingProviders,
4292
+ "build837: spec.billingProviders",
4293
+ refuseHierarchy
4294
+ );
4295
+ if (billingProviders.length === 0) {
4224
4296
  throw new Claim837BuildError(
4225
4297
  CLAIM_837_BUILD_ERROR_CODES.X12_837_BUILD_INVALID_HIERARCHY,
4226
4298
  "build837: at least one billing provider (HL level 20) is required."
4227
4299
  );
4228
4300
  }
4229
- for (let b = 0; b < spec.billingProviders.length; b += 1) {
4230
- const billing = spec.billingProviders[b];
4301
+ for (let b = 0; b < billingProviders.length; b += 1) {
4302
+ const billing = billingProviders[b];
4231
4303
  if (billing === void 0) continue;
4232
- if (billing.subscribers.length === 0) {
4304
+ const subscribers = requireCallerArray(
4305
+ billing.subscribers,
4306
+ `build837: spec.billingProviders[${String(b)}].subscribers`,
4307
+ refuseHierarchy
4308
+ );
4309
+ if (subscribers.length === 0) {
4233
4310
  throw new Claim837BuildError(
4234
4311
  CLAIM_837_BUILD_ERROR_CODES.X12_837_BUILD_INVALID_HIERARCHY,
4235
4312
  `build837: billing provider at index ${String(b)} has no subscriber (HL level 22) child.`
4236
4313
  );
4237
4314
  }
4238
- for (let s = 0; s < billing.subscribers.length; s += 1) {
4239
- const subscriber = billing.subscribers[s];
4315
+ for (let s = 0; s < subscribers.length; s += 1) {
4316
+ const subscriber = subscribers[s];
4240
4317
  if (subscriber === void 0) continue;
4241
- const directClaims = subscriber.claims ?? [];
4242
- const patients = subscriber.patients ?? [];
4318
+ const locator = `spec.billingProviders[${String(b)}].subscribers[${String(s)}]`;
4319
+ const directClaims = requireCallerArray(
4320
+ subscriber.claims,
4321
+ `build837: ${locator}.claims`,
4322
+ refuseHierarchy
4323
+ );
4324
+ const patients = requireCallerArray(
4325
+ subscriber.patients,
4326
+ `build837: ${locator}.patients`,
4327
+ refuseHierarchy
4328
+ );
4243
4329
  if (directClaims.length === 0 && patients.length === 0) {
4244
4330
  throw new Claim837BuildError(
4245
4331
  CLAIM_837_BUILD_ERROR_CODES.X12_837_BUILD_INVALID_HIERARCHY,
@@ -4259,14 +4345,19 @@ function enforceStructuralSpec2(variant, spec) {
4259
4345
  for (let p = 0; p < patients.length; p += 1) {
4260
4346
  const patient = patients[p];
4261
4347
  if (patient === void 0) continue;
4262
- if (patient.claims.length === 0) {
4348
+ const patientClaims = requireCallerArray(
4349
+ patient.claims,
4350
+ `build837: ${locator}.patients[${String(p)}].claims`,
4351
+ refuseHierarchy
4352
+ );
4353
+ if (patientClaims.length === 0) {
4263
4354
  throw new Claim837BuildError(
4264
4355
  CLAIM_837_BUILD_ERROR_CODES.X12_837_BUILD_INVALID_HIERARCHY,
4265
4356
  `build837: dependent patient at billing[${String(b)}].subscriber[${String(s)}].patient[${String(p)}] has no claim.`
4266
4357
  );
4267
4358
  }
4268
- for (let c = 0; c < patient.claims.length; c += 1) {
4269
- const claim = patient.claims[c];
4359
+ for (let c = 0; c < patientClaims.length; c += 1) {
4360
+ const claim = patientClaims[c];
4270
4361
  if (claim !== void 0) {
4271
4362
  enforceClaim(
4272
4363
  variant,
@@ -4286,14 +4377,19 @@ function enforceClaim(variant, claim, locator) {
4286
4377
  `build837: claim at ${locator} has an empty claimId (CLM-01 is required).`
4287
4378
  );
4288
4379
  }
4289
- if (claim.serviceLines.length === 0) {
4380
+ const serviceLines = requireCallerArray(
4381
+ claim.serviceLines,
4382
+ `build837: claim at ${locator}: serviceLines`,
4383
+ refuseSpec2
4384
+ );
4385
+ if (serviceLines.length === 0) {
4290
4386
  throw new Claim837BuildError(
4291
4387
  CLAIM_837_BUILD_ERROR_CODES.X12_837_BUILD_INVALID_SPEC,
4292
4388
  `build837: claim at ${locator} has no service line (a CLM requires at least one LX/SVx loop).`
4293
4389
  );
4294
4390
  }
4295
- for (let l = 0; l < claim.serviceLines.length; l += 1) {
4296
- const line = claim.serviceLines[l];
4391
+ for (let l = 0; l < serviceLines.length; l += 1) {
4392
+ const line = serviceLines[l];
4297
4393
  if (line === void 0) continue;
4298
4394
  if (line.variant !== variant) {
4299
4395
  throw new Claim837BuildError(
@@ -4523,12 +4619,13 @@ function emitAdjudication(adj, body, ctx) {
4523
4619
  adj.paidUnits === void 0 ? "" : ctx.esc(adj.paidUnits.toString())
4524
4620
  ])
4525
4621
  );
4526
- emitCasGroup2(adj.adjustments ?? [], body, ctx);
4622
+ emitCasGroup2(adj.adjustments, "build837: adjudication.adjustments", body, ctx);
4527
4623
  if (adj.dateAdjudicated !== void 0) {
4528
4624
  body.push(ctx.seg(["DTP", "573", "D8", ctx.esc(adj.dateAdjudicated)]));
4529
4625
  }
4530
4626
  }
4531
- function emitCasGroup2(adjustments, body, ctx) {
4627
+ function emitCasGroup2(raw, at, body, ctx) {
4628
+ const adjustments = requireCallerArray(raw, at, refuseSpec2);
4532
4629
  let i = 0;
4533
4630
  while (i < adjustments.length) {
4534
4631
  const first = adjustments[i];
@@ -5246,6 +5343,12 @@ var Eligibility271BuildError = class extends Error {
5246
5343
  };
5247
5344
 
5248
5345
  // src/transactions/eligibility/build-271.ts
5346
+ function refuseHierarchy2(message) {
5347
+ throw new Eligibility271BuildError(
5348
+ ELIGIBILITY_271_BUILD_ERROR_CODES.X12_271_BUILD_INVALID_HIERARCHY,
5349
+ message
5350
+ );
5351
+ }
5249
5352
  var X279A1_VERSION_RELEASE = "005010X279A1";
5250
5353
  var X12_271_FUNCTIONAL_ID = "HB";
5251
5354
  var X12_AGENCY_CODE3 = "X";
@@ -5323,25 +5426,40 @@ function build271(spec) {
5323
5426
  return parseX12(raw);
5324
5427
  }
5325
5428
  function enforceStructuralSpec3(spec) {
5326
- if (spec.informationSources.length === 0) {
5429
+ const sources = requireCallerArray(
5430
+ spec.informationSources,
5431
+ "build271: spec.informationSources",
5432
+ refuseHierarchy2
5433
+ );
5434
+ if (sources.length === 0) {
5327
5435
  throw new Eligibility271BuildError(
5328
5436
  ELIGIBILITY_271_BUILD_ERROR_CODES.X12_271_BUILD_INVALID_HIERARCHY,
5329
5437
  "build271: at least one information source (HL level 20) is required."
5330
5438
  );
5331
5439
  }
5332
- for (let s = 0; s < spec.informationSources.length; s += 1) {
5333
- const source = spec.informationSources[s];
5440
+ for (let s = 0; s < sources.length; s += 1) {
5441
+ const source = sources[s];
5334
5442
  if (source === void 0) continue;
5335
- if (source.receivers.length === 0) {
5443
+ const receivers = requireCallerArray(
5444
+ source.receivers,
5445
+ `build271: spec.informationSources[${String(s)}].receivers`,
5446
+ refuseHierarchy2
5447
+ );
5448
+ if (receivers.length === 0) {
5336
5449
  throw new Eligibility271BuildError(
5337
5450
  ELIGIBILITY_271_BUILD_ERROR_CODES.X12_271_BUILD_INVALID_HIERARCHY,
5338
5451
  `build271: information source at index ${String(s)} has no receiver (HL level 21) child.`
5339
5452
  );
5340
5453
  }
5341
- for (let r = 0; r < source.receivers.length; r += 1) {
5342
- const receiver = source.receivers[r];
5454
+ for (let r = 0; r < receivers.length; r += 1) {
5455
+ const receiver = receivers[r];
5343
5456
  if (receiver === void 0) continue;
5344
- if (receiver.subscribers.length === 0) {
5457
+ const subscribers = requireCallerArray(
5458
+ receiver.subscribers,
5459
+ `build271: spec.informationSources[${String(s)}].receivers[${String(r)}].subscribers`,
5460
+ refuseHierarchy2
5461
+ );
5462
+ if (subscribers.length === 0) {
5345
5463
  throw new Eligibility271BuildError(
5346
5464
  ELIGIBILITY_271_BUILD_ERROR_CODES.X12_271_BUILD_INVALID_HIERARCHY,
5347
5465
  `build271: receiver at source[${String(s)}].receiver[${String(r)}] has no subscriber (HL level 22) child.`
@@ -5917,6 +6035,18 @@ var ClaimStatus277BuildError = class extends Error {
5917
6035
  };
5918
6036
 
5919
6037
  // src/transactions/status/build-277.ts
6038
+ function refuseHierarchy3(message) {
6039
+ throw new ClaimStatus277BuildError(
6040
+ CLAIM_STATUS_277_BUILD_ERROR_CODES.X12_277_BUILD_INVALID_HIERARCHY,
6041
+ message
6042
+ );
6043
+ }
6044
+ function refuseSpec3(message) {
6045
+ throw new ClaimStatus277BuildError(
6046
+ CLAIM_STATUS_277_BUILD_ERROR_CODES.X12_277_BUILD_INVALID_SPEC,
6047
+ message
6048
+ );
6049
+ }
5920
6050
  var X12_277_FUNCTIONAL_ID = "HN";
5921
6051
  var X12_AGENCY_CODE4 = "X";
5922
6052
  var HL_LEVEL3 = {
@@ -6011,42 +6141,62 @@ function buildClaimStatus(version, spec) {
6011
6141
  return parseX12(raw);
6012
6142
  }
6013
6143
  function enforceStructuralSpec4(spec) {
6014
- if (spec.informationSources.length === 0) {
6144
+ const sources = requireCallerArray(
6145
+ spec.informationSources,
6146
+ "build277: spec.informationSources",
6147
+ refuseHierarchy3
6148
+ );
6149
+ if (sources.length === 0) {
6015
6150
  throw new ClaimStatus277BuildError(
6016
6151
  CLAIM_STATUS_277_BUILD_ERROR_CODES.X12_277_BUILD_INVALID_HIERARCHY,
6017
6152
  "build277: at least one information source (HL level 20) is required."
6018
6153
  );
6019
6154
  }
6020
- for (let s = 0; s < spec.informationSources.length; s += 1) {
6021
- const source = spec.informationSources[s];
6155
+ for (let s = 0; s < sources.length; s += 1) {
6156
+ const source = sources[s];
6022
6157
  if (source === void 0) continue;
6023
- if (source.receivers.length === 0) {
6158
+ const receivers = requireCallerArray(
6159
+ source.receivers,
6160
+ `build277: spec.informationSources[${String(s)}].receivers`,
6161
+ refuseHierarchy3
6162
+ );
6163
+ if (receivers.length === 0) {
6024
6164
  throw new ClaimStatus277BuildError(
6025
6165
  CLAIM_STATUS_277_BUILD_ERROR_CODES.X12_277_BUILD_INVALID_HIERARCHY,
6026
6166
  `build277: information source at index ${String(s)} has no receiver (HL level 21) child.`
6027
6167
  );
6028
6168
  }
6029
- for (let r = 0; r < source.receivers.length; r += 1) {
6030
- const receiver = source.receivers[r];
6169
+ for (let r = 0; r < receivers.length; r += 1) {
6170
+ const receiver = receivers[r];
6031
6171
  if (receiver === void 0) continue;
6032
- if (receiver.providers.length === 0) {
6172
+ const providers = requireCallerArray(
6173
+ receiver.providers,
6174
+ `build277: spec.informationSources[${String(s)}].receivers[${String(r)}].providers`,
6175
+ refuseHierarchy3
6176
+ );
6177
+ if (providers.length === 0) {
6033
6178
  throw new ClaimStatus277BuildError(
6034
6179
  CLAIM_STATUS_277_BUILD_ERROR_CODES.X12_277_BUILD_INVALID_HIERARCHY,
6035
6180
  `build277: receiver at source[${String(s)}].receiver[${String(r)}] has no provider (HL level 19) child.`
6036
6181
  );
6037
6182
  }
6038
- for (let p = 0; p < receiver.providers.length; p += 1) {
6039
- const provider = receiver.providers[p];
6183
+ for (let p = 0; p < providers.length; p += 1) {
6184
+ const provider = providers[p];
6040
6185
  if (provider === void 0) continue;
6041
6186
  const locator = `source[${String(s)}].receiver[${String(r)}].provider[${String(p)}]`;
6042
- if (provider.subscribers.length === 0) {
6187
+ const subscribers = requireCallerArray(
6188
+ provider.subscribers,
6189
+ `build277: ${locator}.subscribers`,
6190
+ refuseHierarchy3
6191
+ );
6192
+ if (subscribers.length === 0) {
6043
6193
  throw new ClaimStatus277BuildError(
6044
6194
  CLAIM_STATUS_277_BUILD_ERROR_CODES.X12_277_BUILD_INVALID_HIERARCHY,
6045
6195
  `build277: provider at ${locator} has no subscriber (HL level 22) child.`
6046
6196
  );
6047
6197
  }
6048
- for (let u = 0; u < provider.subscribers.length; u += 1) {
6049
- const subscriber = provider.subscribers[u];
6198
+ for (let u = 0; u < subscribers.length; u += 1) {
6199
+ const subscriber = subscribers[u];
6050
6200
  if (subscriber === void 0) continue;
6051
6201
  enforceSubscriber(subscriber, `${locator}.subscriber[${String(u)}]`);
6052
6202
  }
@@ -6055,8 +6205,16 @@ function enforceStructuralSpec4(spec) {
6055
6205
  }
6056
6206
  }
6057
6207
  function enforceSubscriber(subscriber, locator) {
6058
- const claims = subscriber.claims ?? [];
6059
- const dependents = subscriber.dependents ?? [];
6208
+ const claims = requireCallerArray(
6209
+ subscriber.claims,
6210
+ `build277: subscriber at ${locator}: claims`,
6211
+ refuseHierarchy3
6212
+ );
6213
+ const dependents = requireCallerArray(
6214
+ subscriber.dependents,
6215
+ `build277: subscriber at ${locator}: dependents`,
6216
+ refuseHierarchy3
6217
+ );
6060
6218
  if (claims.length === 0 && dependents.length === 0) {
6061
6219
  throw new ClaimStatus277BuildError(
6062
6220
  CLAIM_STATUS_277_BUILD_ERROR_CODES.X12_277_BUILD_INVALID_HIERARCHY,
@@ -6071,21 +6229,34 @@ function enforceSubscriber(subscriber, locator) {
6071
6229
  const dependent = dependents[d];
6072
6230
  if (dependent === void 0) continue;
6073
6231
  const depLocator = `${locator}.dependent[${String(d)}]`;
6074
- if (dependent.claims.length === 0) {
6232
+ const depClaims = requireCallerArray(
6233
+ dependent.claims,
6234
+ `build277: dependent at ${depLocator}: claims`,
6235
+ refuseHierarchy3
6236
+ );
6237
+ if (depClaims.length === 0) {
6075
6238
  throw new ClaimStatus277BuildError(
6076
6239
  CLAIM_STATUS_277_BUILD_ERROR_CODES.X12_277_BUILD_INVALID_HIERARCHY,
6077
6240
  `build277: dependent at ${depLocator} has no claim.`
6078
6241
  );
6079
6242
  }
6080
- for (let c = 0; c < dependent.claims.length; c += 1) {
6081
- const claim = dependent.claims[c];
6243
+ for (let c = 0; c < depClaims.length; c += 1) {
6244
+ const claim = depClaims[c];
6082
6245
  if (claim !== void 0) enforceClaim2(claim, `${depLocator}.claim[${String(c)}]`, c === 0);
6083
6246
  }
6084
6247
  }
6085
6248
  }
6086
6249
  function enforceClaim2(claim, locator, isFirst) {
6087
- const statuses = claim.statuses ?? [];
6088
- const serviceLines = claim.serviceLines ?? [];
6250
+ const statuses = requireCallerArray(
6251
+ claim.statuses,
6252
+ `build277: claim at ${locator}: statuses`,
6253
+ refuseSpec3
6254
+ );
6255
+ const serviceLines = requireCallerArray(
6256
+ claim.serviceLines,
6257
+ `build277: claim at ${locator}: serviceLines`,
6258
+ refuseSpec3
6259
+ );
6089
6260
  if (claim.trace === void 0 && statuses.length === 0 && serviceLines.length === 0) {
6090
6261
  throw new ClaimStatus277BuildError(
6091
6262
  CLAIM_STATUS_277_BUILD_ERROR_CODES.X12_277_BUILD_INVALID_SPEC,
@@ -6104,7 +6275,11 @@ function enforceClaim2(claim, locator, isFirst) {
6104
6275
  for (let l = 0; l < serviceLines.length; l += 1) {
6105
6276
  const line = serviceLines[l];
6106
6277
  if (line === void 0) continue;
6107
- const lineStatuses = line.statuses ?? [];
6278
+ const lineStatuses = requireCallerArray(
6279
+ line.statuses,
6280
+ `build277: claim at ${locator}.line[${String(l)}]: statuses`,
6281
+ refuseSpec3
6282
+ );
6108
6283
  for (let i = 0; i < lineStatuses.length; i += 1) {
6109
6284
  enforceStatus(lineStatuses[i], `${locator}.line[${String(l)}].status[${String(i)}]`);
6110
6285
  }
@@ -6654,6 +6829,9 @@ var Premium820BuildError = class extends Error {
6654
6829
  };
6655
6830
 
6656
6831
  // src/transactions/premium/build-820.ts
6832
+ function refuseSpec4(message) {
6833
+ throw new Premium820BuildError(PREMIUM_820_BUILD_ERROR_CODES.X12_820_BUILD_INVALID_SPEC, message);
6834
+ }
6657
6835
  var X218_VERSION_RELEASE = "005010X218";
6658
6836
  var X12_820_FUNCTIONAL_ID = "RA";
6659
6837
  var X12_AGENCY_CODE5 = "X";
@@ -6797,20 +6975,26 @@ function build820(spec) {
6797
6975
  return parseX12(raw);
6798
6976
  }
6799
6977
  function enforceStructuralSpec5(spec) {
6800
- if (spec.traces.length === 0) {
6978
+ const traces = requireCallerArray(spec.traces, "build820: spec.traces", refuseSpec4);
6979
+ if (traces.length === 0) {
6801
6980
  throw new Premium820BuildError(
6802
6981
  PREMIUM_820_BUILD_ERROR_CODES.X12_820_BUILD_INVALID_SPEC,
6803
6982
  "build820: at least one TRN trace is required (TR3 005010X218 header)."
6804
6983
  );
6805
6984
  }
6806
- if (spec.remittances.length === 0) {
6985
+ const remittances = requireCallerArray(
6986
+ spec.remittances,
6987
+ "build820: spec.remittances",
6988
+ refuseSpec4
6989
+ );
6990
+ if (remittances.length === 0) {
6807
6991
  throw new Premium820BuildError(
6808
6992
  PREMIUM_820_BUILD_ERROR_CODES.X12_820_BUILD_INVALID_SPEC,
6809
6993
  "build820: at least one remittance (Loop 2000) is required."
6810
6994
  );
6811
6995
  }
6812
- for (let r = 0; r < spec.remittances.length; r += 1) {
6813
- const remittance = spec.remittances[r];
6996
+ for (let r = 0; r < remittances.length; r += 1) {
6997
+ const remittance = remittances[r];
6814
6998
  if (remittance === void 0) continue;
6815
6999
  if (remittance.entity === void 0 && remittance.individual === void 0) {
6816
7000
  throw new Premium820BuildError(
@@ -6818,14 +7002,19 @@ function enforceStructuralSpec5(spec) {
6818
7002
  `build820: remittance at index ${String(r)} has neither an entity (ENT) nor an individual (NM1) to open its loop.`
6819
7003
  );
6820
7004
  }
6821
- if (remittance.openItems.length === 0) {
7005
+ const openItems = requireCallerArray(
7006
+ remittance.openItems,
7007
+ `build820: spec.remittances[${String(r)}].openItems`,
7008
+ refuseSpec4
7009
+ );
7010
+ if (openItems.length === 0) {
6822
7011
  throw new Premium820BuildError(
6823
7012
  PREMIUM_820_BUILD_ERROR_CODES.X12_820_BUILD_INVALID_SPEC,
6824
7013
  `build820: remittance at index ${String(r)} has no RMR open item.`
6825
7014
  );
6826
7015
  }
6827
- for (let o = 0; o < remittance.openItems.length; o += 1) {
6828
- const item = remittance.openItems[o];
7016
+ for (let o = 0; o < openItems.length; o += 1) {
7017
+ const item = openItems[o];
6829
7018
  if (item === void 0) continue;
6830
7019
  if (item.qualifier === "" && item.referenceId === "") {
6831
7020
  throw new Premium820BuildError(
@@ -7336,6 +7525,12 @@ var Enrollment834BuildError = class extends Error {
7336
7525
  };
7337
7526
 
7338
7527
  // src/transactions/enrollment/build-834.ts
7528
+ function refuseSpec5(message) {
7529
+ throw new Enrollment834BuildError(
7530
+ ENROLLMENT_834_BUILD_ERROR_CODES.X12_834_BUILD_INVALID_SPEC,
7531
+ message
7532
+ );
7533
+ }
7339
7534
  var X220A1_VERSION_RELEASE = "005010X220A1";
7340
7535
  var X12_834_FUNCTIONAL_ID = "BE";
7341
7536
  var X12_AGENCY_CODE6 = "X";
@@ -7435,14 +7630,15 @@ function build834(spec) {
7435
7630
  return parseX12(raw);
7436
7631
  }
7437
7632
  function enforceStructuralSpec6(spec) {
7438
- if (spec.members.length === 0) {
7633
+ const members = requireCallerArray(spec.members, "build834: spec.members", refuseSpec5);
7634
+ if (members.length === 0) {
7439
7635
  throw new Enrollment834BuildError(
7440
7636
  ENROLLMENT_834_BUILD_ERROR_CODES.X12_834_BUILD_INVALID_SPEC,
7441
7637
  "build834: at least one member loop (INS) is required."
7442
7638
  );
7443
7639
  }
7444
- for (let m = 0; m < spec.members.length; m += 1) {
7445
- const member = spec.members[m];
7640
+ for (let m = 0; m < members.length; m += 1) {
7641
+ const member = members[m];
7446
7642
  if (member === void 0) continue;
7447
7643
  if (member.maintenanceTypeCode === "") {
7448
7644
  throw new Enrollment834BuildError(
@@ -7456,8 +7652,13 @@ function enforceStructuralSpec6(spec) {
7456
7652
  `build834: member at index ${String(m)} has an unknown INS-03 maintenance type code ${renderCallerValue(member.maintenanceTypeCode)} (outside X12 Code Source 875).`
7457
7653
  );
7458
7654
  }
7459
- for (let c = 0; c < (member.healthCoverages ?? []).length; c += 1) {
7460
- const coverage = member.healthCoverages?.[c];
7655
+ const coverages = requireCallerArray(
7656
+ member.healthCoverages,
7657
+ `build834: spec.members[${String(m)}].healthCoverages`,
7658
+ refuseSpec5
7659
+ );
7660
+ for (let c = 0; c < coverages.length; c += 1) {
7661
+ const coverage = coverages[c];
7461
7662
  if (coverage === void 0) continue;
7462
7663
  const hd01 = coverage.maintenanceTypeCode;
7463
7664
  if (hd01 !== void 0 && hd01 !== "" && lookupMaintenanceType(hd01) === void 0) {
@@ -7975,6 +8176,12 @@ var ServicesReview278BuildError = class extends Error {
7975
8176
  };
7976
8177
 
7977
8178
  // src/transactions/auth/build-278.ts
8179
+ function refuseHierarchy4(message) {
8180
+ throw new ServicesReview278BuildError(
8181
+ AUTH_278_BUILD_ERROR_CODES.X12_278_BUILD_INVALID_HIERARCHY,
8182
+ message
8183
+ );
8184
+ }
7978
8185
  var X12_278_FUNCTIONAL_ID = "HI";
7979
8186
  var X12_AGENCY_CODE7 = "X";
7980
8187
  var HL_LEVEL4 = {
@@ -8086,7 +8293,11 @@ function buildServicesReview(version, direction, spec) {
8086
8293
  }
8087
8294
  function enforceStructuralSpec7(spec, direction) {
8088
8295
  const subscriber = spec.subscriber;
8089
- const subscriberReviews = subscriber.reviews ?? [];
8296
+ const subscriberReviews = requireCallerArray(
8297
+ subscriber.reviews,
8298
+ "build278: spec.subscriber.reviews",
8299
+ refuseHierarchy4
8300
+ );
8090
8301
  if (subscriberReviews.length === 0 && subscriber.dependent === void 0) {
8091
8302
  throw new ServicesReview278BuildError(
8092
8303
  AUTH_278_BUILD_ERROR_CODES.X12_278_BUILD_INVALID_HIERARCHY,
@@ -8099,14 +8310,19 @@ function enforceStructuralSpec7(spec, direction) {
8099
8310
  }
8100
8311
  const dependent = subscriber.dependent;
8101
8312
  if (dependent !== void 0) {
8102
- if (dependent.reviews.length === 0) {
8313
+ const dependentReviews = requireCallerArray(
8314
+ dependent.reviews,
8315
+ "build278: spec.subscriber.dependent.reviews",
8316
+ refuseHierarchy4
8317
+ );
8318
+ if (dependentReviews.length === 0) {
8103
8319
  throw new ServicesReview278BuildError(
8104
8320
  AUTH_278_BUILD_ERROR_CODES.X12_278_BUILD_INVALID_HIERARCHY,
8105
8321
  "build278: dependent has no review (HL level EV/SS) child."
8106
8322
  );
8107
8323
  }
8108
- for (let r = 0; r < dependent.reviews.length; r += 1) {
8109
- const review = dependent.reviews[r];
8324
+ for (let r = 0; r < dependentReviews.length; r += 1) {
8325
+ const review = dependentReviews[r];
8110
8326
  if (review !== void 0) enforceReview(review, `dependent.review[${String(r)}]`, direction);
8111
8327
  }
8112
8328
  }
@@ -8132,7 +8348,11 @@ function enforceReview(review, locator, direction) {
8132
8348
  );
8133
8349
  }
8134
8350
  }
8135
- const nested = review.reviews ?? [];
8351
+ const nested = requireCallerArray(
8352
+ review.reviews,
8353
+ `build278: review at ${locator}: reviews`,
8354
+ refuseHierarchy4
8355
+ );
8136
8356
  for (let r = 0; r < nested.length; r += 1) {
8137
8357
  const child = nested[r];
8138
8358
  if (child !== void 0) enforceReview(child, `${locator}.review[${String(r)}]`, direction);
@@ -9426,17 +9646,17 @@ function levenshtein(a, b) {
9426
9646
  function validateProfileName(opts) {
9427
9647
  if (opts === null || opts === void 0) {
9428
9648
  throw new X12ProfileError(
9429
- `defineProfile: options is required and must be an object. Received: ${String(opts)}.`
9649
+ `defineProfile: options is required and must be an object. Received: ${renderCallerJson(opts)}.`
9430
9650
  );
9431
9651
  }
9432
9652
  if (typeof opts.name !== "string") {
9433
9653
  throw new X12ProfileError(
9434
- `defineProfile: 'name' is required and must be a non-empty string. Received: ${JSON.stringify(opts.name)}.`
9654
+ `defineProfile: 'name' is required and must be a non-empty string. Received: ${renderCallerJson(opts.name)}.`
9435
9655
  );
9436
9656
  }
9437
9657
  if (opts.name.trim().length === 0) {
9438
9658
  throw new X12ProfileError(
9439
- `defineProfile: 'name' is required and must be a non-empty string. Received: ${JSON.stringify(opts.name)}.`,
9659
+ `defineProfile: 'name' is required and must be a non-empty string. Received: ${renderCallerJson(opts.name)}.`,
9440
9660
  opts.name
9441
9661
  );
9442
9662
  }
@@ -9452,7 +9672,7 @@ function validateOptionKeys(opts) {
9452
9672
  }
9453
9673
  }
9454
9674
  throw new X12ProfileError(
9455
- `Profile '${opts.name}' has unknown option key '${key}'. ` + (hint !== void 0 ? `Did you mean '${hint}'? ` : "") + `Known keys: ${KNOWN_OPTION_KEYS.join(", ")}.`,
9675
+ `Profile ${renderCallerValue(opts.name)} has unknown option key ${renderCallerValue(key)}. ` + (hint !== void 0 ? `Did you mean '${hint}'? ` : "") + `Known keys: ${KNOWN_OPTION_KEYS.join(", ")}.`,
9456
9676
  opts.name
9457
9677
  );
9458
9678
  }
@@ -9461,44 +9681,46 @@ function validateQuirks(quirks, profileName) {
9461
9681
  const seenIds = /* @__PURE__ */ new Set();
9462
9682
  for (let i = 0; i < quirks.length; i++) {
9463
9683
  const q = quirks[i];
9464
- const at = `quirks[${String(i)}]`;
9465
9684
  if (q === void 0 || q === null || typeof q !== "object") {
9466
- throw new X12ProfileError(`Profile '${profileName}' ${at} must be an object.`, profileName);
9685
+ throw new X12ProfileError(
9686
+ `Profile ${renderCallerValue(profileName)} quirks[${String(i)}] must be an object.`,
9687
+ profileName
9688
+ );
9467
9689
  }
9468
9690
  if (typeof q.id !== "string" || !QUIRK_ID_RE.test(q.id)) {
9469
9691
  throw new X12ProfileError(
9470
- `Profile '${profileName}' ${at}.id must be a kebab-case string (e.g. "payer-loop-ref-2u"). Received: ${JSON.stringify(q.id)}.`,
9692
+ `Profile ${renderCallerValue(profileName)} quirks[${String(i)}].id must be a kebab-case string (e.g. "payer-loop-ref-2u"). Received: ${renderCallerJson(q.id)}.`,
9471
9693
  profileName
9472
9694
  );
9473
9695
  }
9474
9696
  if (seenIds.has(q.id)) {
9475
9697
  throw new X12ProfileError(
9476
- `Profile '${profileName}' declares duplicate quirk id '${q.id}'. Each quirk id must be unique within a profile.`,
9698
+ `Profile ${renderCallerValue(profileName)} declares duplicate quirk id ${renderCallerValue(q.id)}. Each quirk id must be unique within a profile.`,
9477
9699
  profileName
9478
9700
  );
9479
9701
  }
9480
9702
  seenIds.add(q.id);
9481
9703
  if (typeof q.effect !== "string" || !KNOWN_EFFECTS.includes(q.effect)) {
9482
9704
  throw new X12ProfileError(
9483
- `Profile '${profileName}' quirk '${q.id}' has invalid effect ${JSON.stringify(q.effect)} - must be one of ${KNOWN_EFFECTS.join(" / ")}.`,
9705
+ `Profile ${renderCallerValue(profileName)} quirk ${renderCallerValue(q.id)} has invalid effect ${renderCallerJson(q.effect)} - must be one of ${KNOWN_EFFECTS.join(" / ")}.`,
9484
9706
  profileName
9485
9707
  );
9486
9708
  }
9487
9709
  if (typeof q.summary !== "string" || q.summary.trim().length === 0) {
9488
9710
  throw new X12ProfileError(
9489
- `Profile '${profileName}' quirk '${q.id}' must have a non-empty summary.`,
9711
+ `Profile ${renderCallerValue(profileName)} quirk ${renderCallerValue(q.id)} must have a non-empty summary.`,
9490
9712
  profileName
9491
9713
  );
9492
9714
  }
9493
9715
  if (typeof q.fixture !== "string" || !FIXTURE_PATH_RE.test(q.fixture)) {
9494
9716
  throw new X12ProfileError(
9495
- `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)}.`,
9717
+ `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)}.`,
9496
9718
  profileName
9497
9719
  );
9498
9720
  }
9499
9721
  if (typeof q.sourceCategory !== "string" || q.sourceCategory.trim().length === 0) {
9500
9722
  throw new X12ProfileError(
9501
- `Profile '${profileName}' quirk '${q.id}' must have a non-empty sourceCategory (where the deviation was observed).`,
9723
+ `Profile ${renderCallerValue(profileName)} quirk ${renderCallerValue(q.id)} must have a non-empty sourceCategory (where the deviation was observed).`,
9502
9724
  profileName
9503
9725
  );
9504
9726
  }
@@ -9506,7 +9728,7 @@ function validateQuirks(quirks, profileName) {
9506
9728
  for (const code of q.expectedWarnings) {
9507
9729
  if (!WARNING_CODE_SET.has(code)) {
9508
9730
  throw new X12ProfileError(
9509
- `Profile '${profileName}' quirk '${q.id}' lists unknown expected warning ${JSON.stringify(code)} - must be a member of WARNING_CODES.`,
9731
+ `Profile ${renderCallerValue(profileName)} quirk ${renderCallerValue(q.id)} lists unknown expected warning ${renderCallerJson(code)} - must be a member of WARNING_CODES.`,
9510
9732
  profileName
9511
9733
  );
9512
9734
  }
@@ -9668,7 +9890,7 @@ var profiles = Object.freeze({
9668
9890
  });
9669
9891
 
9670
9892
  // src/index.ts
9671
- var VERSION = "0.0.5";
9893
+ var VERSION = "0.0.6";
9672
9894
 
9673
9895
  export { ACK_BUILD_ERROR_CODES, ALL_WARNING_MESSAGES, AUTH_278_BUILD_ERROR_CODES, AUTH_278_LOOP_2000A, AUTH_278_LOOP_2000B, AUTH_278_LOOP_2000C, AUTH_278_LOOP_2000D, AUTH_278_LOOP_2000E, AUTH_278_LOOP_2000F, AckBuildError, BALANCE_INVARIANTS, BUILD_REFUSAL_VALUE_MAX_LENGTH, BUILD_REFUSAL_VALUE_MAX_RENDERED, CARC, CLAIM_837D_LOOP_2000A, CLAIM_837D_LOOP_2300, CLAIM_837D_LOOP_2400, CLAIM_837I_LOOP_2000A, CLAIM_837I_LOOP_2300, CLAIM_837I_LOOP_2400, CLAIM_837P_LOOP_2000A, CLAIM_837P_LOOP_2300, CLAIM_837P_LOOP_2400, CLAIM_837P_LOOP_2410, CLAIM_837_BUILD_ERROR_CODES, CLAIM_837_LOOP_1000A, CLAIM_837_LOOP_1000B, CLAIM_837_LOOP_2010AA, CLAIM_837_LOOP_2010BA, CLAIM_837_LOOP_2010BB, CLAIM_837_LOOP_2010CA, CLAIM_837_LOOP_2430, CLAIM_ADJUSTMENT_GROUP_CODES, CLAIM_STATUS_277_BUILD_ERROR_CODES, CLAIM_STATUS_CATEGORY_CODES, CLAIM_STATUS_CODES, CLP_STATUS, CONTROL_NUMBER_PAIRS, Claim837BuildError, ClaimStatus277BuildError, DELIMITER_POSITIONS, ELIGIBILITY_271_BUILD_ERROR_CODES, ELIGIBILITY_271_LOOP_2000A, ELIGIBILITY_271_LOOP_2000B, ELIGIBILITY_271_LOOP_2000C, ELIGIBILITY_271_LOOP_2000D, ELIGIBILITY_271_LOOP_2100C, ELIGIBILITY_271_LOOP_2100D, ELIGIBILITY_271_LOOP_2110, ENROLLMENT_834_BUILD_ERROR_CODES, ENROLLMENT_834_LOOP_1000A, ENROLLMENT_834_LOOP_1000B, ENROLLMENT_834_LOOP_2000, ENROLLMENT_834_LOOP_2100A, ENROLLMENT_834_LOOP_2300, ENROLLMENT_834_LOOP_2320, Eligibility271BuildError, Enrollment834BuildError, FATAL_CODES, HI_QUALIFIERS, HL_LEVEL_CODES, IK3_SYNTAX_ERROR_CODES, IK4_SYNTAX_ERROR_CODES, ISA_MIN_LENGTH, LoopSpecDefinitionError, MAINTENANCE_TYPE_CODES, NM1_QUALIFIERS, NON_SPEC_SEGMENT_ID, PREMIUM_820_BUILD_ERROR_CODES, PREMIUM_820_LOOP_1000A, PREMIUM_820_LOOP_1000B, PREMIUM_820_LOOP_2000A, PREMIUM_820_LOOP_2100A, PREMIUM_820_LOOP_2300A, PREMIUM_820_LOOP_2310A, Premium820BuildError, RARC, RELEASE_CHAR, REMIT_835_BUILD_ERROR_CODES, REMIT_835_LOOP_1000A, REMIT_835_LOOP_1000B, REMIT_835_LOOP_2000, REMIT_835_LOOP_2100, REMIT_835_LOOP_2110, REQUIRED_LOOPS, Remit835BuildError, SERVICE_TYPE_CODES, STATUS_277_LOOP_2000A, STATUS_277_LOOP_2000B, STATUS_277_LOOP_2000C, STATUS_277_LOOP_2000D, STATUS_277_LOOP_2000E, STATUS_277_LOOP_2200, STATUS_277_LOOP_2220, ServicesReview278BuildError, TA1_ACK_CODES, TA1_NOTE_CODES, UNEXPECTED_SEGMENT_CONTEXTS, VERSION, WARNING_CODES, X12BuildError, X12Decimal, X12ParseError, X12ProfileError, X12_ACK_DISPOSITION_CODES, X12_BUILD_ERROR_CODES, build271, build277, build277CA, build278Request, build278Response, build820, build834, build835, build837D, build837I, build837P, build999, buildInterchange, buildTA1, checkClaimBalance, checkRemitTotalBalance, checkServiceLineBalance, collectElementValues, componentOptional, controlNumberMismatch, danglingReleaseChar, decodeSegment, defineLoopSpec, defineProfile, detectDelimiters, elementDecimal, elementDecimalOrZero, elementOptional, elementValue, escapeRelease, get271Eligibility, get277CADisposition, get277Status, get278Request, get278Response, get820Payments, get834Enrollments, get834Header, get835, get837Claims, getAllSegmentValues, getDefaultProfile, getSegmentValue, groupCountMismatch, hlParentLevelInvalid, hlParentMismatch, isAcceptDisposition, isClaimAdjustmentGroupCode, isDiagnosisQualifier, isProcedureQualifier, lookupCarc, lookupClaimStatus, lookupClaimStatusCategory, lookupClpStatus, lookupMaintenanceType, lookupRarc, lookupServiceType, missingGe, missingIea, missingRequiredLoop, missingSe, parse999, parseTA1, parseX12, partitionWarnings, pre005010, profiles, remitBalanceMismatch, renderCallerValue, resolveHiQualifier, segmentCountMismatch, serializeX12, setDefaultProfile, trailingGarbage, transactionCountMismatch, unescapeRelease, unexpectedSegment, unknown837Variant, unknownCarc, unknownClaimStatus, unknownClaimStatusCategory, unknownHiQualifier, unknownMaintenanceType, unknownRarc };
9674
9896
  //# sourceMappingURL=index.mjs.map