asma-core-helpers 0.28.1 → 0.29.0

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.
@@ -6,7 +6,13 @@ import { IMPLEMENTED_ANSWER_OP_TYPES } from './answerOperations.js'
6
6
  import { IMPLEMENTED_OP_TYPES } from './operations.js'
7
7
  import { QUESTION_TYPES } from './questionTypes.js'
8
8
  import type { BindingTarget, QnrTemplateDocument } from './templateDocument.js'
9
- import { bindingTargetKey } from './templateDocument.js'
9
+ import {
10
+ BINDING_CARDINALITIES,
11
+ BINDING_ON_MANY,
12
+ BINDING_ON_MISSING,
13
+ BINDING_OPTION_DEFAULTS,
14
+ bindingTargetKey,
15
+ } from './templateDocument.js'
10
16
 
11
17
  /**
12
18
  * The ArkType schemas for the two hashed documents and both op vocabularies (plan TASK-003),
@@ -200,13 +206,24 @@ const bindingTargetSchema = type.or(
200
206
  type({ kind: '"gridColumn"', gridQuestionId: 'string', columnQuestionId: 'string' }),
201
207
  )
202
208
 
209
+ /**
210
+ * The three behaviour vocabularies, derived from the single declaration in `templateDocument.ts`
211
+ * rather than re-spelled — a second list of literals here could drift from the one the reducer and
212
+ * the UI branch on, and the drift would only show up as a validation refusal in production.
213
+ */
214
+ const bindingCardinalitySchema = type.enumerated(...BINDING_CARDINALITIES)
215
+ const bindingOnMissingSchema = type.enumerated(...BINDING_ON_MISSING)
216
+ const bindingOnManySchema = type.enumerated(...BINDING_ON_MANY)
217
+
203
218
  const mappingBindingSchema = type({
204
219
  nodeId: 'string',
205
220
  fieldId: 'string',
206
221
  target: bindingTargetSchema,
207
- cardinality: 'string?',
208
- onMissing: 'string?',
209
- onMany: 'string?',
222
+ // Closed sets, not `string?`: these compile into an immutable artifact, so a typo that validates
223
+ // here is a wrong prefill nobody can fix in place afterwards.
224
+ 'cardinality?': bindingCardinalitySchema,
225
+ 'onMissing?': bindingOnMissingSchema,
226
+ 'onMany?': bindingOnManySchema,
210
227
  ...({ '[string]': 'unknown' } as const),
211
228
  })
212
229
 
@@ -376,14 +393,23 @@ export const templateOpSchema = type.or(
376
393
  nodeId: 'string',
377
394
  fieldId: 'string',
378
395
  target: bindingTargetSchema,
396
+ 'cardinality?': bindingCardinalitySchema,
397
+ 'onMissing?': bindingOnMissingSchema,
398
+ 'onMany?': bindingOnManySchema,
379
399
  }),
380
400
  type({
381
401
  type: '"mappingBinding.update"',
382
402
  bindingId: 'string',
403
+ // `| null` on the three behaviours only: the op layer is where the absent-vs-cleared
404
+ // tri-state lives (operations.ts header), so returning a behaviour to its default is
405
+ // expressible without making "leave it alone" and "reset it" the same patch.
383
406
  patch: type({
384
407
  nodeId: 'string?',
385
408
  fieldId: 'string?',
386
- "target?": bindingTargetSchema,
409
+ 'target?': bindingTargetSchema,
410
+ 'cardinality?': bindingCardinalitySchema.or(type.null),
411
+ 'onMissing?': bindingOnMissingSchema.or(type.null),
412
+ 'onMany?': bindingOnManySchema.or(type.null),
387
413
  }),
388
414
  }),
389
415
  type({ type: '"mappingBinding.delete"', bindingId: 'string' }),
@@ -605,8 +631,26 @@ const TEMPLATE_DOCUMENT_DEFAULT_PATHS: readonly string[] = [
605
631
  'timestamps', // question.grid (legacy composite `row_timestamps`)
606
632
  ]
607
633
 
608
- export const templateDocumentIsDefault: IsDefault = (path, value) =>
609
- value === false && TEMPLATE_DOCUMENT_DEFAULT_PATHS.some((name) => path.endsWith(`.${name}`))
634
+ /**
635
+ * The binding behaviours, whose default is a **value** rather than `false` and whose path must
636
+ * therefore be matched exactly rather than by suffix.
637
+ *
638
+ * Anchored on `mappingBindingsById.<id>.<key>` on purpose: a mapping **node** may also carry a
639
+ * `cardinality`, and its default is the catalog relation's — not a literal. A suffix rule would
640
+ * read a node's deliberately-narrowed `cardinality: '0..1'` over a `0..*` relation as an omittable
641
+ * default and drop the one member that made the node take a single row.
642
+ */
643
+ const BINDING_OPTION_DEFAULT_PATH = /^mappingBindingsById\.[^.]+\.(cardinality|onMissing|onMany)$/
644
+
645
+ export const templateDocumentIsDefault: IsDefault = (path, value) => {
646
+ const option = BINDING_OPTION_DEFAULT_PATH.exec(path)?.[1]
647
+
648
+ if (option !== undefined) {
649
+ return value === BINDING_OPTION_DEFAULTS[option as keyof typeof BINDING_OPTION_DEFAULTS]
650
+ }
651
+
652
+ return value === false && TEMPLATE_DOCUMENT_DEFAULT_PATHS.some((name) => path.endsWith(`.${name}`))
653
+ }
610
654
 
611
655
  /**
612
656
  * The instance-side half of the default lint: every present-and-default field on a stored
@@ -266,16 +266,84 @@ export const bindingTargetKey = (target: BindingTarget): string =>
266
266
  ? `question:${target.questionId}`
267
267
  : `gridColumn:${target.gridQuestionId}:${target.columnQuestionId}`
268
268
 
269
+ /**
270
+ * A binding's expected cardinality (§13.1). Closed rather than a loose string because the compiler
271
+ * branches on it: `0..1`/`1` fill one slot, `0..*`/`1..*` feed a grid's rows, and a typo would compile
272
+ * into an artifact that is immutable forever.
273
+ *
274
+ * `0..1` is the default, so DOC-LAW-2 means it is **never stored** — a binding that takes at most one
275
+ * value carries no `cardinality` key at all.
276
+ *
277
+ * @see asma-modules/_docs/editor/qnrs/cross/2026-07-15-20-38-analysis-qnr-external-data-mapping-options.md:840 — the four values
278
+ */
279
+ export const BINDING_CARDINALITIES = ['0..1', '1', '0..*', '1..*'] as const
280
+ export type BindingCardinality = (typeof BINDING_CARDINALITIES)[number]
281
+
282
+ /**
283
+ * What the runtime does when the source yields **no** value for a binding. `omit` leaves the question
284
+ * unfilled (the default); `error` makes the absence a prefill failure the author wants surfaced rather
285
+ * than silently tolerated.
286
+ */
287
+ export const BINDING_ON_MISSING = ['omit', 'error'] as const
288
+ export type BindingOnMissing = (typeof BINDING_ON_MISSING)[number]
289
+
290
+ /**
291
+ * What the runtime does when the source yields **many** values for a binding expecting one. `error` is
292
+ * the default — an ambiguous prefill is a real defect — and `first` is the deliberate opt-in, which is
293
+ * why the node's `orderBy` must make "first" deterministic before it means anything.
294
+ */
295
+ export const BINDING_ON_MANY = ['error', 'first'] as const
296
+ export type BindingOnMany = (typeof BINDING_ON_MANY)[number]
297
+
298
+ /**
299
+ * The three binding behaviours and their defaults, in one place because two rules depend on it: the
300
+ * reducer omits a chosen value that equals its default (DOC-LAW-2 — a key present with its own default
301
+ * is a second encoding of "not set", and the hash is computed on the minimal form), and every reader
302
+ * hydrates the absent key back to the same value.
303
+ *
304
+ * @see asma-modules/_docs/editor/qnrs/cross/2026-07-15-20-38-analysis-qnr-external-data-mapping-options.md:832 — absent because they equal their defaults
305
+ */
306
+ /** The binding behaviours a reader sees — total, because the stored document is minimal. */
307
+ export type ResolvedBindingOptions = {
308
+ cardinality: BindingCardinality
309
+ onMissing: BindingOnMissing
310
+ onMany: BindingOnMany
311
+ }
312
+
313
+ export const BINDING_OPTION_DEFAULTS = {
314
+ cardinality: '0..1',
315
+ onMissing: 'omit',
316
+ onMany: 'error',
317
+ } as const satisfies ResolvedBindingOptions
318
+
269
319
  export type MappingBinding = {
270
320
  nodeId: NodeId
271
321
  fieldId: string
272
322
  target: BindingTarget
273
- cardinality?: string
274
- onMissing?: string
275
- onMany?: string
323
+ /** Absent means `0..1` (BINDING_OPTION_DEFAULTS) — never stored at its default. */
324
+ cardinality?: BindingCardinality
325
+ /** Absent means `omit`. */
326
+ onMissing?: BindingOnMissing
327
+ /** Absent means `error`. */
328
+ onMany?: BindingOnMany
276
329
  [key: string]: unknown
277
330
  }
278
331
 
332
+ /**
333
+ * Hydrates a stored binding's behaviours to the total set (§2.2a: "the hydrated in-memory binding is
334
+ * a total object"). One place rather than `?? 'omit'` at each use site — a reader that spelled the
335
+ * fallback itself would be a second declaration of the default, free to drift from the reducer's.
336
+ */
337
+ export const resolveBindingOptions = (binding: MappingBinding): ResolvedBindingOptions => ({
338
+ cardinality: binding.cardinality ?? BINDING_OPTION_DEFAULTS.cardinality,
339
+ onMissing: binding.onMissing ?? BINDING_OPTION_DEFAULTS.onMissing,
340
+ onMany: binding.onMany ?? BINDING_OPTION_DEFAULTS.onMany,
341
+ })
342
+
343
+ /** Whether a binding's cardinality means "many values", i.e. it feeds a grid's rows rather than one slot. */
344
+ export const bindingIsMultiValued = (binding: MappingBinding): boolean =>
345
+ resolveBindingOptions(binding).cardinality.endsWith('*')
346
+
279
347
  /** Carries no `nodeId` back-pointer: the node already lists it (§2.2a). */
280
348
  export type MappingFilter = {
281
349
  fieldId: string