@composable-os/sdk 0.1.0-beta.11 → 0.1.0-beta.14

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.
Files changed (104) hide show
  1. package/dist/acknowledge.d.ts +38 -0
  2. package/dist/acknowledge.js +30 -0
  3. package/dist/agreements.d.ts +80 -0
  4. package/dist/agreements.js +109 -0
  5. package/dist/allowance.d.ts +47 -5
  6. package/dist/allowance.js +59 -4
  7. package/dist/automation-check.d.ts +6 -1
  8. package/dist/automation-check.js +93 -8
  9. package/dist/automation.d.ts +62 -4
  10. package/dist/automation.js +32 -9
  11. package/dist/behaviour.js +25 -6
  12. package/dist/calendar-entry.d.ts +97 -0
  13. package/dist/calendar-entry.js +180 -0
  14. package/dist/capacity.js +1 -2
  15. package/dist/checklists.d.ts +41 -0
  16. package/dist/checklists.js +75 -0
  17. package/dist/contract.d.ts +495 -26
  18. package/dist/contract.js +21 -3
  19. package/dist/discount.d.ts +135 -0
  20. package/dist/discount.js +281 -0
  21. package/dist/document.d.ts +2 -1
  22. package/dist/document.js +2 -1
  23. package/dist/field-types.d.ts +1 -1
  24. package/dist/field-types.js +5 -0
  25. package/dist/formula/compile.d.ts +15 -0
  26. package/dist/formula/compile.js +9 -0
  27. package/dist/index.d.ts +17 -0
  28. package/dist/index.js +17 -0
  29. package/dist/intake.d.ts +134 -0
  30. package/dist/intake.js +239 -0
  31. package/dist/journey-signals.d.ts +89 -0
  32. package/dist/journey-signals.js +151 -0
  33. package/dist/manifest.d.ts +944 -56
  34. package/dist/manifest.js +1036 -60
  35. package/dist/mine.d.ts +45 -0
  36. package/dist/mine.js +119 -0
  37. package/dist/occupies.d.ts +64 -0
  38. package/dist/occupies.js +122 -0
  39. package/dist/questions.d.ts +179 -0
  40. package/dist/questions.js +514 -0
  41. package/dist/recurring.d.ts +33 -4
  42. package/dist/recurring.js +50 -3
  43. package/dist/restricted-fields.d.ts +15 -0
  44. package/dist/restricted-fields.js +97 -0
  45. package/dist/rich-text.d.ts +22 -0
  46. package/dist/rich-text.js +91 -0
  47. package/dist/routine-mail.d.ts +400 -0
  48. package/dist/routine-mail.js +955 -0
  49. package/dist/runtime.d.ts +5 -11
  50. package/dist/runtime.js +21 -11
  51. package/dist/screens.d.ts +3 -0
  52. package/dist/screens.js +74 -0
  53. package/dist/sellable.d.ts +153 -2
  54. package/dist/sellable.js +188 -0
  55. package/dist/slug.d.ts +2 -0
  56. package/dist/slug.js +6 -2
  57. package/dist/staffed.d.ts +178 -0
  58. package/dist/staffed.js +287 -0
  59. package/dist/states.d.ts +17 -1
  60. package/dist/states.js +48 -20
  61. package/dist/stock.d.ts +129 -0
  62. package/dist/stock.js +205 -0
  63. package/dist/team.d.ts +28 -0
  64. package/dist/team.js +52 -0
  65. package/dist/testing.d.ts +22 -1
  66. package/dist/testing.js +47 -5
  67. package/dist/worked-on.d.ts +42 -0
  68. package/dist/worked-on.js +79 -0
  69. package/package.json +9 -1
  70. package/src/acknowledge.ts +100 -0
  71. package/src/agreements.ts +185 -0
  72. package/src/allowance.ts +98 -8
  73. package/src/automation-check.ts +120 -9
  74. package/src/automation.ts +78 -10
  75. package/src/behaviour.ts +26 -6
  76. package/src/calendar-entry.ts +258 -0
  77. package/src/capacity.ts +1 -2
  78. package/src/checklists.ts +118 -0
  79. package/src/contract.ts +489 -27
  80. package/src/discount.ts +408 -0
  81. package/src/document.ts +4 -2
  82. package/src/field-types.ts +5 -0
  83. package/src/formula/compile.ts +10 -0
  84. package/src/index.ts +17 -0
  85. package/src/intake.ts +380 -0
  86. package/src/journey-signals.ts +215 -0
  87. package/src/manifest.ts +1236 -65
  88. package/src/mine.ts +161 -0
  89. package/src/occupies.ts +192 -0
  90. package/src/questions.ts +681 -0
  91. package/src/recurring.ts +90 -9
  92. package/src/restricted-fields.ts +129 -0
  93. package/src/rich-text.ts +112 -0
  94. package/src/routine-mail.ts +1272 -0
  95. package/src/runtime.ts +25 -12
  96. package/src/screens.ts +91 -0
  97. package/src/sellable.ts +344 -0
  98. package/src/slug.ts +7 -2
  99. package/src/staffed.ts +474 -0
  100. package/src/states.ts +68 -26
  101. package/src/stock.ts +332 -0
  102. package/src/team.ts +83 -0
  103. package/src/testing.ts +70 -6
  104. package/src/worked-on.ts +134 -0
@@ -0,0 +1,514 @@
1
+ // The question schema (stage 29, step 1): what a form asks, as a document field (ADR 0445) with
2
+ // one fixed schema the platform owns, as rich text is. A form's questions field is one:
3
+ //
4
+ // questions: { type: "document", label: "Questions", schema: QUESTIONS_SCHEMA,
5
+ // maxBytes: QUESTIONS_MAX_BYTES }
6
+ //
7
+ // The value is `{ items: [...] }`, at most 60 items told apart by `kind`, each with a stable `key`
8
+ // (camelCase, unique in the form). The questions: `text` (short, or `long` for several lines, with
9
+ // a maximum length), `email`, `phone`, `number` (min, max, whole), `choice` (single or `multiple`,
10
+ // up to 50 options), `date`, `rating` (`stars`, 1 to 5, or `ten`, 0 to 10), `yesNo`, and `consent`
11
+ // (a tick box: its exact `words`, its `purpose`: contact, marketing or other). The non-questions:
12
+ // `heading` and `paragraph` (rich text's inline runs). There is no health kind, no signature and no
13
+ // payment: those are Agreements' (stage 44) and orders'. A `file` kind is added with the trades work.
14
+ //
15
+ // Any item may be shown only if one earlier `choice` or `yesNo` has a value (`showIf`), one level
16
+ // deep: the item it names has no `showIf` of its own. A question may `map` to the submitter's Person
17
+ // (name from a short text, email from an email, phone from a phone) or to a field of the form's
18
+ // destination object; each target is filled from one question at most. A marketing consent line is
19
+ // never required (consent is not a condition of being heard).
20
+ //
21
+ // Two checks. `checkQuestions` is the document check plus what a schema cannot say (keys, earlier
22
+ // items, maps, options). `checkAnswers` checks what a person sent against one version's questions
23
+ // and gives the answers document a submission stores (`ANSWERS_SCHEMA`), in the questions' order.
24
+ // Its faults name the question's key and never the value, so they can go in an error or an audit
25
+ // row. It never repairs a value: a client trims and converts before it sends.
26
+ import { z } from "zod";
27
+ import { DOCUMENT_MAX_ISSUES, documentBytes, validateDocument, } from "./document.js";
28
+ import { RICH_TEXT_SCHEMA } from "./rich-text.js";
29
+ /** The kinds of item, in the order a builder lists them. */
30
+ export const QUESTION_KINDS = [
31
+ "text",
32
+ "email",
33
+ "phone",
34
+ "number",
35
+ "choice",
36
+ "date",
37
+ "rating",
38
+ "yesNo",
39
+ "consent",
40
+ "heading",
41
+ "paragraph",
42
+ ];
43
+ /** The kinds that ask nothing: no answer is given to them. */
44
+ export const QUESTION_DISPLAY_KINDS = ["heading", "paragraph"];
45
+ /** What a consent line is for. Only `marketing` lines are "keep me updated". */
46
+ export const CONSENT_PURPOSES = ["contact", "marketing", "other"];
47
+ /** The Person's fields a question may fill (only where empty: the host's rule, ADR 0721). */
48
+ export const QUESTION_PERSON_FIELDS = ["name", "email", "phone"];
49
+ /** Items in a form, at most. */
50
+ export const QUESTIONS_MAX_ITEMS = 60;
51
+ /** Options of a choice, at most. */
52
+ export const QUESTION_MAX_OPTIONS = 50;
53
+ /** The most a questions value may be, in bytes of its JSON. */
54
+ export const QUESTIONS_MAX_BYTES = 262_144;
55
+ /** A short text's longest answer, and its default. */
56
+ export const SHORT_TEXT_MAX = 500;
57
+ export const SHORT_TEXT_DEFAULT = 200;
58
+ /** A long text's longest answer, and its default. */
59
+ export const LONG_TEXT_MAX = 5000;
60
+ /** Links in any one text answer, at most. */
61
+ export const ANSWER_MAX_LINKS = 3;
62
+ /** The most a submission's answers may be, in bytes of their JSON. */
63
+ export const ANSWERS_MAX_BYTES = 20_480;
64
+ const KEY_RE = /^[a-z][a-zA-Z0-9]{0,39}$/;
65
+ // Control characters other than tab and newline (and a lone carriage return) are refused.
66
+ // biome-ignore lint/suspicious/noControlCharactersInRegex: control characters are what it finds.
67
+ const CONTROL_RE = /[\u0000-\u0008\u000b-\u001f\u007f]/;
68
+ // What counts as a link in an answer: an address with a scheme, or one starting `www.`.
69
+ const LINK_RE = /\b(?:[a-z][a-z0-9+.-]*:\/\/|www\.)/gi;
70
+ const PHONE_RE = /^\+?[0-9 ().-]{3,40}$/;
71
+ const DATE_RE = /^(\d{4})-(\d{2})-(\d{2})$/;
72
+ const emailShape = z.email().max(320);
73
+ // --- The schema ----------------------------------------------------------------------------------
74
+ const KEY_CHARS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
75
+ const keyNode = {
76
+ type: "text",
77
+ min: 1,
78
+ max: 40,
79
+ chars: KEY_CHARS,
80
+ description: "The item's stable key in the form (camelCase): answers are kept by it",
81
+ };
82
+ const prose = (max, extra = {}) => ({ type: "text", max, noMarkup: true, ...extra });
83
+ const optional = (node) => ({ ...node, optional: true });
84
+ const flag = (description) => ({
85
+ type: "boolean",
86
+ optional: true,
87
+ description,
88
+ });
89
+ const object = (fields) => ({
90
+ type: "object",
91
+ fields,
92
+ });
93
+ /** What every item has: its key and the one condition that shows it. */
94
+ const shown = { key: keyNode, showIf: optional({ type: "use", def: "showIf" }) };
95
+ /** What every question has besides: its label, help, whether it is required and where it lands. */
96
+ const asked = (mapped) => ({
97
+ ...shown,
98
+ label: prose(300, { min: 1, description: "The question, as the person reads it" }),
99
+ help: optional(prose(500, { multiline: true, description: "A line under the question" })),
100
+ required: flag("The form is not sent without an answer (when the question is shown)"),
101
+ ...(mapped ? { map: optional({ type: "use", def: "map" }) } : {}),
102
+ });
103
+ const items = {
104
+ text: object({
105
+ ...asked(true),
106
+ long: flag("Several lines (up to 5,000 characters) rather than one (up to 500)"),
107
+ max: optional({ type: "number", integer: true, min: 1, max: LONG_TEXT_MAX }),
108
+ }),
109
+ email: object(asked(true)),
110
+ phone: object(asked(true)),
111
+ number: object({
112
+ ...asked(true),
113
+ min: optional({ type: "number" }),
114
+ max: optional({ type: "number" }),
115
+ integer: flag("Whole numbers only"),
116
+ }),
117
+ choice: object({
118
+ ...asked(true),
119
+ options: {
120
+ type: "array",
121
+ min: 1,
122
+ max: QUESTION_MAX_OPTIONS,
123
+ of: prose(200, { min: 1 }),
124
+ description: "What the person chooses from; an answer is one of these words",
125
+ },
126
+ multiple: flag("Several options may be chosen"),
127
+ }),
128
+ date: object(asked(true)),
129
+ rating: object({
130
+ ...asked(true),
131
+ scale: {
132
+ type: "choice",
133
+ options: ["stars", "ten"],
134
+ description: "stars: 1 to 5; ten: 0 to 10",
135
+ },
136
+ }),
137
+ yesNo: object(asked(true)),
138
+ consent: object({
139
+ ...shown,
140
+ words: prose(1000, {
141
+ min: 1,
142
+ multiline: true,
143
+ description: "The exact words beside the tick box, kept with every answer to this version",
144
+ }),
145
+ purpose: { type: "choice", options: CONSENT_PURPOSES },
146
+ required: flag("The form is not sent unless it is ticked (never for marketing)"),
147
+ }),
148
+ heading: object({ ...shown, text: prose(200, { min: 1 }) }),
149
+ paragraph: object({ ...shown, content: { type: "use", def: "inlines" } }),
150
+ };
151
+ /** The one schema of every form's questions. */
152
+ export const QUESTIONS_SCHEMA = {
153
+ root: object({
154
+ items: {
155
+ type: "array",
156
+ max: QUESTIONS_MAX_ITEMS,
157
+ unique: "key",
158
+ of: { type: "union", tag: "kind", variants: items },
159
+ },
160
+ }),
161
+ defs: {
162
+ // Rich text's inline runs, so a paragraph is drawn as any rich text is.
163
+ inlines: RICH_TEXT_SCHEMA.defs.inlines,
164
+ showIf: object({
165
+ question: { ...keyNode, description: "The key of an earlier choice or yes/no" },
166
+ is: prose(200, { min: 1, description: "One of its options, or yes or no" }),
167
+ }),
168
+ map: {
169
+ type: "union",
170
+ tag: "to",
171
+ variants: {
172
+ person: object({ field: { type: "choice", options: QUESTION_PERSON_FIELDS } }),
173
+ destination: object({
174
+ field: { ...keyNode, description: "A field the destination's intake lists" },
175
+ }),
176
+ },
177
+ },
178
+ },
179
+ };
180
+ /**
181
+ * What a submission stores: the answers, in the questions' order, each with its key and kind.
182
+ * Private: a customer or the public never reads it, not even their own.
183
+ */
184
+ export const ANSWERS_SCHEMA = {
185
+ root: {
186
+ type: "object",
187
+ private: true,
188
+ fields: {
189
+ answers: {
190
+ type: "array",
191
+ max: QUESTIONS_MAX_ITEMS,
192
+ unique: "key",
193
+ of: {
194
+ type: "union",
195
+ tag: "kind",
196
+ variants: {
197
+ text: object({
198
+ key: keyNode,
199
+ value: { type: "text", max: LONG_TEXT_MAX, multiline: true },
200
+ }),
201
+ email: object({ key: keyNode, value: { type: "text", max: 320 } }),
202
+ phone: object({
203
+ key: keyNode,
204
+ value: { type: "text", max: 40, chars: "+0123456789 ().-" },
205
+ }),
206
+ number: object({ key: keyNode, value: { type: "number" } }),
207
+ choice: object({
208
+ key: keyNode,
209
+ values: {
210
+ type: "array",
211
+ min: 1,
212
+ max: QUESTION_MAX_OPTIONS,
213
+ of: { type: "text", max: 200 },
214
+ },
215
+ }),
216
+ date: object({
217
+ key: keyNode,
218
+ value: { type: "text", min: 10, max: 10, chars: "0123456789-" },
219
+ }),
220
+ rating: object({
221
+ key: keyNode,
222
+ value: { type: "number", integer: true, min: 0, max: 10 },
223
+ }),
224
+ yesNo: object({ key: keyNode, value: { type: "boolean" } }),
225
+ consent: object({ key: keyNode, value: { type: "boolean" } }),
226
+ },
227
+ },
228
+ },
229
+ },
230
+ },
231
+ };
232
+ /** The text answers a person map takes from each kind. */
233
+ const PERSON_MAP_KINDS = {
234
+ name: "text",
235
+ email: "email",
236
+ phone: "phone",
237
+ };
238
+ /**
239
+ * Checks a form's questions: the document schema, then what it cannot say. Every fault (up to the
240
+ * document check's limit) with its path from the root (`items.3.showIf.question`).
241
+ */
242
+ export function checkQuestions(value, options = {}) {
243
+ const checked = validateDocument(QUESTIONS_SCHEMA, value, { maxBytes: QUESTIONS_MAX_BYTES });
244
+ if (!checked.ok)
245
+ return checked;
246
+ const questions = value;
247
+ const issues = [];
248
+ const fault = (path, message) => {
249
+ if (issues.length < DOCUMENT_MAX_ISSUES)
250
+ issues.push({ path, message });
251
+ };
252
+ const before = new Map();
253
+ const mapped = new Map();
254
+ questions.items.forEach((item, i) => {
255
+ const at = ["items", i];
256
+ if (!KEY_RE.test(item.key))
257
+ fault([...at, "key"], "A key is camelCase: a lowercase letter first");
258
+ if (item.showIf) {
259
+ const target = before.get(item.showIf.question);
260
+ const why = target ? showIfFault(target, item.showIf.is) : "names no earlier item";
261
+ if (why)
262
+ fault([...at, "showIf"], `"Show only if" ${why}`);
263
+ }
264
+ switch (item.kind) {
265
+ case "text":
266
+ if (item.max !== undefined && !item.long && item.max > SHORT_TEXT_MAX) {
267
+ fault([...at, "max"], `A short text takes at most ${SHORT_TEXT_MAX} characters`);
268
+ }
269
+ break;
270
+ case "number":
271
+ if (item.min !== undefined && item.max !== undefined && item.min > item.max) {
272
+ fault([...at, "min"], "min is above max");
273
+ }
274
+ break;
275
+ case "choice":
276
+ if (new Set(item.options).size !== item.options.length) {
277
+ fault([...at, "options"], "Each option is listed once");
278
+ }
279
+ break;
280
+ case "consent":
281
+ if (item.purpose === "marketing" && item.required) {
282
+ fault([...at, "required"], "A marketing consent line is never required");
283
+ }
284
+ break;
285
+ }
286
+ const map = "map" in item ? item.map : undefined;
287
+ if (map) {
288
+ const why = mapFault(item, map, options);
289
+ const target = `${map.to}.${map.field}`;
290
+ if (why)
291
+ fault([...at, "map"], why);
292
+ else if (mapped.has(target))
293
+ fault([...at, "map"], `${target} is filled by ${mapped.get(target)} already`);
294
+ else
295
+ mapped.set(target, item.key);
296
+ }
297
+ before.set(item.key, item);
298
+ });
299
+ if (issues.length)
300
+ return { ok: false, issues };
301
+ return { ok: true, questions, bytes: checked.bytes };
302
+ }
303
+ function showIfFault(target, is) {
304
+ if (target.showIf)
305
+ return "names an item that is itself shown only if (one level only)";
306
+ if (target.kind === "yesNo")
307
+ return is === "yes" || is === "no" ? null : "of a yes/no is yes or no";
308
+ if (target.kind === "choice") {
309
+ return target.options.includes(is) ? null : `names no option of ${target.key}`;
310
+ }
311
+ return "names a choice or a yes/no";
312
+ }
313
+ function mapFault(item, map, options) {
314
+ if (map.to === "person") {
315
+ if (options.anonymous)
316
+ return "An anonymous form keeps no Person, so nothing maps to one";
317
+ const kind = PERSON_MAP_KINDS[map.field];
318
+ if (item.kind !== kind || (item.kind === "text" && item.long)) {
319
+ return `The Person's ${map.field} is filled from ${kind === "text" ? "a short text" : `an ${kind}`} question`;
320
+ }
321
+ return null;
322
+ }
323
+ if (options.anonymous)
324
+ return "An anonymous form has no destination";
325
+ if (!options.destinationFields)
326
+ return "The form has no destination";
327
+ if (!options.destinationFields.includes(map.field)) {
328
+ return `The destination's intake does not list "${map.field}"`;
329
+ }
330
+ if (item.kind === "choice" && item.multiple)
331
+ return "A choice of several fills no destination field";
332
+ return null;
333
+ }
334
+ /** Whether a value counts as no answer: left out, null, empty text or no option chosen. */
335
+ function unanswered(value) {
336
+ return (value === undefined || value === null || value === "" || (Array.isArray(value) && !value.length));
337
+ }
338
+ /**
339
+ * Whether an item is shown, given the answers so far: it has no "show only if", or the earlier
340
+ * item it names has that value (a choice of several: among those chosen). The headless entry and
341
+ * `checkAnswers` both read it.
342
+ */
343
+ export function isQuestionShown(item, questions, input) {
344
+ if (!item.showIf)
345
+ return true;
346
+ const { question, is } = item.showIf;
347
+ const target = questions.items.find((q) => q.key === question);
348
+ const value = Object.hasOwn(input, question) ? input[question] : undefined;
349
+ if (target?.kind === "yesNo")
350
+ return value === (is === "yes");
351
+ if (target?.kind === "choice")
352
+ return Array.isArray(value) ? value.includes(is) : value === is;
353
+ return false;
354
+ }
355
+ /** Links in a text answer. */
356
+ export function countLinks(text) {
357
+ return text.match(LINK_RE)?.length ?? 0;
358
+ }
359
+ /**
360
+ * Checks what a person sent (`{ [key]: value }`) against one version's questions, already checked
361
+ * by `checkQuestions`: every fault, or the answers document to store. An item that is not shown
362
+ * takes no answer; a question left unanswered is not stored; a consent line left unticked may be
363
+ * sent as false.
364
+ */
365
+ export function checkAnswers(questions, input) {
366
+ const isRecord = typeof input === "object" && input !== null && !Array.isArray(input);
367
+ if (!isRecord) {
368
+ return {
369
+ ok: false,
370
+ issues: [{ key: null, fault: "shape", message: "Answers are an object of question keys" }],
371
+ };
372
+ }
373
+ const given = input;
374
+ const issues = [];
375
+ const fault = (key, kind, message) => {
376
+ if (issues.length < DOCUMENT_MAX_ISSUES)
377
+ issues.push({ key, fault: kind, message });
378
+ };
379
+ const byKey = new Map(questions.items.map((item) => [item.key, item]));
380
+ for (const key of Object.keys(given)) {
381
+ const item = byKey.get(key);
382
+ if (!item || item.kind === "heading" || item.kind === "paragraph") {
383
+ fault(null, "unknown", "An answer names no question of this form");
384
+ }
385
+ }
386
+ const answers = [];
387
+ for (const item of questions.items) {
388
+ if (item.kind === "heading" || item.kind === "paragraph")
389
+ continue;
390
+ const value = Object.hasOwn(given, item.key) ? given[item.key] : undefined;
391
+ if (!isQuestionShown(item, questions, given)) {
392
+ if (!unanswered(value))
393
+ fault(item.key, "hidden", "This question was not shown");
394
+ continue;
395
+ }
396
+ if (unanswered(value)) {
397
+ if (item.required)
398
+ fault(item.key, "required", "An answer is required");
399
+ continue;
400
+ }
401
+ const answer = checkAnswer(item, value, (kind, message) => {
402
+ fault(item.key, kind, message);
403
+ return null;
404
+ });
405
+ if (answer)
406
+ answers.push(answer);
407
+ }
408
+ if (issues.length)
409
+ return { ok: false, issues };
410
+ const document = { answers };
411
+ const bytes = documentBytes(document);
412
+ if (bytes > ANSWERS_MAX_BYTES) {
413
+ return {
414
+ ok: false,
415
+ issues: [
416
+ {
417
+ key: null,
418
+ fault: "size",
419
+ message: `The answers are more than ${ANSWERS_MAX_BYTES} bytes`,
420
+ },
421
+ ],
422
+ };
423
+ }
424
+ // The stored shape is the platform's: a fault here is a fault in this file.
425
+ const stored = validateDocument(ANSWERS_SCHEMA, document, { maxBytes: ANSWERS_MAX_BYTES });
426
+ if (!stored.ok)
427
+ throw new Error("checkAnswers built answers its own schema refuses");
428
+ return { ok: true, answers: document, bytes };
429
+ }
430
+ function checkAnswer(item, value, fault) {
431
+ const { key } = item;
432
+ const text = (max, multiline) => {
433
+ if (typeof value !== "string")
434
+ return fault("type", "Expected text");
435
+ if (value.length > max)
436
+ return fault("length", `At most ${max} characters`);
437
+ if (CONTROL_RE.test(value))
438
+ return fault("type", "Control characters are not allowed");
439
+ if (!multiline && /[\r\n]/.test(value))
440
+ return fault("type", "One line only");
441
+ if (countLinks(value) > ANSWER_MAX_LINKS) {
442
+ return fault("links", `At most ${ANSWER_MAX_LINKS} links`);
443
+ }
444
+ return value;
445
+ };
446
+ switch (item.kind) {
447
+ case "text": {
448
+ const max = Math.min(item.max ?? (item.long ? LONG_TEXT_MAX : SHORT_TEXT_DEFAULT), item.long ? LONG_TEXT_MAX : SHORT_TEXT_MAX);
449
+ const v = text(max, item.long === true);
450
+ return v === null ? null : { kind: "text", key, value: v };
451
+ }
452
+ case "email": {
453
+ if (typeof value !== "string" || !emailShape.safeParse(value).success) {
454
+ return fault("type", "Expected an email address");
455
+ }
456
+ return { kind: "email", key, value };
457
+ }
458
+ case "phone": {
459
+ if (typeof value !== "string" ||
460
+ !PHONE_RE.test(value) ||
461
+ (value.match(/[0-9]/g)?.length ?? 0) < 5) {
462
+ return fault("type", "Expected a phone number");
463
+ }
464
+ return { kind: "phone", key, value };
465
+ }
466
+ case "number": {
467
+ if (typeof value !== "number" || !Number.isFinite(value))
468
+ return fault("type", "Expected a number");
469
+ if (item.integer && !Number.isInteger(value))
470
+ return fault("type", "Expected a whole number");
471
+ if (item.min !== undefined && value < item.min)
472
+ return fault("type", `At least ${item.min}`);
473
+ if (item.max !== undefined && value > item.max)
474
+ return fault("type", `At most ${item.max}`);
475
+ return { kind: "number", key, value };
476
+ }
477
+ case "choice": {
478
+ const values = item.multiple ? value : [value];
479
+ if (!Array.isArray(values) ||
480
+ values.some((v) => typeof v !== "string" || !item.options.includes(v)) ||
481
+ new Set(values).size !== values.length) {
482
+ return fault("type", item.multiple ? "Expected some of the options, each once" : "Expected one of the options");
483
+ }
484
+ return { kind: "choice", key, values: values };
485
+ }
486
+ case "date": {
487
+ const match = typeof value === "string" ? DATE_RE.exec(value) : null;
488
+ const [y, m, d] = match ? [Number(match[1]), Number(match[2]), Number(match[3])] : [0, 0, 0];
489
+ const day = new Date(Date.UTC(y, m - 1, d));
490
+ if (!match ||
491
+ day.getUTCFullYear() !== y ||
492
+ day.getUTCMonth() !== m - 1 ||
493
+ day.getUTCDate() !== d) {
494
+ return fault("type", "Expected a date (YYYY-MM-DD)");
495
+ }
496
+ return { kind: "date", key, value: value };
497
+ }
498
+ case "rating": {
499
+ const [min, max] = item.scale === "stars" ? [1, 5] : [0, 10];
500
+ if (typeof value !== "number" || !Number.isInteger(value) || value < min || value > max) {
501
+ return fault("type", `Expected a whole number from ${min} to ${max}`);
502
+ }
503
+ return { kind: "rating", key, value };
504
+ }
505
+ case "yesNo":
506
+ case "consent": {
507
+ if (typeof value !== "boolean")
508
+ return fault("type", "Expected true or false");
509
+ if (item.kind === "consent" && item.required && !value)
510
+ return fault("required", "This must be ticked");
511
+ return { kind: item.kind, key, value };
512
+ }
513
+ }
514
+ }
@@ -3,12 +3,17 @@ import type { ExtensionManifest, ObjectManifest } from "./manifest.ts";
3
3
  /** The weekday values a rule's `weekdays` field must use (ISO order, Monday first). */
4
4
  export declare const WEEKDAYS: readonly ["mon", "tue", "wed", "thu", "fri", "sat", "sun"];
5
5
  export type Weekday = (typeof WEEKDAYS)[number];
6
+ /** The values a rule's `unit` field may use (ADR 0860): every N weeks, months or years. */
7
+ export declare const RECURRING_UNITS: readonly ["week", "month", "year"];
8
+ export type RecurringUnit = (typeof RECURRING_UNITS)[number];
9
+ /** The largest N of "every N units". */
10
+ export declare const RECURRING_EVERY_MAX = 100;
6
11
  /** How far ahead the host makes records when a series does not say (days). */
7
12
  export declare const RECURRING_HORIZON_DAYS = 56;
8
13
  /** The most days ahead a series may ask for. */
9
14
  export declare const RECURRING_HORIZON_MAX = 366;
10
15
  /**
11
- * A weekly schedule that generates dated records (ADR 0500). The rule is kept in local
16
+ * A schedule that generates dated records (ADR 0500; the interval rule, ADR 0860). The rule is kept in local
12
17
  * wall-clock time in the series' zone: 06:00 is 06:00 on both sides of a clock change. A local
13
18
  * time that does not exist on a change day (02:30 when clocks go forward) moves forward by the
14
19
  * gap; one that happens twice is the first of the two.
@@ -18,7 +23,16 @@ export declare const RECURRING_HORIZON_MAX = 366;
18
23
  * - `rule` names this object's fields: `weekdays` (a multiSelect whose option values are
19
24
  * `mon`…`sun`), `time` (a text field, `HH:MM`), `length` (a number field, minutes), `from` and
20
25
  * `until` (date fields; `until` empty for ongoing) and `zone` (a timezone field; empty is the
21
- * organisation's zone).
26
+ * organisation's zone). Without `unit` the rule is weekly, as it always was.
27
+ * - The interval rule (ADR 0860) adds, all optional: `unit` (a select whose option values are
28
+ * some of `week`, `month`, `year`; empty is `week`), `every` (a number field: every N units,
29
+ * empty is 1), `day` (a number field: the day of the month, 1 to 31, for months and years; the
30
+ * month's last day when it is shorter; empty is `from`'s day), `month` (a number field: the
31
+ * month, 1 to 12, for years; empty is `from`'s month) and `lengthDays` (a number field: the
32
+ * length in whole days, ending at the same wall-clock time; instead of `length`). Dates are
33
+ * fixed on the calendar, counted from `from`: every 2 weeks from `from`'s week, every 3 months
34
+ * from `from`'s month, every year from `from`'s year, never from when the last was done.
35
+ * `weekdays` is needed only when `unit` may be `week`.
22
36
  * - `into` names the generated object's fields the host fills: `starts` (datetime), `ends`
23
37
  * (datetime, starts plus the length), `zone` (timezone) and `changed` (a read-only boolean the
24
38
  * host sets when someone other than the host edits the record).
@@ -36,6 +50,9 @@ export declare const RECURRING_HORIZON_MAX = 366;
36
50
  * - `horizonAtLeast` (optional): another of this extension's objects and a number field of it in
37
51
  * days (a class type's booking window): the horizon is never less than the largest value any
38
52
  * live record of it holds, so a class can be booked as soon as its window opens.
53
+ * - `horizonSetting` (optional, ADR 0626): a number setting of this extension, in days, that
54
+ * sets the horizon instead of `horizonDays` (a business choosing how far ahead its duties are
55
+ * made); `horizonDays` stays the default while the setting is empty.
39
56
  *
40
57
  * Made records are never made twice (the host keys each on the series and its local date). A
41
58
  * record edited by staff (its time, copied fields or state) is never overwritten or made again;
@@ -45,12 +62,17 @@ export interface RecurringScheduleSpec {
45
62
  generates: string;
46
63
  link: string;
47
64
  rule: {
48
- weekdays: string;
65
+ weekdays?: string;
49
66
  time: string;
50
67
  length?: string;
51
68
  from: string;
52
69
  until?: string;
53
70
  zone?: string;
71
+ unit?: string;
72
+ every?: string;
73
+ day?: string;
74
+ month?: string;
75
+ lengthDays?: string;
54
76
  };
55
77
  into: {
56
78
  starts: string;
@@ -74,17 +96,23 @@ export interface RecurringScheduleSpec {
74
96
  object: string;
75
97
  field: string;
76
98
  };
99
+ horizonSetting?: string;
77
100
  }
78
101
  export declare const recurringManifestSchema: z.ZodObject<{
79
102
  generates: z.ZodString;
80
103
  link: z.ZodString;
81
104
  rule: z.ZodObject<{
82
- weekdays: z.ZodString;
105
+ weekdays: z.ZodOptional<z.ZodString>;
83
106
  time: z.ZodString;
84
107
  length: z.ZodOptional<z.ZodString>;
85
108
  from: z.ZodString;
86
109
  until: z.ZodOptional<z.ZodString>;
87
110
  zone: z.ZodOptional<z.ZodString>;
111
+ unit: z.ZodOptional<z.ZodString>;
112
+ every: z.ZodOptional<z.ZodString>;
113
+ day: z.ZodOptional<z.ZodString>;
114
+ month: z.ZodOptional<z.ZodString>;
115
+ lengthDays: z.ZodOptional<z.ZodString>;
88
116
  }, z.core.$strict>;
89
117
  into: z.ZodObject<{
90
118
  starts: z.ZodString;
@@ -108,6 +136,7 @@ export declare const recurringManifestSchema: z.ZodObject<{
108
136
  object: z.ZodString;
109
137
  field: z.ZodString;
110
138
  }, z.core.$strict>>;
139
+ horizonSetting: z.ZodOptional<z.ZodString>;
111
140
  }, z.core.$strict>;
112
141
  export type RecurringScheduleManifest = z.infer<typeof recurringManifestSchema>;
113
142
  /** Reports problems with `objects.<name>.recurring` through `contract`. */