foldkit-form 0.1.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.
package/dist/index.mjs ADDED
@@ -0,0 +1,594 @@
1
+ import { Effect, Result, Schema } from "effect";
2
+ import { Bundle } from "foldkit-bundle";
3
+ import { Metadata } from "foldkit-metadata";
4
+ import * as FieldValidation from "foldkit/fieldValidation";
5
+ import { defineMessageUnion } from "foldkit/message";
6
+ //#region src/input.ts
7
+ /** The draft of a control that edits one value. A `Nested` key holds rows, not a draft. */
8
+ const draftKind = (control) => control._tag === "Toggle" ? "flag" : control._tag === "RelationMany" ? "list" : "text";
9
+ const key = Metadata.key("foldkit-form/input", {
10
+ merge: (controls) => controls.slice(-1),
11
+ summarize: (control) => control._tag
12
+ });
13
+ /** The schema with `null` and `undefined` set aside: optional text is still text. */
14
+ const present = (ast) => ast._tag === "Union" ? (ast.types ?? []).filter((member) => member._tag !== "Null" && member._tag !== "Undefined") : [ast];
15
+ const fromSchema = (schema) => {
16
+ const members = present(schema.ast);
17
+ const [only] = members;
18
+ if (members.length === 1 && only !== void 0) {
19
+ if (only._tag === "String") return { _tag: "Text" };
20
+ if (only._tag === "Number") return { _tag: "Number" };
21
+ if (only._tag === "Boolean") return { _tag: "Toggle" };
22
+ }
23
+ const literals = members.map((member) => member._tag === "Literal" ? member.literal : void 0);
24
+ return literals.length > 0 && literals.every((literal) => typeof literal === "string") ? {
25
+ _tag: "Select",
26
+ options: literals
27
+ } : void 0;
28
+ };
29
+ const Input = {
30
+ text: () => ({ _tag: "Text" }),
31
+ hidden: () => ({ _tag: "Hidden" }),
32
+ multiline: () => ({ _tag: "Multiline" }),
33
+ number: () => ({ _tag: "Number" }),
34
+ toggle: () => ({ _tag: "Toggle" }),
35
+ select: (options) => ({
36
+ _tag: "Select",
37
+ options
38
+ }),
39
+ /**
40
+ * Entity metadata: the control a member is edited with wherever it appears,
41
+ * e.g. `Entity.annotateMembers({ body: Input.of(Input.multiline()) })`.
42
+ */
43
+ of: (control) => key.of(control),
44
+ /**
45
+ * The control for one input key, from the most to the least explicit source:
46
+ * the member's `Input.of` metadata, the relation it writes, then the shape of
47
+ * its schema. `undefined` when none of them says, which a form reports rather
48
+ * than guessing.
49
+ */
50
+ resolve: (member, schema) => {
51
+ if (member._tag === "Unmapped") return fromSchema(schema);
52
+ if (member._tag === "NestedInput") return void 0;
53
+ const owner = member._tag === "Field" ? member : member.relation;
54
+ const [explicit] = key.get(owner.metadata);
55
+ if (explicit !== void 0) return explicit;
56
+ if (member._tag === "Field") return fromSchema(schema);
57
+ const target = member.relation.target();
58
+ return member.relation.cardinality === "many" ? {
59
+ _tag: "RelationMany",
60
+ target
61
+ } : {
62
+ _tag: "RelationOne",
63
+ target
64
+ };
65
+ }
66
+ };
67
+ //#endregion
68
+ //#region src/index.ts
69
+ /**
70
+ * `foldkit-form` — a form as a Bundle over Foldkit's own field validation.
71
+ *
72
+ * A form is built from an operation's input (`Entity.input`): the struct says
73
+ * what may be submitted, and the Entity says what each key means. The Model
74
+ * holds one `fieldValidation` Field per key, carrying the draft the control
75
+ * edits. `Submitted` leaves as an out Message with the decoded value; what
76
+ * happens to it (a Remote mutation, a Sync operation, a plain `update`) is the
77
+ * parent's.
78
+ */
79
+ const labelKey = Metadata.key("foldkit-form/label", {
80
+ merge: (labels) => labels.slice(-1),
81
+ summarize: (label) => label.label
82
+ });
83
+ /** Assigned once `Form` exists: a form builds the forms of its nested keys with itself. */
84
+ let makeNested;
85
+ /** An edit, however deep: it answers the last submit. An answered check or a blur does not. */
86
+ const isEdit = (message) => message._tag === "Nested" ? isEdit(message.message) : [
87
+ "Changed",
88
+ "RowAdded",
89
+ "RowRemoved",
90
+ "Reset"
91
+ ].includes(message._tag);
92
+ const kindOf = (draft) => typeof draft === "string" ? "text" : typeof draft === "boolean" ? "flag" : Array.isArray(draft) && draft.every((item) => typeof item === "string") ? "list" : void 0;
93
+ const sameDraft = (left, right) => Array.isArray(left) && Array.isArray(right) ? left.length === right.length && left.every((item, index) => item === right[index]) : left === right;
94
+ const isEmpty = (draft) => draft === "" || Array.isArray(draft) && draft.length === 0;
95
+ /** A number's draft is blank when it is only spaces: `Number(' ')` is 0, which nobody typed. */
96
+ const isBlank = (control, draft) => isEmpty(draft) || control._tag === "Number" && typeof draft === "string" && draft.trim() === "";
97
+ const annotationsOf = (schema) => Schema.resolveAnnotations(schema) ?? {};
98
+ /** The schema's `title`, else `Form.label` metadata on the member, else the key. */
99
+ const wordsOf = (key, schema, member) => {
100
+ const own = member._tag === "Unmapped" ? void 0 : member._tag === "Field" ? member : member.relation;
101
+ const [labelled] = own === void 0 ? [] : labelKey.get(own.metadata);
102
+ const annotated = [schema, ...member._tag === "Field" ? [member.schema] : []].map(annotationsOf);
103
+ const title = annotated.map((entry) => entry.title).find((value) => typeof value === "string");
104
+ const description = annotated.map((entry) => entry.description).find((value) => typeof value === "string");
105
+ return {
106
+ label: title ?? labelled?.label ?? key,
107
+ description: description ?? labelled?.description
108
+ };
109
+ };
110
+ const nestedPlanOf = (name, key, schema, member, options) => {
111
+ const accepts = Schema.is(Schema.toType(schema));
112
+ const cardinality = member.relation.cardinality;
113
+ const optional = cardinality === "many" || accepts(void 0) || accepts(null);
114
+ const form = makeNested(`${name}.${key}`, member.input, options);
115
+ return {
116
+ key,
117
+ ...wordsOf(key, schema, member),
118
+ control: {
119
+ _tag: "Nested",
120
+ cardinality,
121
+ optional,
122
+ form
123
+ },
124
+ required: !optional,
125
+ member,
126
+ form,
127
+ cardinality,
128
+ optional,
129
+ nothing: accepts(void 0) ? void 0 : null
130
+ };
131
+ };
132
+ const planOf = (name, key, schema, member, override, messages) => {
133
+ const control = override ?? Input.resolve(member, schema) ?? fail(name, `no control for "${key}"; name one under "inputs"`);
134
+ if (control._tag === "Nested") return fail(name, `"${key}" cannot be given a Nested control; map it with Relation.nested`);
135
+ const kind = draftKind(control);
136
+ const type = Schema.toType(schema);
137
+ const decode = Schema.decodeUnknownResult(type);
138
+ const accepts = Schema.is(type);
139
+ const empty = kind === "flag" ? false : kind === "list" ? [] : "";
140
+ const samples = kind === "flag" ? [true, false] : kind === "list" ? [[], ["id"]] : [];
141
+ if (samples.length > 0 && !samples.some(accepts)) fail(name, `"${key}" is edited as ${control._tag}, but its schema accepts no such value`);
142
+ const { label, description } = wordsOf(key, schema, member);
143
+ const field = {
144
+ key,
145
+ label,
146
+ control
147
+ };
148
+ const say = {
149
+ required: messages.required?.(field) ?? "Required",
150
+ notANumber: messages.notANumber?.(field) ?? "Enter a number",
151
+ invalid: (message) => messages.invalid?.(field, message) ?? message
152
+ };
153
+ const check = (draft) => {
154
+ if (isBlank(control, draft)) {
155
+ for (const nothing of [
156
+ void 0,
157
+ null,
158
+ empty
159
+ ]) if (accepts(nothing)) return Result.succeed(nothing);
160
+ return Result.fail(say.required);
161
+ }
162
+ const value = control._tag === "Number" ? Number(draft) : draft;
163
+ if (typeof value === "number" && !Number.isFinite(value)) return Result.fail(say.notANumber);
164
+ return Result.mapError(decode(value), (error) => say.invalid(error.message));
165
+ };
166
+ const required = kind !== "flag" && Result.isFailure(check(empty));
167
+ return {
168
+ key,
169
+ control,
170
+ kind,
171
+ empty,
172
+ check,
173
+ required,
174
+ member,
175
+ label,
176
+ description,
177
+ rules: FieldValidation.makeRules({
178
+ isEmpty: (draft) => isBlank(control, draft),
179
+ ...required ? { required: say.required } : {},
180
+ rules: [[(draft) => Result.isSuccess(check(draft)), (draft) => Result.match(check(draft), {
181
+ onSuccess: () => "",
182
+ onFailure: (error) => error
183
+ })]]
184
+ })
185
+ };
186
+ };
187
+ const fail = (name, message) => {
188
+ throw new Error(`Form "${name}": ${message}`);
189
+ };
190
+ /** The draft that shows `value`: a number as its text, nothing as the empty draft. */
191
+ const draftOf = (plan, value) => value === null || value === void 0 ? plan.empty : plan.kind === "text" ? String(value) : value;
192
+ const Form = {
193
+ /**
194
+ * A form for one operation's input. A key mapped with `Relation.nested` holds
195
+ * rows of a form of its own, built here from the nested input; every other key
196
+ * holds a draft. `options` names what the input cannot say: a control, the
197
+ * words, the checks.
198
+ */
199
+ make: (name, input, options = {}) => {
200
+ const members = input.members;
201
+ const everyKey = Object.keys(input.schema.fields);
202
+ const keys = everyKey.filter((key) => members[key]._tag !== "NestedInput");
203
+ const rowsKeys = everyKey.filter((key) => members[key]._tag === "NestedInput");
204
+ const overrides = options.inputs ?? {};
205
+ const plans = Object.fromEntries(keys.map((key) => [key, planOf(name, key, input.schema.fields[key], members[key], overrides[key], options.messages ?? {})]));
206
+ const nestedOptions = options.nested ?? {};
207
+ const nestedPlans = Object.fromEntries(rowsKeys.map((key) => [key, nestedPlanOf(name, key, input.schema.fields[key], members[key], {
208
+ messages: options.messages,
209
+ debounce: options.debounce,
210
+ ...nestedOptions[key]
211
+ })]));
212
+ const draftSchema = {
213
+ text: Schema.String,
214
+ flag: Schema.Boolean,
215
+ list: Schema.Array(Schema.String)
216
+ };
217
+ const Model = Schema.Struct({
218
+ fields: Schema.Struct(Object.fromEntries(keys.map((key) => [key, FieldValidation.Field(draftSchema[plans[key].kind])]))),
219
+ rows: Schema.Struct(Object.fromEntries(rowsKeys.map((key) => [key, Schema.Array(Schema.Struct({
220
+ id: Schema.String,
221
+ model: nestedPlans[key].form.bundle.Model
222
+ }))]))),
223
+ nextRow: Schema.Number,
224
+ errors: Schema.Array(Schema.String),
225
+ submitPending: Schema.Boolean
226
+ });
227
+ const KeySchema = keys.length === 0 ? Schema.Never : Schema.Literals(keys);
228
+ const DraftSchema = Schema.Union([
229
+ Schema.String,
230
+ Schema.Boolean,
231
+ Schema.Array(Schema.String)
232
+ ]);
233
+ const Message = defineMessageUnion({
234
+ Changed: {
235
+ key: KeySchema,
236
+ value: DraftSchema
237
+ },
238
+ /** Validates the key as it stands, so a required key left empty says so. */
239
+ Blurred: { key: KeySchema },
240
+ Submitted: {},
241
+ Reset: {},
242
+ /** The answer of a check for the draft it was asked about; one for an older draft is dropped. */
243
+ Checked: {
244
+ key: KeySchema,
245
+ draft: DraftSchema,
246
+ error: Schema.NullOr(Schema.String)
247
+ },
248
+ /** A Message of the nested form in one row of a nested key. One that row does not take is dropped. */
249
+ Nested: {
250
+ key: Schema.String,
251
+ row: Schema.String,
252
+ message: Schema.Unknown
253
+ },
254
+ /** A new, empty row. A `one` that already has its row takes no other. */
255
+ RowAdded: { key: Schema.String },
256
+ /** Removes a row. A `one` that must be there stays. */
257
+ RowRemoved: {
258
+ key: Schema.String,
259
+ row: Schema.String
260
+ }
261
+ });
262
+ const fieldsFrom = (draft) => Object.fromEntries(keys.map((key) => [key, draft(plans[key])]));
263
+ const rowsOf = (model) => model.rows;
264
+ const withRows = (model, key, rows) => ({
265
+ ...model,
266
+ rows: {
267
+ ...model.rows,
268
+ [key]: rows
269
+ }
270
+ });
271
+ /** Rows numbered from the Model's count, so an id is never reused. */
272
+ const numbered = (model, key, models) => ({
273
+ ...withRows(model, key, models.map((row, index) => ({
274
+ id: `r${model.nextRow + index}`,
275
+ model: row
276
+ }))),
277
+ nextRow: model.nextRow + models.length
278
+ });
279
+ const initial = rowsKeys.reduce((model, key) => numbered(model, key, nestedPlans[key].optional ? [] : [nestedPlans[key].form.initial]), {
280
+ fields: fieldsFrom((plan) => FieldValidation.NotValidated({ value: plan.empty })),
281
+ rows: {},
282
+ nextRow: 0,
283
+ errors: [],
284
+ submitPending: false
285
+ });
286
+ const drafts = (model) => model.fields;
287
+ const withField = (model, key, field) => ({
288
+ ...model,
289
+ fields: {
290
+ ...model.fields,
291
+ [key]: field
292
+ }
293
+ });
294
+ const checks = options.checks ?? {};
295
+ const debounce = options.debounce ?? "300 millis";
296
+ /** What currently decodes, by key: the context of a check, and the makings of the value. */
297
+ const decoded = (model) => Object.fromEntries(keys.flatMap((key) => {
298
+ const value = Result.getOrUndefined(plans[key].check(drafts(model)[key].value));
299
+ return value === void 0 ? [] : [[key, value]];
300
+ }));
301
+ /** The value of each nested key, or `undefined` while some row has none. */
302
+ const nestedValues = (model) => {
303
+ const entries = [];
304
+ for (const key of rowsKeys) {
305
+ const plan = nestedPlans[key];
306
+ const values = rowsOf(model)[key].map((row) => plan.form.engine.value(row.model));
307
+ if (values.includes(void 0)) return void 0;
308
+ entries.push([key, plan.cardinality === "many" ? values : values.length === 0 ? plan.nothing : values[0]]);
309
+ }
310
+ return Object.fromEntries(entries);
311
+ };
312
+ /** A nested form's Commands as this form's: their Messages arrive wrapped for the row. */
313
+ const lift = (key, row, commands) => commands.map((command) => ({
314
+ name: command.name,
315
+ args: {
316
+ ...command.args,
317
+ at: key,
318
+ row
319
+ },
320
+ effect: Effect.map(command.effect, (message) => Message.Nested({
321
+ key,
322
+ row,
323
+ message
324
+ }))
325
+ }));
326
+ /**
327
+ * Validates the draft of one key. A key with a check that passes its schema is
328
+ * not `Valid` yet: it is `Validating`, with the Command that asks.
329
+ */
330
+ const validateKey = (model, key, draft) => {
331
+ const state = FieldValidation.validate(plans[key].rules)(draft);
332
+ const check = checks[key];
333
+ if (check === void 0 || state._tag !== "Valid") return {
334
+ model: withField(model, key, state),
335
+ commands: []
336
+ };
337
+ const asking = withField(model, key, FieldValidation.Validating({ value: draft }));
338
+ const value = Result.getOrUndefined(plans[key].check(draft));
339
+ return {
340
+ model: asking,
341
+ commands: [{
342
+ name: `${name}.check`,
343
+ args: { key },
344
+ effect: Effect.sleep(debounce).pipe(Effect.andThen(check(value, { values: decoded(asking) })), Effect.map((error) => Message.Checked({
345
+ key,
346
+ draft,
347
+ error: error ?? null
348
+ })))
349
+ }]
350
+ };
351
+ };
352
+ const decodeInput = Schema.decodeUnknownResult(Schema.toType(input.schema));
353
+ const acceptable = (model) => keys.every((key) => FieldValidation.isValid(plans[key].rules)(drafts(model)[key]));
354
+ /** A check is running, in this form or in a row of it. */
355
+ const isValidating = (model) => keys.some((key) => drafts(model)[key]._tag === "Validating") || rowsKeys.some((key) => rowsOf(model)[key].some((row) => nestedPlans[key].form.engine.isValidating(row.model)));
356
+ /** The decoded input, once every key and every row is acceptable; else what is wrong with the whole. */
357
+ const finish = (model) => {
358
+ const settled = {
359
+ ...model,
360
+ submitPending: false
361
+ };
362
+ const nested = acceptable(model) ? nestedValues(model) : void 0;
363
+ if (nested === void 0) return { model: settled };
364
+ return Result.match(decodeInput({
365
+ ...decoded(model),
366
+ ...nested
367
+ }), {
368
+ onSuccess: (value) => ({
369
+ model: {
370
+ ...settled,
371
+ errors: []
372
+ },
373
+ value
374
+ }),
375
+ onFailure: (error) => ({ model: {
376
+ ...settled,
377
+ errors: [options.messages?.form?.(error.message) ?? error.message]
378
+ } })
379
+ });
380
+ };
381
+ /**
382
+ * A submit: every key not yet validated is, in every row too, so every failure
383
+ * shows. With none, the value goes out; with checks still running, the submit
384
+ * waits for them.
385
+ */
386
+ const submit = (model) => {
387
+ let next = {
388
+ ...model,
389
+ errors: [],
390
+ submitPending: false
391
+ };
392
+ const commands = [];
393
+ for (const key of keys) {
394
+ const state = drafts(next)[key];
395
+ if (state._tag !== "NotValidated") continue;
396
+ const validated = validateKey(next, key, state.value);
397
+ next = validated.model;
398
+ commands.push(...validated.commands);
399
+ }
400
+ for (const key of rowsKeys) {
401
+ const rows = rowsOf(next)[key].map((row) => {
402
+ const submitted = nestedPlans[key].form.engine.submit(row.model);
403
+ commands.push(...lift(key, row.id, submitted.commands));
404
+ return {
405
+ id: row.id,
406
+ model: submitted.model
407
+ };
408
+ });
409
+ next = withRows(next, key, rows);
410
+ }
411
+ if (isValidating(next)) return {
412
+ model: {
413
+ ...next,
414
+ submitPending: true
415
+ },
416
+ commands
417
+ };
418
+ return {
419
+ ...finish(next),
420
+ commands
421
+ };
422
+ };
423
+ const out = (model, commands, value) => value === void 0 ? {
424
+ model,
425
+ commands
426
+ } : {
427
+ model,
428
+ commands,
429
+ outMessage: {
430
+ _tag: "Submitted",
431
+ value
432
+ }
433
+ };
434
+ const submitted = (model) => {
435
+ const { model: next, commands, value } = submit(model);
436
+ return out(next, commands, value);
437
+ };
438
+ /** After an answer: a submit that was waiting goes out once nothing is asked any more. */
439
+ const resume = (model, commands = []) => {
440
+ if (!model.submitPending || isValidating(model)) return {
441
+ model,
442
+ commands
443
+ };
444
+ const { model: next, value } = finish(model);
445
+ return out(next, commands, value);
446
+ };
447
+ /** An edit answers the last submit: its failures describe a form that has changed. */
448
+ const edited = (model) => ({
449
+ ...model,
450
+ errors: [],
451
+ submitPending: false
452
+ });
453
+ /** Shows existing values, as an edit form does; keys not given keep their draft and their rows. */
454
+ const fill = (model, values) => {
455
+ const given = values;
456
+ const filled = {
457
+ ...model,
458
+ errors: [],
459
+ submitPending: false,
460
+ fields: fieldsFrom((plan) => plan.key in given ? FieldValidation.NotValidated({ value: draftOf(plan, given[plan.key]) }) : drafts(model)[plan.key])
461
+ };
462
+ return { model: rowsKeys.reduce((next, key) => {
463
+ if (!(key in given)) return next;
464
+ const { form, optional } = nestedPlans[key];
465
+ const held = given[key];
466
+ const models = (Array.isArray(held) ? held : held === null || held === void 0 ? [] : [held]).map((value) => form.fill(form.initial, value).model);
467
+ return numbered(next, key, models.length === 0 && !optional ? [form.initial] : models);
468
+ }, filled) };
469
+ };
470
+ return {
471
+ bundle: Bundle.make(name, {
472
+ Model,
473
+ Message,
474
+ init: () => ({ model: initial }),
475
+ update: (model, message) => {
476
+ switch (message._tag) {
477
+ case "Changed": return kindOf(message.value) !== plans[message.key].kind ? { model } : validateKey(edited(model), message.key, message.value);
478
+ case "Blurred": {
479
+ const state = drafts(model)[message.key];
480
+ return state._tag === "NotValidated" ? validateKey(model, message.key, state.value) : { model };
481
+ }
482
+ case "Checked": {
483
+ const state = drafts(model)[message.key];
484
+ if (state._tag !== "Validating" || !sameDraft(state.value, message.draft)) return { model };
485
+ return resume(withField(model, message.key, message.error === null ? FieldValidation.Valid({ value: state.value }) : FieldValidation.Invalid({
486
+ value: state.value,
487
+ errors: [message.error]
488
+ })));
489
+ }
490
+ case "Nested": {
491
+ const plan = nestedPlans[message.key];
492
+ const row = rowsOf(model)[message.key]?.find((held) => held.id === message.row);
493
+ if (plan === void 0 || row === void 0) return { model };
494
+ if (!Schema.is(plan.form.bundle.Message)(message.message)) return { model };
495
+ const inner = message.message;
496
+ if (inner._tag === "Submitted") return submitted(model);
497
+ const answered = plan.form.bundle.update(row.model, inner, void 0);
498
+ const next = withRows(model, message.key, rowsOf(model)[message.key].map((held) => held.id === row.id ? {
499
+ id: row.id,
500
+ model: answered.model
501
+ } : held));
502
+ return resume(isEdit(inner) ? edited(next) : next, lift(message.key, row.id, answered.commands ?? []));
503
+ }
504
+ case "RowAdded": {
505
+ const plan = nestedPlans[message.key];
506
+ const rows = rowsOf(model)[message.key];
507
+ if (plan === void 0 || rows === void 0) return { model };
508
+ if (plan.cardinality === "one" && rows.length > 0) return { model };
509
+ const added = numbered(edited(model), message.key, [plan.form.initial]);
510
+ return { model: withRows(added, message.key, [...rows, ...rowsOf(added)[message.key]]) };
511
+ }
512
+ case "RowRemoved": {
513
+ const plan = nestedPlans[message.key];
514
+ const rows = rowsOf(model)[message.key];
515
+ if (plan === void 0 || rows === void 0) return { model };
516
+ if (plan.cardinality === "one" && !plan.optional) return { model };
517
+ return { model: withRows(edited(model), message.key, rows.filter((held) => held.id !== message.row)) };
518
+ }
519
+ case "Reset": return { model: initial };
520
+ case "Submitted": return submitted(model);
521
+ }
522
+ },
523
+ helpers: { fill }
524
+ }),
525
+ Message,
526
+ /** The reading the form was made from, for `Entity.selectFor` and `Entity.valuesFor`. */
527
+ input,
528
+ /** The `fill` helper as a plain function of the form's Model, for a package that wraps the form. */
529
+ fill,
530
+ /** The Model the form starts from and resets to: every key empty and not validated. */
531
+ initial,
532
+ /** The keys in the input's order, each with its control, label, and member. */
533
+ controls: everyKey.map((key) => {
534
+ const { control, label, description, required, member } = plans[key] ?? nestedPlans[key];
535
+ return {
536
+ key,
537
+ control,
538
+ label,
539
+ description,
540
+ required,
541
+ member
542
+ };
543
+ }),
544
+ /**
545
+ * One key's state with its draft as any `Draft`, for a view that walks
546
+ * `controls`. `model.fields.title` is the same value, typed to that key. A
547
+ * nested key has rows and no draft.
548
+ */
549
+ field: (model, key) => drafts(model)[key] ?? fail(name, `"${key}" holds rows, not a draft; read it with rows`),
550
+ /** The rows of a nested key, each a Model of `control.form`. */
551
+ rows: (model, key) => rowsOf(model)[key] ?? fail(name, `"${key}" holds a draft, not rows`),
552
+ /**
553
+ * Whether a submit now could go through: nothing is invalid. A check still
554
+ * running does not stop it; the submit waits for the answer.
555
+ */
556
+ canSubmit: (model) => {
557
+ const next = submit(model);
558
+ return next.value !== void 0 || next.model.submitPending;
559
+ },
560
+ /** The form as the form that nests it drives it. */
561
+ engine: {
562
+ submit: (model) => {
563
+ const { model: next, commands } = submit(model);
564
+ return {
565
+ model: next,
566
+ commands
567
+ };
568
+ },
569
+ isValidating,
570
+ /** The decoded input when every key and row is valid as it stands; validates nothing. */
571
+ value: (model) => finish(model).value
572
+ }
573
+ };
574
+ },
575
+ /**
576
+ * Entity metadata: the label of a member that has no schema to annotate (a
577
+ * relation, a derived member). A field is labelled on its schema, with
578
+ * `Schema.String.annotate({ title })`.
579
+ */
580
+ label: (label, description) => labelKey.of(description === void 0 ? { label } : {
581
+ label,
582
+ description
583
+ }),
584
+ /**
585
+ * What `Form.label` attached to an Entity member, for a package that labels
586
+ * the same member elsewhere, such as a table column.
587
+ */
588
+ labelOf: (member) => labelKey.get(member.metadata)[0]?.label
589
+ };
590
+ makeNested = Form.make;
591
+ //#endregion
592
+ export { Form, Input };
593
+
594
+ //# sourceMappingURL=index.mjs.map